C Lab Manual: Data Structures Guide
C Lab Manual: Data Structures Guide
IV Semester
Data Structure using C Lab Manual
Subject code: BECL456D
LIST of Experiments
[Link] Experiment name
Write a C Program to create a student record structure to store, N records, each record
having the structure shown below: USN, Student Name and Semester. Write necessary
functions a. To display all the records in the file. b. To search for a specific record based
1 on the USN. In case the record is not found, suitable message should be displayed. Both
the options in this case must be demonstrated. (Use pointer to structure for dynamic
memory allocation)
Write a C Program to construct a stack of integers and to perform the following
2 operations on it: a. Push b. Pop c. Display The program should print appropriate
messages for stack overflow, stack underflow, and stack empty.
Write a C Program to convert and print a given valid parenthesized infix arithmetic
expression to postfix expression. The expression consists of single character operands
3 and the binary operators + (plus), - (minus), * (multiply) and / (divide).
GENERAL INSTRUCTIONS
1. Students are instructed to come to Data Structures through C++ laboratory on time.
Late comers are not entertained in the lab.
2. Students should be punctual to the lab. If not, the conducted experiments will not be
repeated.
3. Students are expected to come prepared at home with the experiments which are going to be
performed.
4. Students are instructed to display their identity cards before entering into the lab.
6. Any damage/loss of system parts like keyboard, mouse during the lab
session, it is student’s responsibility and penalty or fine will be collected from
the student.
7. Students should update the records and lab observation books session wise. Before
leaving the lab the student should get his lab observation book signed by the faculty.
8. Students should submit the lab records by the next lab to the concerned faculty members
in the staffroom for their correction and return.
9. Students should not move around the lab during the lab session.
10. If any emergency arises, the student should take the permission from faculty
member concerned in written format.
11. The faculty members may suspend any student from the lab session on disciplinary grounds.
12. Never copy the output from other students. Write down your own outputs.
Experiment-1:
Write a C Program to create a student record structure to store, N records, each record
having the structure shown below: USN, Student Name and Semester. Write necessary
functions a. To display all the records in the file. b. To search for a specific record based on
the USN. In case the record is not found, suitable message should be displayed. Both the
options in this case must be demonstrated. (Use pointer to structure for dynamic memory
allocation)
#include <stdio.h>
#include <stdlib.h>
struct student {
char firstName[50];
int USN;
float marks;
};
// displaying information
printf("Name = %s\n",(s+l)->firstName);
void main() {
int N=3;
int i,usn,ch=1;
// storing information
scanf("%d",&(s1+i)->USN);
scanf("%s", &(s1+i)->firstName);
scanf("%f", &(s1+i)->marks);
printf("Displaying Information:\n\n");
for(i=0;i<N;i++)
display(s1,i);
while(ch)
scanf("%d",&ch);
if(ch==1)
scanf("%d",&usn);
for(i=0;i<N;i++)
if((s1+i)->USN == usn)
display(s1,i);
break;
if(i==N)
else
break;
free(s1);
OUTPUT:
Displaying Information:
Name = madhu
Marks= 98.500000
Name = manu
Marks= 85.400002
Name = Alex
Marks= 65.500000
111
Name = madhu
Marks= 98.500000
454
No records found
Experiment-2:
#include<stdio.h>
int stack[100]; /*Declaring a 100 element stack array*/
int top=-1; /*Declaring and initializing the stack pointer*/
void push(int); /*Declaring a function prototype for inserting an element into the stack*/
int pop(); /*Declaring a function prototype for removing an element from the stack*/
void display(); /*Declaring a function prototype for displaying the elements of a stack*/
int main()
{
int choice;
int num1=0,num2=0;
while(1)
{
printf("Your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("Enter the element to be pushed into the stack:\n");
scanf("%d",&num1);
push(num1); /*Inserting an element*/
break;
}
case 2:
{
num2=pop(); /*Removing an element*/
printf("element popped out of the stack = %d\n",num2);
break;
}
case 3:
{
display(); /*Displaying stack elements*/
break;
}
case 4:
exit(1);
break;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
/*Push function*/
void push(int element)
{
if(top==99) /*Checking whether the stack is full*/
{
printf("Stack is Full/stack overflow.\n\n");
exit(1);
}
top=top+1; /*Incrementing stack pointer*/
stack[top]=element; /*Inserting the new element*/
}
/*Pop function*/
int pop()
{
if(top==-1) /*Checking whether the stack is empty*/
{
printf("Stack is Empty/ stack under flow\n");
exit(1);
}
return(stack[top--]); /*Returning the top element and decrementing the stack pointer*/
}
void display()
{
int i;
printf("The various stack elements are:\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]); /*Printing stack elements*/
}
OUTPUT:
56
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
55
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
1
Enter the element to be pushed into the stack:
65
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
3
The various stack elements are:
65
55
56
85
78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 65
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
3
The various stack elements are:
55
56
85
78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 55
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 56
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 85
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
Stack is Empty/ stack under flow
Experiment-3:
Write a C Program to convert and print a given valid parenthesized infix arithmetic
expression to postfix expression. The expression consists of single character operands
and the binary operators + (plus), - (minus), * (multiply) and / (divide).
Program:
#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;
}
OUTPUT:
abc-*de/+
Experiment-4:
Write a C Program to simulate the working of a queue of integers using an array. Provide
the following operations: a. Insert b. Delete c. Display
void insert(int); /*Declaring a function prototype for inserting an element into the queue*/
int del(); /*Declaring a function prototype for removing an element from the queue*/
void display(); /*Declaring a function prototype for displaying the queue elements*/
void main()
{
int choice;
int num1=0,num2=0;
while(1)
{
switch(choice)
{
case 1:
{
printf("\n\tEnter the element to be added to the queue:");
scanf("%d",&num1);
insert(num1); /*Adding an element*/
break;
}
case 2:
{
num2=del(); /*Removing an element*/
if(num2 == -9999)
printf( "Element is not deleted \n");
else
printf("\n\t%d element removed from the queue\n\t",num2);
break;
}
case 3:
{
display(); /*Displaying queue elements*/
break;
}
case 4:
exit(1);
break;
default:
printf("\nInvalid choice!\n");
break;
}
}
}
/*Insert function*/
void insert(int element)
{
if(front==-1) /*Adding element in an empty queue*/
{
front = rear = front+1;
queue[rear] = element;
return;
}
printf("\n\tQueue is Empty.\n");
return (-9999);
}
if(front!=-1 && front==rear) /*Checking whether the queue has only one element
left*/
{
i=queue[front];
front=-1;
rear=-1;
return(i);
}
return(queue[front++]); /*Returning the front most element and incrementing the front
pointer*/
}
/*Display function*/
void display()
{
int i;
if(front==-1)
{
printf("\n\tQueue is Empty!\n");
return;
}
OUTPUT:
Your choice: 1
Your choice: 1
Your choice: 3
Your choice: 1
Your choice: 3
Your choice: 1
Your choice: 3
Your choice: 2
Your choice: 3
Your choice: 1
Your choice: 3
Experiment-5
Write a C Program using dynamic variables and pointers to construct a stack of integers using
singly linked list and to perform the following operations: a. Push b. Pop c. Display The
program should print appropriate messages for stack overflow and stack empty.
Program:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
int element;
}*top;
the stack*/
elements of a stack*/
void main()
while(1)
printf("Your choice:\n");
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&num1);
break;
case 2:
break;
case 3:
break;
case 4:
exit(1);
break;
default:
printf("Invalid choice!\n");
break;
/*Push function*/
ptr->next=top;
top=ptr;
return;
/*Pop function*/
int pop()
exit(1);
else
/*display */
void display()
ptr1=top;
while(ptr1!=NULL)
ptr1=ptr1->next;
OUTPUT:
55
56
85
78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 55
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 56
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 85
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
element popped out of the stack = 78
Select a choice from the following:
1- Push, 2- POP, 3-Display, 4- exit
Your choice:
2
Stack is Empty/ stack under flow
Experiment – 6:
Write a C Program to support the following operations on a doubly linked list where each
node consists of integers:
b. Delete the node of a given data, if it is found, otherwise display appropriate message.
Algorithm:
The following structure declaration defines the node of a doubly linked list:
➢ The above structure declaration defines a new data type called NODE that represents
a doubly linked list node.
➢ The node structure contains three members, INFO for storing integer data values,
NEXT for storing address of the next node, and PREVIOUS for storing the address of
the previous node.
INSERT:
➢ The insert operation in a doubly linked list is performed in the same manner as a
singly linked list.
➢ The only exception is that the additional node pointer PREVIOUS is also required to
be updated for the new node at the time of insertion.
DELETE:
➢ Delete The delete operation in a doubly linked list is performed in the same
manner as a singly linked list.
➢ The only exception is that the additional node pointer PREVIOUS of the adjacent
node is also required to be updated at the time of deletion.
SEARCH:
PRINT:
➢ The print operation prints or displays the linked list elements on the screen.
➢ To print the elements, the linked list is traversed from start till end using NEXT
pointers.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int INFO;
};
struct dl_node *PTR =(struct dl_node *)malloc(sizeof(struct dl_node )); /*Storing the
element to be inserted in the new node*/
if(FIRST==NULL)
PTR->NEXT=NULL;
PTR->PREVIOUS=NULL;
else
LAST->NEXT = PTR;
PTR->NEXT = NULL;
PTR->PREVIOUS = LAST;
LAST = PTR;
/*Search function*/
return(NULL);
return(FIRST);
for(PTR=FIRST;PTR!=LAST;PTR=PTR->NEXT)
if(PTR->INFO==value)
if(LAST->INFO==value)
return(LAST);
else
/*Delete function*/
int i;
i=value;
return(-9999);
if(LOC==FIRST)
if(FIRST==LAST)
FIRST=LAST=NULL;
else
FIRST=FIRST->NEXT;
FIRST->PREVIOUS=NULL;
return(value);
for(TEMP=FIRST;TEMP->NEXT!=LOC;TEMP=TEMP->NEXT);
if(LOC==LAST)
LAST=TEMP;
TEMP->NEXT=NULL;
else
TEMP->NEXT=LOC->NEXT;
LOC->NEXT->PREVIOUS=TEMP;
return(LOC->INFO);
/*print function*/
void print()
printf("Empty List\n");
return;
for(PTR=FIRST;PTR!=NULL;PTR=PTR->NEXT)
printf("%d\t",PTR->INFO);
int main()
while(1)
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&num1);
break;
case 2:
printf("Enter the element to be deleted from the doubly linked list: \n");
scanf("%d",&num1);
if(num2==-9999)
else
break;
case 3:
break;
case 4:
exit(1);
break;
default:
break;
return 0;
OUTPUT:
Select an option
55
Select an option
35
Select an option
89
Select an option
78
Select an option
Select an option
55 35 89 78 Select an option
35
Select an option
55 89 78
Experiment -7:
if tree is empty then insert a node as root node otherwise go to next step if ele is less than the
root node the insert left sub tree of root node otherwise insert right sub tree of root node
Algorithm inorder(ptr)
else
while(ptr!=NULL||top>=0)
if(ptr!=NULL)
ptr=ptr->left;
else
Source Code:
#include <stdio.h>
#include<stdlib.h>
struct node
int INFO;
};
if(r==NULL)
r->INFO = n;
else if(n<r->INFO)
else if(n>r->INFO)
else if(n==r->INFO)
return(r);
//inorder traversal
if(r->LEFT!=NULL)
inorder(r->LEFT);
printf("%d\t",r->INFO);
if(r->RIGHT!=NULL)
inorder(r->RIGHT);
//preorder traversal
printf("%d\t",r->INFO);
if(r->LEFT!=NULL)
preorder(r->LEFT);
if(r->RIGHT!=NULL)
preorder(r->RIGHT);
//postorder traversal
if(r->LEFT!=NULL)
postorder(r->LEFT);
if(r->RIGHT!=NULL)
postorder(r->RIGHT);
printf("%d\t",r->INFO);
int main()
while(1)
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&element);
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
exit(1);
break;
default:
break;
return 0;
OUTPUT:
Select an option
55
Select an option
35
Select an option
65
Select an option
25
Select an option
75
Select an option
25 35 55 65 75 Select an option
55 35 25 65 75 Select an option
25 35 75 65 55 Select an option
Experiment -8:
a) Searching an element on a given list of integers using the Binary Search method.
Program:
Algorithm:
Step 1: Start
Step 2: Initialize
low = 1 high = n
Step 3: Perform Search While(low <= high)
Step 4: Obtain index of midpoint of interval Middle = (low + high) / 2
Step 5: Compare
if(X < K[middle]) high = middle - 1 else
print “Element found at position” Return(middle)
goto: step 2
Step 6: Unsuccessful Search
print “Element found at position” Return (middle)
Step 7: Stop
#include<stdio.h>
int main()
{
int a[10], i, n, key, low, high, mid;
printf("enter the array size \n");
scanf("%d",&n);
low = 0; high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf("The key element is found at location %d \n",mid + 1);
else
printf("the key element is not found \n");
}
Output:
Program:
#include <stdio.h>
if (n == 1)
return;
int main()
return 0;
OUTPUT:
Experiment -9:
Write a program to traverse a graph using BFS method. Write a program to check
whether given graph is connected or not using DFS method.
BFS Algorithm
▪ Unlike the BFS traversal method, which visits the graph nodes level by level, the DFS
method visits the graph nodes along the different paths.
▪ It begins analyzing the nodes from the start to the end node and then proceeds along
the next path from the start node.
▪ This process is repeated until all the graph nodes are visited.
▪ The DFS method also requires frequent backtracking to the already analyzed nodes.
▪ It uses the stack data structure for storing information related to the previous nodes.
▪ The DFS traversal sequence for this graph will be: v1, v2, v4, v8, v5, v7, v3, v6.
▪ Another DFS traversal sequence can be: v1, v3, v6, v7, v8, v2, v5, v4.
Experiment -10:
Design and develop a program in C that uses Hash Function H:K->L as H(K)=K mod
m(reminder method) and implement hashing technique to map a given key K to the address
space L. Resolve the collision (if any) using linear probing.
Given a File of N employee records with a set K of Keys(4-digit) which uniquely determine
the records in file F.
Design and develop a Program in C that uses Hash function H: K -> L as H(K)=K mod
m (remainder method), and implement hashing technique to map a given key K to the address
space L.
Program:
#include<stdio.h>
#include<stdlib.h>
int key[20],n,m;
int *ht,index;
int count = 0;
index = key % m;
while(ht[index] != -1)
index = (index+1)%m;
ht[index] = key;
count++;
void display()
int i;
if(count == 0)
return;
void main()
int i;
scanf("%d", &n);
printf("\nEnter the two digit memory locations (m) for hash table: ");
scanf("%d", &m);
ht = (int *)malloc(m*sizeof(int));
ht[i] = -1;
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
scanf("%d", &key[i]);
for(i=0;i<n;i++)
if(count == m)
break;
insert(key[i]);
display();
OUTPUT:
Enter the two digit memory locations (m) for hash table: 10
Enter the four digit key values (K) for N Employee Records:
1240
1890
1211
1289
T[3] --> -1
T[4] --> -1
T[5] --> -1
T[6] --> -1
T[7] --> -1
T[8] --> -1