Data Structures
Data Structures
data structure is a data organization, management and storage format that enables efficient access and modification. More
precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be
applied to the data.
Data structure are classified into two type such as linear or non-linear.
Linear: A data structure is said to be linear if its elements form a sequence. The elements of linear data
structure represents by means of sequential memory locations. The other way is to have the linear
relationship between the elements represented by means of pointers or links. Ex- Array and Link List.
Non-linear: A data structure is said to be non-linear if its elements a hierarchical relationship between
elements such as trees and graphs. All elements assign the memory as random form and you can fetch
data elements through random access process.
Following operations can be performed on the data structures:
1. Traversing
2. Searching
3. Inserting
4. Deleting
5. Sorting
6. Merging
1. Traversing- It is used to access each data item exactly once so that it can be processed.
2. Searching- It is used to find out the location of the data item if it exists in the given collection of data
items.
3. Inserting- It is used to add a new data item in the given collection of data items.
4. Deleting- It is used to delete an existing data item from the given collection of data items.
5. Sorting- It is used to arrange the data items in some order i.e. in ascending or descending order in
case of numerical data and in dictionary order in case of alphanumeric data.
6. Merging- It is used to combine the data items of two sorted files into single file in the sorted form.
Arrays:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
1 2 3 4 5 6 7 8 9 10
C program to delete an element in an array: This program deletes or removes an element from an array. A
user will enter the position at which the array element deletion is required. Deleting an element does not
affect the size of the array. It also checks whether deletion is possible or not, for example, if an array
contains five elements and user wants to delete the element at the sixth position, it isn't possible.
Linear Search
Binary Search
A linear search scans one item at a time, without jumping to any item .
1. The worst case complexity is O(n), sometimes known an O(n) search
2. Time taken to search elements keep increasing as the number of elements are increased.
A binary search however, cut down your search to half as soon as you find middle of a sorted list.
1. The middle element is looked to check if it is greater than or less than the value to be searched.
2. Accordingly, search is done to either half of the given list
Important Differences
Input data needs to be sorted in Binary Search and not in Linear Search
Linear search does the sequential access whereas Binary search access data randomly.
Time complexity of linear search -O(n) , Binary search has time complexity O(log n).
Linear search performs equality comparisons and Binary search performs ordering comparisons
Let us look at an example to compare the two:
Linear Search to find the element “J” in a given sorted list from A-X
Binary Search to find the element “J” in a given sorted list from A-X
Bubble sort:
C program for bubble sort: C programming code for bubble sort to sort numbers or arrange them in
ascending order. You can modify it to print numbers in descending order. You can also sort strings using
Bubble sort, it is less efficient as its average and worst case complexity is high, there are many other fast
sorting algorithms like quick-sort, heap-sort, etc. Sorting simplifies problem-solving in computer
programming.
Sparse Matrix:
Stack:
A stack data structure can be implemented using one dimensional array. But stack implemented
using array stores only fixed number of data values. This implementation is very simple. Just
define a one dimensional array of specific size and insert or delete the values into that array by
using LIFO(Last In First Out) principle with the help of a variable called 'top'. Initially top is set
to -1. Whenever we want to insert a value into the stack, increment the top value by one and
then insert. Whenever we want to delete a value from the stack, then delete the top value and
decrement the top value by one.
Before implementing actual operations, first follow the below steps to create an empty stack.
Step 1 - Include all the header files which are used in the program and define a constant 'MAX' with
specific value.
Step 2 - Declare all the functions used in stack implementation.
Step 3 - Create a one dimensional array with fixed size (int stack[MAX])
Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5 - In main method, display menu with list of operations and make suitable function calls to
perform operation selected by the user on the stack.
Applications of STACK:
1. During function calls
2. recursive functions
3. Infix expression to postfix expression coversion
4. Postfix expression evaluation
5. Backtracking
6. Compiler design aspects (parsing)
Program for STACK:
#include<stdio.h>
#define MAX 5
int s[MAX],tos=-1;
void push(int);
int pop();
void disp();
int main()
{
int ch,ele;
do{
printf("\nMENU\n1.Push()\n2.Pop()\n3.Display\n4.Exit\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf(" Enter to be insert ele: ");
scanf("%d",&ele);
push(ele);
break;
case 2:
ele=pop();
printf(" the pop item from stack is: %d",ele);
break;
case 3:
disp();
break;
case 4:
printf("End of program: ");
break;
default:
printf(" Invalid choice: ");
}
}while(ch!=4);
return 0;
}
void push(int ele)
{
if(tos==MAX-1)
{
printf(" stack is full: ");
}
else
{
tos++;
s[tos]=ele;
printf("element inserted successfully: ");
}
}
int pop()
{
if(tos==-1)
{
printf(" stack is empty:");
return 0;
}
else
{
int item=s[tos];
tos--;
return item;
}
}
void disp()
{
int i;
if(tos==-1)
{
printf(" stack is empty:");
}
else
{
for(i=tos;i>=0;i--)
{
printf(" %d\n",s[i]);
}
}
}
Ouput:
MENU MENU
1.Push() 1.Push()
2.Pop() 2.Pop()
3.Display 3.Display
4.Exit 4.Exit
Enter your choice: 1 Enter your choice: 2
Enter to be insert ele: 10 the pop item from stack is: 50
element inserted successfully: MENU
MENU 1.Push()
1.Push() 2.Pop()
2.Pop() 3.Display
3.Display 4.Exit
4.Exit Enter your choice: 2
Enter your choice: 1 the pop item from stack is: 40
Enter to be insert ele: 20 MENU
element inserted successfully: 1.Push()
MENU 2.Pop()
1.Push() 3.Display
2.Pop() 4.Exit
3.Display Enter your choice: 2
4.Exit the pop item from stack is: 20
Enter your choice: 1 MENU
Enter to be insert ele: 30 1.Push()
element inserted successfully: 2.Pop()
MENU 3.Display
1.Push() 4.Exit
2.Pop() Enter your choice: 2
3.Display the pop item from stack is: 10
4.Exit MENU
Enter your choice: 1 1.Push()
Enter to be insert ele: 40 2.Pop()
element inserted successfully: 3.Display
MENU 4.Exit
1.Push() Enter your choice: 2
2.Pop() stack is empty: the pop item from stack is: 0
3.Display MENU
4.Exit 1.Push()
Enter your choice: 1 2.Pop()
Enter to be insert ele: 50 3.Display
element inserted successfully: 4.Exit
MENU Enter your choice: 5
1.Push() Invalid choice:
2.Pop() MENU
3.Display 1.Push()
4.Exit 2.Pop()
Enter your choice: 1 3.Display
Enter to be insert ele: 60 4.Exit
stack is full: Enter your choice: 4
MENU End of program:
1.Push() --------------------------------
2.Pop() Process exited after 54.23 seconds with return
3.Display value 0
4.Exit
Enter your choice: 3 Press any key to continue . . .
50
40
30
20
10
MENU
1.Push()
2.Pop()
3.Display
4.Exit
Enter your choice: 1
Enter to be insert ele: 60
stack is full:
A queue is a useful data structure in programming. It is similar to the ticket queue outside a cinema hall, where the first
person entering the queue is the first person who gets the ticket.
Queue follows the First In First Out(FIFO) rule - the item that goes in first is the item that comes out first too.
In the above image, since 1 was kept in the queue before 2, it was the first to be removed from the queue as well. It follows
the FIFO rule.
In programming terms, putting an item in the queue is called an "enqueue" and removing an item from the queue is called
"dequeue".
We can implement queue in any programming language like C, C++, Java, Python or C#, but the specification is pretty much
the same.
Queue Specifications
A queue is an object or more specifically an abstract data structure(ADT) that allows the following operations:
Enqueue: Add element to end of queue
Dequeue: Remove element from front of queue
Display: Display elements from FRONT to REAR if queue is not empty
IsEmpty: Check if queue is empty
IsFull: Check if queue is full
How Queue Works
Queue operations work as follows:
1. Two pointers called FRONT and REAR are used to keep track of the first and last elements in the queue.
2. When initializing the queue, we set the value of FRONT and REAR to -1.
3. On enqueing an element, we increase the value of REAR index and place the new element in the position pointed to
by REAR.
4. On dequeueing an element, we return the value pointed to by FRONT and increase the FRONT index.
5. Before enqueing, we check if queue is already full.
6. Before dequeuing, we check if queue is already empty.
7. When enqueing the first element, we set the value of FRONT to 0.
8. When dequeing the last element, we reset the values of FRONT and REAR to -1.
Applications of QUEUE
1. In Operating system, to process the scheduling on processes.
Execution:
Circular Queue:
In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the
next element even if there is a space in front of queue.
1. Check whether queue is Full – Check ((rear == MAX-1 && front == 0) || ((rear+1)%MAX == front)).
2. if it is full then display Queue is full. If queue is not full then
check if rear==-1 and front ==-1 then set rear = 0, front=0, cq[rear]=element;
else
set rear=(rear+1)%MAX and insert element cq[rear]=element;
.
deQueue() This function is used to delete an element from the circular queue. In a circular queue, the element is
always deleted from front position.
Steps:
Execution:
Postfix evaluation:
Example:
Let the given expression be “2 3 1 * + 9 -“. We scan all elements one by one.
1) Scan ‘2’, it’s a number, so push it to stack. Stack contains ‘2’
2) Scan ‘3’, again a number, push it to stack, stack now contains ‘2 3’ (from bottom to top)
3) Scan ‘1’, again a number, push it to stack, stack now contains ‘2 3 1’
4) Scan ‘*’, it’s an operator, pop two operands from stack, apply the * operator on operands, we get 3*1
which results in 3. We push the result ‘3’ to stack. Stack now becomes ‘2 3’.
5) Scan ‘+’, it’s an operator, pop two operands from stack, apply the + operator on operands, we get 3 +
2 which results in 5. We push the result ‘5’ to stack. Stack now becomes ‘5’.
6) Scan ‘9’, it’s a number, we push it to the stack. Stack now becomes ‘5 9’.
7) Scan ‘-‘, it’s an operator, pop two operands from stack, apply the – operator on operands, we get 5 –
9 which results in -4. We push the result ‘-4’ to stack. Stack now becomes ‘-4’.
8) There are no more elements to scan, we return the top element from stack (which is the only element
left in stack).
Execution: