Data Structure LAB Program
Data Structure LAB Program
Pgm 1
Design, Develop and Implement a menu-driven Program in C for the following Array
operations
1. Creating an Array of N Integer Elements
2. Display of Array Elements with Suitable Headings
3. Inserting an Element (ELEM) at a given valid Position (POS)
4. Deleting an Element at a given valid Position(POS)
5. Exit.
Support the program with functions for each of the above operations.
#include<stdio.h>
#include<stdlib.h>
int a[10],n,elem,i,pos;
void create();
void display();
void insert();
void delete();
void create()
int i;
scanf("%d",&n);
scanf("%d",&a[i]);
void display()
int i;
printf("The array elements are:\n");
printf("%d\t",a[i]);
void insert()
int i;
scanf("%d",&pos);
scanf("%d",&elem);
a[i+1]=a[i];
a[pos]=elem;
n=n+1;
void delete()
scanf("%d",&pos);
elem=a[pos];
a[i]=a[i+1];
n=n-1;
int main()
int ch;
while(ch)
{
printf("\n\n______MENU_____\n");
printf("1.Create\n2.Display\n3.Insert\n4.Delete\n5.Exit\n");
scanf("%d",&ch);
switch(ch)
case 1:create();break;
case 2:display();break;
case 3:insert();break;
case 4:delete();break;
case 5:exit(0);break;
}return 0;
Pgm 2
Design, Develop and Implement a Program in C for the following operations on Strings
1. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
2. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT
in STR with REP if PAT exists in STR. Report suitable messages in case PAT
does not exist in STR
Support the program with functions for each of the above operations. Don’t use
Built-in functions.
#include<stdio.h>
void main()
char s[20],pat[20],rep[20],ans[30];
int i,j,k,l,flag;
printf("\nEnter string:");
scanf("%s",s);
printf("\nEnter pattern:");
scanf("%s",pat);
printf("\nEnter replacement:");
scanf("%s",rep);
for(i=0,k=0;s[i]!='\0';i++)
flag=1;
for(j=0;pat[j]!='\0';j++)
if(s[i+j]!=pat[j])
flag=0;
l=j;
if(flag)
for(j=0;rep[j]!='\0';j++,k++)
ans[k]=rep[j];
i+=l-1; }
else
ans[k++]=s[i];
ans[k]='\0';
printf("%s",ans);
Pgm 3
Design, Develop and Implement a menu driven Program in C for the following
operations on STACK of Integers (Array Implementation of Stack with maximum
size MAX).
1. Push an Element on to Stack
2. Pop an Element from Stack
3. Demonstrate how Stack can be used to check Palindrome
4. Demonstrate Overflow and Underflow situations on Stack
5. Display the status of Stack
6. Exit
Support the program with appropriate functions for each of the above operations
#include <stdio.h>
#include <stdlib.h>
int s[5],top=-1;
void push()
if(top==4)
printf("\nStack overflow!!!!");
else
scanf("%d",&s[++top]);
void pop()
if(top==-1)
printf("\nStack underflow!!!");
else
void disp()
int t=top;
if(t==-1)
printf("\nStack empty!!");
else
while(t>=0)
printf("%d ",s[t--]);
void pali()
{
int num[5],rev[5],i,t;
for(i=0,t=top;t>=0;i++,t--)
num[i]=rev[t]=s[t];
for(i=0;i<=top;i++)
if(num[i]!=rev[i])
break;
for(t=0;t<=top;t++)
printf("%4d %4d\n",num[t],rev[t]);*///remove /* */ to
display num and rev
if(i==top+1)
printf("\nIt is a palindrome");
else
int main()
int ch;
do
printf("\n...Stack operations.....\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.Palindrome\n");
printf("4.Display\n");
printf("5.Exit\n________________\n");
printf("Enter choice:");
scanf("%d",&ch);
switch(ch)
case 1:push();break;
case 2:pop();break;
case 3:pali();break;
case 4:disp();break;
case 5:exit(0);
default:printf("\nInvalid choice");
while(1);
return 0;
Pgm 4
Design, Develop and Implement a Program in C for converting an Infix Expression
to Postfix Expression. Program should support for both parenthesizedand free
parenthesized expressions with the operators: +, -, *, /, %(Remainder), ^(Power)
and alphanumeric operands.
#include<stdio.h>
#include<string.h>
switch (symbol)
case '+':
case '-':return 2;
case '*':
case '/':
case '%':return 4;
case '^':
case '$':return 5;
case '(':return 0;
default :return 8;
switch (symbol)
case '+':
case '-':return 1;
case '*':
case '/':
case '%':return 3;
case '^':
case '$':return 6;
case '(':return 3;
case ')':return 0;
default :return 7;
s[++top] = '#';
symbol = infix[i];
{
postfix[j] = s[top--];
j++;
if(F(s[top]) != G(symbol))
s[++top] = symbol;
else
top--;
while(s[top] != '#')
postfix[j++] = s[top--];
postfix[j] = '\0';
void main()
printf ("%s",infix);
printf ("%s",postfix) ;
Pgm 5
Design, Develop and Implement a Program in C for the following Stack
Applications
1. Evaluation of Suffix expression with single-digit operands and operators:+, -,
*, /, %, ^
2. Solving Tower of Hanoi problem with n disks
#include<stdio.h>
#include<math.h>
#include<string.h>
switch (symbol)
case '$':
default : return 0;
void main()
int top, i;
top=-1;
symbol = postfix[i];
if(isdigit(symbol))
s[++top]=symbol - '0';
else
{
op2 = s[top--];
op1 = s[top--];
s[++top] = res;
res = s[top--];
Pgm 6
Design, Develop and Implement a menu driven Program in C for the following
operations on Circular QUEUE of Characters (Array Implementation of Queue with
maximum size MAX)
1. Insert an Element on to Circular QUEUE
2. Delete an Element from Circular QUEUE
3. Demonstrate Overflow and Underflow situations on Circular QUEUE
4. Display the status of Circular QUEUE
5. Exit
Support the program with appropriate functions for each of the above operations
#include <stdio.h>
#include <stdlib.h>
#define max 5
int q[max],f=-1,r=-1;
void ins()
if(f==(r+1)%max)
printf("\nQueue overflow");
else
if(f==-1)
f++;
r=(r+1)%max;
void del()
if(r==-1)
printf("\nQueue underflow");
else
if(f==r)
f=r=-1;
else
f=(f+1)%max;
void disp()
if(f==-1)
printf("\nQueue empty");
else
int i;
for(i=f;i!=r;i=(i+1)%max)
printf("%d\t",q[i]);
printf("%d",q[i]);
int main()
printf("\n2.Delete");
printf("\n3.Display");
printf("\n4.Exit");
int ch;
do{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
case 1:ins();break;
case 2:del();break;
case 3:disp();break;
case 4:exit(0);
default:printf("\nInvalid choice...!");
}while(1);
return 0;
Pgm 7
Design, Develop and Implement a menu driven Program in C for the following
operations on Singly Linked List (SLL) of Student Data with the fields: USN,
Name, Branch, Sem, PhNo
1. Create a SLL of N Students Data by using front insertion.
2. Display the status of SLL and count the number of nodes in it
3. Perform Insertion / Deletion at End of SLL
4. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
5. Exit
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
struct stud
{
char usn[11],name[15],branch[4],phno[11];
int sem;
}*f=NULL,*r=NULL,*t=NULL;
printf("\nEnter USN:");
scanf("%s",t->usn);
printf("Enter Name:");
scanf("%s",t->name);
printf("Enter Branch:");
scanf("%s",t->branch);
printf("Enter Sem:");
scanf("%d",&t->sem);
printf("Enter Phno:");
scanf("%s",t->phno);
t->next=NULL;
if(!r)
f=r=t;
else
if(ch)
r->next=t;
r=t;
else
t->next=f;
f=t;
}
}
if(!f)
printf("\nList Empty");
else
if(f==r)
t1=f;
f=r=NULL;
else if(ch)
t1=r;
for(t=f;t->next!=r;t=t->next)
r=t;
r->next=NULL;
else
t1=f;
f=f->next;
printf("USN:%s\nName:%s\nBranch:%s\nSem:%d\nPhno:%s\
n",t1->usn,t1->name,t1->branch,t1->sem,t1->phno);
free(t1);
void disp()
{
if(!f)
printf("\nList Empty!!!");
else
for(t=f;t;t=t->next)
printf("\nUSN:%s\nName:%s\nBranch:%s\nSem:%d\nPhno:%s\
n",t->usn,t->name,t->branch,t->sem,t->phno);
void main()
int ch,n,i;
printf("\n........Menu..........,\n");
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert at end\n");
printf("4.Delete at end\n");
printf("5.Insert at beg\n");
printf("6.Delete at beg\n");
printf("7.Exit\n");
while(1)
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
for(i=0;i<n;i++)
ins(0);
break;
case 2:disp();break;
case 3:ins(1);break;
case 4:del(1);break;
case 5:ins(0);break;
case 6:del(0);break;
case 7:exit(0);
default:printf("\nInvalid choice!!!!");
Pgm 8
Design, Develop and Implement a menu driven Program in C for the following operations
on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept,
Designation, Sal, PhNo
1. Create a DLL of N Employees Data by using end insertion.
2. Display the status of DLL and count the number of nodes in it
3. Perform Insertion and Deletion at End of DLL
4. Perform Insertion and Deletion at Front of DLL
5. Demonstrate how this DLL can be used as Double Ended Queue
6. Exit
#include<string.h>
int count=0;
struct node
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
}*h,*temp,*temp1,*temp2,*temp4;
void create()
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
temp->prev = NULL;
temp->next = NULL;
temp->ssn = ssn;
strcpy(temp->name,name);
strcpy(temp->dept,dept);
strcpy(temp->desg,desg);
temp->sal = sal;
temp->phno = phno;
count++;
void insertbeg()
if (h == NULL)
create();
h = temp;
temp1 = h;
else
create();
temp->next = h;
h->prev = temp;
h = temp;
}
void insertend()
if(h==NULL)
create();
h = temp;
temp1 = h;
else
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
void displaybeg()
temp2 =h;
if(temp2 == NULL)
{
return;
temp2->desg,temp2->sal, temp2->phno );
temp2 = temp2->next;
}
printf(" No of employees = %d ", count);
int deleteend()
temp=h;
if(temp->next==NULL)
free(temp);
h=NULL;
return 0;
else
temp2=temp1->prev;
temp2->next=NULL;
temp1->desg,temp1->sal, temp1->phno );
free(temp1);
}
count--;
return 0;
int deletebeg()
temp=h;
if(temp->next==NULL)
free(temp);
h=NULL;
}
else
h=h->next;
temp->desg,temp->sal, temp->phno );
free(temp);
count--;
return 0;
void main()
int ch,n,i;
h=NULL;
printf("-----------------MENU--------------------\n");
printf("\n 7 - exit\n");
printf("------------------------------------------\n");
while (1)
scanf("%d", &ch);
switch (ch)
case 1:
printf("\n Enter no of employees : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insertend();
break;
case 2:
displaybeg();
break;
case 3:
insertend();
break;
case 4:
deleteend();
break;
case 5:
insertbeg();
break;
case 6:
deletebeg();
break;
case 7:
exit(0);
default:
printf("wrong choice\n");
Pgm 9
Design, Develop and Implement a Program in C for the following operations on Singly
Circular Linked List (SCLL) with header nodes
1. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
2. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result
in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<math.h>
int expo,coef;
}node;
/*FUNCTION PROTOTYPE*/
node *p,*q;
p=(node *)malloc(sizeof(node));
p->expo=expo1;
p->coef=coef1;
p->next=NULL;
if(head==NULL)
head=p;
head->next=head;
return(head);
if(expo1>head->expo)
{
p->next=head->next;
head->next=p;
head=p;
return(head);
if(expo1==head->expo)
head->coef=head->coef+coef1;
return(head);
q=head;
while(q->next!=head&&expo1>=q->next->expo)
q=q->next;
if(p->expo==q->expo)
q->coef=q->coef+coef1;
else
p->next=q->next;
q->next=p;
}
return(head);
node *create()
int n,i,expo1,coef1;
node *head=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d",&coef1,&expo1);
head=insert(head,expo1,coef1);
return(head);
node *p;
node *head=NULL;
printf("\n\n\nAddition of polynomial==>");
p=p1->next;
do
head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p1->next);
p=p2->next;
do
head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p2->next);
return(head);
node *p;
int x,ans=0;
scanf("%d",&x);
p=head->next;
do
{
ans=ans+p->coef*pow(x,p->expo);
p=p->next;
}while(p!=head->next);
return(ans);
node *p,*q;
int n=0;
q=head->next;
p=head->next;
do
n++;
q=q->next;
}while(q!=head->next);
do
{
if(n-1)
printf("%dx^(%d) + ",p->coef,p->expo);
p=p->next;
else
printf(" %dx^(%d)",p->coef,p->expo);
p=p->next;
n--;
} while(p!=head->next);
}
void main()
int a,x,ch;
node *p1,*p2,*p3;
p1=p2=p3=NULL;
while(1)
printf(" 1.Add");
printf("\n\t\t\t\t2.Evaluate");
printf("\n\t\t\t\t3.Exit");
printf("\n\t------------------------------------------- ");
scanf("%d",&ch);
switch(ch)
case 1 :
p1=create();
display(p1);
p2=create();
display(p2);
p3=add(p1,p2);
display(p3);
break;
case 2 :
p1=create();
display(p1);
a=eval(p1);
printf("\n\nValue of polynomial=%d",a);
break;
case 3 :
exit(0);
break;
default :
break;
Pgm 10
Design, Develop and Implement a menu driven Program in C for the following operations
on Binary Search Tree(BST) of Integers
1. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
2. Traverse the BST in Inorder, Preorder and Post Order
3. Search the BST for a given element (KEY) and report the appropriate message
4. Exit
#include <stdio.h>
#include <stdlib.h>
int flag=0;
int data;
} node;
/*FUNCTION PROTOTYPE*/
void main()
int choice;
int key;
node *get_node();
root = NULL;
do
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
scanf("%d", &choice);
switch (choice)
{
case 1:
do
new_node = get_node();
scanf("%d", &new_node->data);
root = new_node;
else
insert(root, new_node);
} while (ans);
break;
case 2:
scanf("%d", &key);
if(flag==1)
else
flag=0;
break;
case 3:
if (root == NULL)
inorder(root);
preorder(root);
postorder(root);
break;
}
while (choice != 4);
node *get_node()
node *temp;
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
if(root->lchild==NULL)
root->lchild=new_node;
else
insert(root->lchild, new_node);
}
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
node *temp;
temp = root;
if (temp->data == key)
flag=1;
return temp;
*parent = temp;
temp = temp->lchild;
else
temp = temp->rchild;
return NULL;
if (temp != NULL)
inorder(temp->lchild);
printf("%d\t", temp->data);
inorder(temp->rchild);
{
if (temp != NULL)
printf("%d\t", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
if (temp != NULL)
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d\t", temp->data);
Pgm 11
Design, Develop and Implement a Program in C for the following operations on
Graph(G) of Cities
1. Create a Graph of N cities using Adjacency Matrix.
2. Print all the nodes reachable from a given starting node in a digraph using
DFS/BFS method
#include <stdio.h>
#include <stdlib.h>
int a[20][20],q[20],visited[20],reach[10],n,i,j,f=0,r= -
1,count=0;
void bfs(int v)
{
for(i=1;i<=n;i++)
q[++r]=i;
if(f<=r)
visited[q[f]]=1;
bfs(q[f++]);
void dfs(int v)
int i;
reach[v]=1;
for(i=1;i<=n;i++)
printf("\n %d->%d",v,i);
count++;
dfs(i);
void main()
int v, choice;
scanf("%d",&n);
for(i=1;i<=n;i++)
q[i]=0;
visited[i]=0;
for(i=1;i<=n-1;i++)
reach[i]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&v);
bfs(v);
if((v<1)||(v>n))
}
else
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
break;
case 2:
dfs(1);
if(count==n-1)
break;
case 3:
exit(0);
Pgm 12
Given a File of N employee records with a set K of Keys(4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table(HT) of
m memory locations with L as the set of memory addresses (2-digit) of locations in HT.
Let the keys in K and addresses in L are Integers. 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. Resolve the collision
(if any) using linear probing.
#include <stdio.h>
#include <stdlib.h>
/*FUNCTION PROTOTYPE */
int create(int);
void main()
int a[MAX],num,key,i;
int ans=1;
for (i=0;i<MAX;i++)
a[i] = -1;
do
{
scanf("%4d", &num);
key=create(num);
linear_prob(a,key,num);
scanf("%d",&ans);
}while(ans);
display(a);
int key;
key=num%100;
return key;
flag=0;
if(a[key]== -1)
a[key] = num;
else
printf("\nCollision Detected...!!!\n");
i=0;
while(i<MAX)
if (a[i]!=-1)
count++;
i++;
if(count == MAX)
display(a);
exit(1);
if(a[i] == -1)
a[i] = num;
flag =1;
break;
//for(i=0;i<key;i++)
i=0;
{
if(a[i] == -1)
a[i] = num;
flag=1;
break;
i++;
int i,choice;
scanf("%d",&choice);
if(choice==1)
else
if(a[i]!=-1)
continue;
}
}