Data Structure Lab Manual
Data Structure Lab Manual
EXPERIMENT NO-1
Software : C
Theory :
In array implementation, the stack is formed by using the array. All the operations regarding
the stack are performed using arrays. Lets see how each operation can be implemented on
the stack using array data structure.
Adding an element into the top of the stack is referred to as push operation. Push operation
involves following two steps.
Algorithm:
begin
top = top + 1
end
Deletion of an element from the top of the stack is called pop operation. The value of the
variable top will be incremented by 1 whenever an item is deleted from the stack. The top
most element of the stack is stored in an another variable and then the top is decremented
by 1. the operation returns the deleted value that was stored in another variable as the
result.
Algorithm :
begin
item := stack(top);
top = top - 1;
end;
Begin
item = stack[top]
return item
End
C Program:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
top=-1;
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t ");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
{
printf("\n\t EXIT POINT ");
break;
default:
while(choice!=4);
return 0;
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
}
void pop()
if(top<=-1)
else
top--;
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
{
printf("\n The STACK is empty");
OUTPUT:
1. PUSH
2. POP
3. DISPLAY
4. EXIT
98
24
12
24
12
EXIT POINT
Software : C
Theory :
Infix expression: The expression of the form a operator b (a + b). When an operator is in-
between every pair of operands.
Postfix expression: The expression of the form a b operator (ab+). When an operator is
followed by every pair of operands.
Examples:
Input: A + B * C + D
Output: ABC*+D+
Else,
If the precedence and associativity of the scanned operator are greater than the
precedence and associativity of the operator in the stack(or the stack is empty or the stack
contains a ‘(‘ ), then push it.
‘^’ operator is right associative and other operators like ‘+’,’-‘,’*’ and ‘/’ are left-
associative. Check especially for a condition when both, operator at the top of the stack
and the scanned operator are ‘^’. In this condition, the precedence of the scanned
operator is higher due to its right associativity. So it will be pushed into the operator stack.
In all the other cases when the top of the operator stack is the same as the scanned
operator, then pop the operator from the stack because of left associativity due to which
the scanned operator has less precedence.
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}return 0;
}
abc*+
EXPERIMENT NO - 3
Software : C
Theory :
Scan the given expression from left to right and do the following for every scanned
element.
If the element is an operator, pop operands for the operator from the stack. Evaluate the
operator and push the result back to the stack
When the expression is ended, the number in the stack is the final answer
Algorithm
Postfix Evaluation(postfix)
Input: Postfix expression to evaluate.
Output: Answer after evaluating postfix form.
Begin
for each character ch in the postfix expression, do
if ch is an operator ⨀ , then
a := pop first element from stack
b := pop second element from the stack
res := b ⨀ a
push res into the stack
else if ch is an operand, then
add ch into the stack
done
return element of stack top
End
C Program:
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
OUTPUT:
Enter the expression :: 245+*
Software : C
Theory :
Doubly Linked List is a variation of Linked list in which navigation is possible in both ways,
either forward and backward easily as compared to Single Linked List. Following are the
important terms to understand the concept of doubly linked list.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
Prev − Each link of a linked list contains a link to the previous link called Prev.
LinkedList − A Linked List contains the connection link to the first link called First and to the
last link called Last.
struct node
{
int data;
Basic Operations
Insertion in DLL:
At the front of the DLL
The new node is always added before the head of the given Linked List. And newly added
node becomes the new head of DLL. For example, if the given Linked List is 1->0->1->5 and
we add an item 5 at the front, then the Linked List becomes 5->1->0->1->5.
2) Add a node after a given node:
We are given a pointer to a node as prev_node, and the new node is inserted after the
given node.
The new node is always added after the last node of the given Linked List. For example, if
the given DLL is 5->1->0->1->5->2 and we add item 30 at the end, then the DLL becomes 5-
>1->0->1->5->2->30.
Implement Doubly Linked List ADT
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
};
bool isEmpty() {
int length() {
int length = 0;
return length;
void displayForward() {
printf("\n[ ");
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
printf(" ]");
void displayBackward() {
printf("\n[ ");
while(ptr != NULL) {
//print data
printf("(%d,%d) ",ptr->key,ptr->data);
//create a link
link->key = key;
link->data = data;
if(isEmpty()) {
last = link;
} else {
head->prev = link;
link->next = head;
head = link;
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
last = link;
} else {
last->next = link;
link->prev = last;
last = link;
if(head->next == NULL){
last = NULL;
} else {
head->next->prev = NULL;
}
head = head->next;
return tempLink;
if(head->next == NULL) {
head = NULL;
} else {
last->prev->next = NULL;
last = last->prev;
return tempLink;
if(head == NULL) {
return NULL;
}
while(current->key != key) {
if(current->next == NULL) {
return NULL;
} else {
previous = current;
current = current->next;
if(current == head) {
head = head->next;
} else {
current->prev->next = current->next;
if(current == last) {
last = current->prev;
} else {
current->next->prev = current->prev;
}
return current;
if(head == NULL) {
return false;
while(current->key != key) {
if(current->next == NULL) {
return false;
} else {
current = current->next;
//create a link
newLink->key = newKey;
newLink->data = data;
if(current == last) {
newLink->next = NULL;
last = newLink;
} else {
newLink->next = current->next;
current->next->prev = newLink;
newLink->prev = current;
current->next = newLink;
return true;
void main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
displayForward();
printf("\n");
displayBackward();
displayForward();
deleteLast();
displayForward();
insertAfter(4,7, 13);
displayForward();
delete(4);
displayForward();
Output
List (First to Last):
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
Software : C
Theory:
Stack is a linear data structure that holds a linear, ordered sequence of elements. It
is an abstract data type. A Stack works on the LIFO process (Last In First Out), i.e.,
the element that was inserted last will be removed first. To implement the Stack, it is
required to maintain a pointer to the top of the Stack, which is the last element to be
inserted because we can access the elements only on the top of the Stack.
Operation on Stack
1. PUSH: PUSH operation implies the insertion of a new element into a Stack. A new
element is always inserted from the topmost position of the Stack; thus, we always
need to check if the top is empty or not, i.e., TOP=Max-1 if this condition goes false,
it means the Stack is full, and no more elements can be inserted, and even if we try
to insert the element, a Stack overflow message will be displayed.
Algorithm:
Print “Overflow”
Goto Step 4
Step-4: END
2. POP: POP means to delete an element from the Stack. Before deleting an
element, make sure to check if the Stack Top is NULL, i.e., TOP=NULL. If this
condition goes true, it means the Stack is empty, and no deletion operation can be
performed, and even if we try to delete, then the Stack underflow message will be
generated.
Algorithm:
Print “Underflow”
Goto Step 4
Step-4: END
3. PEEK: When we need to return the value of the topmost element of the Stack
without deleting it from the Stack, the Peek operation is used. This operation first
checks if the Stack is empty, i.e., TOP = NULL; if it is so, then an appropriate
message will display, else the value will return.
Algorithm:
Goto Step 3
Step-3: END
EXPERIMENT NO-6
Software : C
Theory :
A queue is a linear data structure that follows the First In, First Out (FIFO) principle, which
means that the element which is inserted first in the queue will be the first one to be
removed from the queue. A good example of a queue is a queue of customers purchasing a
train ticket, where the customer who comes first will be served first.
Insertion
Insert operation or insertion on a linked queue adds an element to the end of the queue.
The new element which is added becomes the last element of the queue.
if (front == NULL) {
front = ptr;
rear = ptr;
}
Deletion
Deletion or delete operation on a linked queue removes the element which was first
inserted in the queue, i.e., always the first element of the queue is removed.
If the queue is empty, i.e., front==NULL, so we just print 'underflow' on the screen and exit.
If the queue is not empty, delete the element at which the front pointer is pointing. For
deleting a node, copy the node which is pointed by the front pointer into the pointer ptr and
make the front pointer point to the front's next node and free the node pointed by the node
ptr. This can be done using the following statement:
*ptr = front;
free(ptr);
1. Enqueue Function
Enqueue function adds an element to the end of the queue. It takes O(1) time. The last
element can be tracked using the rear pointer.
If a queue is empty then, a new node is assigned to the front and rear.
Else make next of rear as new node and rear as a new node.
2. Dequeue Function
The dequeue function always removes the first element of the queue. It takes O(1) time. For
dequeue, the queue must contain at least one element, else underflow conditions will
occur.
C Program:
struct node {
int data;
};
} else {
rear = ptr;
printf("Node is Inserted\n\n");
int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
free(temp);
return temp_data;
printf("\nQueue is Empty\n");
} else {
temp = front;
while (temp) {
printf("NULL\n\n");
int main() {
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
return 0;
Output:
Enqueue Operation:
Implementation of Queue using Linked List
1. Enqueue
2. Dequeue
3. Display
4. Exit
Node is Inserted
1.Enqueue
2.Dequeue
3.Display
4.Exit
Node is Inserted
1.Enqueue
2.Dequeue
3.Display
4.Exit
Node is Inserted
1.Enqueue
2.Dequeue
3.Display
4.Exit
The queue is
12--->45--->56--->NULL
Dequeue Operation:
The queue is
12--->45--->56--->NULL
1.Enqueue
2.Dequeue
3.Display
4.Exit
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
1. Enqueue
2. Dequeue
3. Display
4. Exit
The queue is
56--->NULL
1.Enqueue
2.Dequeue
3.Display
4.Exit
1. Enqueue
2. Dequeue
3. Display
4. Exit
Experiment No--7
Software : C
Program:
#include<stdio.h>
#define max 5
int CQueue[max];
void insert();
int delete();
void display();
int main() {
int w, no;
for (;;) {
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. EXIT");
switch (w) {
case 1:
insert();
break;
case 2:
no = delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
void insert() {
int no;
return;
if (front == -1)
front = front + 1;
if (rear == max - 1)
rear = 0;
else rear = rear + 1;
CQueue[rear] = no;
int delete() {
int e;
if (front == -1) {
e = CQueue[front];
if (front == max - 1)
front = 0;
front = -1;
rear = -1;
return e;
void display() {
int i;
if (front == -1) {
return;
}
i = front;
printf("\n\n");
printf("\n");
} else {
printf("\n\n");
i = 0;
printf("\n");
}
Experiment No--8
Software : C
Theory: t is a type of linked list in which traversing occurs only in one direction from the
head to the end node. Every node contains data and the pointer which contains the
address of the next node.
Example:
The most common type of linked list is a single linked list. A singly linked list can only be
traversed in one direction. Each node in a singly linked list has data as well as a pointer
to the next node.
Program:
#include <stdio.h>
#include <stdlib.h>
int item;
}Node;
typedef struct
{Node * head;
Node * tail;
}List;
//Initialize List
lp->head = NULL;
lp->tail = NULL;
Node * nNode;
nNode->item = item;
nNode->next = NULL;
return nNode;
Node * node;
node = createNode(item);
//if list is empty.
if(lp->head == NULL)
lp->head = node;
lp->tail = node;
else
lp->tail->next = node;
lp->tail = lp->tail->next;
Node * temp;
int i = 0;
int item = 0;
if(lp->tail == NULL)
return -1;
else
temp = lp->head
while(temp->next != lp->tail)
{ temp = temp->next;}
item = lp->tail->item;
lp->tail = temp;
lp->tail->next = NULL;
return item;
Node * node;
node = createNode(item);
if(lp->head == NULL)
lp->head = node;
lp->tail = node;
else
node->next = lp->head ;
lp->head = node ;
}}
int item = 0;
if(lp->head == NULL)
return -1;
else
item = lp->head->item;
lp->head = lp->head->next;
return item;
Node * node;
if(lp->head == NULL)
{printf("\nEmpty List");
return;
node = lp->head;
printf("\nList: \n\n\t");
while(node != NULL)
node = node->next;
if(node !=NULL)
printf("--->");
printf("\n\n");
int main()
List *lp;
int item = 0;
lp = (List *) malloc(sizeof(List));
initList(lp);
addAtTail(lp,10);
addAtTail(lp,20);
addAtHead(lp,30);
addAtHead(lp,40);
printList(lp);
item = delFromTail(lp);
if(item >= 0)
printList(lp);
item = delFromHead(lp);
if(item >= 0)
printList(lp);
return 0;
}
EXPERIMENT NO-9
Software : C
Theory :
Graph traversal is a technique used for a searching vertex in a graph. The graph traversal is
also used to decide the order of vertices is visited in the search process. A graph traversal
finds the edges to be used in the search process without creating loops. That means using
graph traversal we visit all the vertices of the graph without getting into looping path.
There are two graph traversal techniques and they are as follows...
DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Stack data structure with maximum size of total number of vertices
in the graph to to implement DFS traversal.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on to
the Stack.
Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top of
stack and push it on to the stack.
Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at
the top of the stack.
Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex
from the stack.
Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused
edges from the graph
C Program:
#include <stdio.h>
#include <stdlib.h>
int vis[100];
// Adjacency matrix
struct Graph {
int V;
int E;
int** Adj;
};
malloc(sizeof(struct Graph));
if (!G) {
printf("Memory Error\n");
return NULL;
G->V = 7;
G->E = 7;
G->Adj[u][v] = 0;
G->Adj[0][1] = G->Adj[1][0] = 1;
G->Adj[0][2] = G->Adj[2][0] = 1;
G->Adj[1][3] = G->Adj[3][1] = 1;
G->Adj[1][4] = G->Adj[4][1] = 1;
G->Adj[1][5] = G->Adj[5][1] = 1;
G->Adj[1][6] = G->Adj[6][1] = 1;
G->Adj[6][2] = G->Adj[2][6] = 1;
return G;
{
vis[u] = 1;
DFS(G, v);
vis[i] = 0;
if (!vis[i]) {
DFS(G, i);
// Driver code
void main()
struct Graph* G;
G = adjMatrix();
DFStraversal(G);
}
EXPERIMENT NO-10
Software : C
Theory :
BFS is the most commonly used approach. It is a recursive algorithm to search all the
vertices of a tree or graph data structure. BFS puts every vertex of the graph into two
categories - visited and non-visited. It selects a single node in a graph and, after that, visits
all the nodes adjacent to the selected node.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it into
the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.
Step 4 - When there is no new vertex to be visited from the vertex which is at front of the
Queue then delete that vertex.
Step 6 - When queue becomes empty, then produce final spanning tree by removing unused
edges from the graph.
C Program:
#include <stdio.h>
int adj[10][10];
void bfs(int v)
queue[++rear] = i;
visited[queue[front]] = 1;
bfs(queue[front++]);
void main()
int v;
scanf("%d", &n);
queue[i] = 0;
visited[i] = 0;
scanf("%d", &adj[i][j]);
scanf("%d", &v);
bfs(v);
printf("The node which are reachable are: \n");
if (visited[i])
printf("%d\t", i);
else
return 0;