Module No 2 Linear Data Structures
Module No 2 Linear Data Structures
Algorithms
Course Code: CSE2001
MODULE – 2
(Linear Data Structures)
Data Structures
Types of Linear Data Structures
Arrays are a fundamental data structure in computer science. They are used in a
wide variety of applications, including:
Storing data for processing
Implementing data structures such as stacks and queues
Representing data in tables and matrices
Creating dynamic data structures such as linked lists and trees
One-dimensional array
Two-dimensional array
Declaration of Array
0 1 2 3 4
Declaration of Array
Method-01 Int a[5] = [1,2,3,4,5]; [1,2,3,4,5]
Method-02 Int a[] = {1,2,3,4,5} [1,2,3,4,5]
Int a[5];
a[0]=1
Method-03 a[1]=2 [1,2,3,4,5]
a[2]=3
a[3]=4
a[4]=5
return 0;
}
Insertion element into Array
(Initialized counter) J = n
Repeat step 3 & 4
while j >= k[Move downward] set LA[J + 1] = LA[J]
[Decrement counter] set j = j - 1;
End of step 2[Insert element] set LA[k] = ITEM[Reset n] set n = n + 1
Exit
Insertion element into Array
#include <iostream>
int main() {
int arr[6] = {10, 20, 30, 40, 50}; // Declare and initialize an array
for (int i = size; i > position; i--) { // Shift elements to the right to make space for the new element
std::cout << "Array after insertion: "; // Display the array after insertion
}
Deletion element into Array
if we need to delete an element from the middle of the array, then on
average, half of the elements must be moved upward, in order to fill the
blank space, after deleting the element from the specified location.
Algorithm
(Here, LA is a linear array with 'n' number of elements and K is a positive integer number, such that k<=N. This
algorithm removes the element from the Kth position)
#include <string>
int main() {
int n = 5;
std::cout << "Elements before deleting the element:\n"; // Display elements before deletion
std::cout << "Position(" << i << ") " << name[i] << std::endl;
std::cout << "\nElements after deleting the element:\n"; // Display elements after deletion
std::cout << "Position(" << i << ") " << name[i] << std::endl;
return 0;
}
Sorting of Array
#include <iostream>
int main() {
int n = 5;
std::cout << "Position(" << i << ") " << numbers[i] << std::endl; }
int minIndex = i;
minIndex = j; }}
if (minIndex != i) {
numbers[i] = numbers[minIndex];
numbers[minIndex] = temp; }}
std::cout << "Position(" << i << ") " << numbers[i] << std::endl; }
return 0; }
Searching Output:
#include <iostream>
int main() {
int n = 5;
std::cout << "Position(" << i << ") " << numbers[i] << std::endl; }
if (numbers[i] == target) {
foundIndex = i;
break; }}
std::cout << "\nElement " << target << " found at position " << foundIndex << ".\n";
} else {
std::cout << "\nElement " << target << " not found in the array.\n";}
return 0;
}
Merging
#include <iostream>
int n2 = 3;
int mergedArray[mergedSize];
for (int i = 0; i < n1; i++) { // Copy elements from the first array to the
merged array
mergedArray[i] = array1[i]; }
for (int i = 0; i < n2; i++) { // Copy elements from the second array to the
merged array
mergedArray[n1 + i] = array2[i]; }
Expression Conversion
Check Parenthesis
Memory Management and Function call
Back Tracking
String Reversal
Conversion Evolution
Tower of Hanoi
Expression Conversion
Infix Expression
5 + 6, A – B, (P * 5)
Syntax: <operand> <operator> <operand>
If the expression is: 4+6*2
If the plus operator is evaluated first, then the expression would look like: 10 * 2 = 20
If the multiplication operator is evaluated first, then the expression would look like:
4 + 12 = 16
operator precedence rules
Higher
Lower
For example: 2^2^3 = 2 ^ 8 = 256
Hence to solve the Infix Expression compiler will scan the expression
multiple times to solve the sub-expressions in expressions orderly
which is very in-efficient.
43
Queues in Data Structures
Queues
Syntax:
Data_type queue_name[size]
Int FRONT=-1
Int REAR=-1
Example:
Queue = MyQueue[10]
Int FRONT=-1
Int REAR=-1
Working of Queue
In this operation, the element is inserted from the front end of the queue. Before
implementing the operation, we first have to check whether the queue is full or not. If
the queue is not full, then the element can be inserted from the front end by using
the below conditions -
If the queue is empty, both rear and front are initialized with 0. Now, both will
point to the first element.
Otherwise, check the position of the front if the front is less than 1 (front < 1),
then reinitialize it by front = n - 1, i.e., the last index of the array.
11/12/2024 52
Insertion at Rear End
In this operation, the element is inserted from the rear end of the queue. Before
implementing the operation, we first have to check again whether the queue is full or
not. If the queue is not full, then the element can be inserted from the rear end by
using the below conditions –
If the queue is empty, both rear and front are initialized with 0. Now, both will
point to the first element.
Otherwise, increment the rear by 1. If the rear is at last index (or size - 1), then
instead of increasing it by 1, we have to make it equal to 0.
11/12/2024 53
Deletion at Front end
In this operation, the element is deleted from the front end of the queue. Before
implementing the operation, we first have to check whether the queue is empty or
not.
If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot
perform the deletion. If the queue is not full, then the element can be inserted from
the front end by using the below conditions -
If the deque has only one element, set rear = -1 and front = -1.
Else if front is at end (that means front = size - 1), set front = 0.
Else increment the front by 1, (i.e., front = front + 1).
11/12/2024 54
Deletion at rear end
In this operation, the element is deleted from the rear end of the queue. Before
implementing the operation, we first have to check whether the queue is empty or
not.
If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot
perform the deletion.
If the deque has only one element, set rear = -1 and front = -1.
If rear = 0 (rear is at front), then set rear = n - 1.
Else, decrement the rear by 1 (or, rear = rear -1).
IsEmpty() operation in Queue
Empty Queue
IsFull() operation in Queue
There are four different types of queues that are used in different
scenarios. They are:
1. Simple Queue or Linear Queue
2. Circular Queue
3. Priority Queue
4. Double Ended Queue (or Deque)
Simple Queue or Linear Queue
Circular Queue
The address of the first node a special name called HEAD. Also, the last node
in the linked list can be identified because its next portion points to NULL.
Linked lists can be of multiple types: singly, doubly, and circular linked
list. In this article, we will focus on the singly linked list.
Terminologies of Linked List
Linked List is a linear data structure, in which elements are not stored at
a contiguous location, rather they are linked using pointers. Linked List
forms a series of connected nodes, where each node stores the data
and the address of the next node.
You can see there is a pointer HEAD to the first node of the list
Here the nodes are scatter in the memory locations but still
connected with each other.
The link part of the each node contains the address of the next node.
First node contain the address of second node and second node
contains the address of third node like wise.
Types of Linked List
In the above code, one, two, and three are the nodes with data items 1,
2, and 3 respectively.
For node one
next stores the address of two (there is no node before it)
For node two
next stores the address of three
For node three
next stores NULL (there is no node after it)
next points to node one
Insertion
11/12/2024 88
If any other nodes are to be
deleted
• travel to the node to be
deleted (here we are deleting
node 2)
• let the node before node 2
be temp
• store the address of the node
next to 2 in temp
• free the memory of 2
key applications of linked lists