Dsa - Lab Manual-18csl38
Dsa - Lab Manual-18csl38
#include<stdio.h>
#include<conio.h>
int a[15],n;
void Create_Array()
{
int i;
printf("Enter how many elements ?");
scanf("%d",&n);
printf(" Enter %d elements in an array”",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void Disp_Array()
{
int i;
printf(" Array elements are \n");
for(i=0;i<n;i++)
printf(" %d \t", a[i]);
}
void Insert_Ele_In_Pos()
{
int pos,key,i;
for(i=n-1;i>=pos-1;i--)
a[i+1]=a[i];
a[i+1]=key;
n++;
void Del_Ele_In_Pos()
{
int pos,i;
main()
{
int ch;
for(;;)
{
printf("\n 1. Create Array \n 2. Display Array \n 3. Insert Elements in a valid”);
printf(“position \n4. Delete an element at a given valid position \n 5. exit");
printf("\n enter ur ch");
scanf("%d",&ch);
switch(ch)
{
case 1: Create_Array();
break;
case 2: Disp_Array();
break;
case 3: Insert_Ele_In_Pos();
break;
case 4: Del_Ele_In_Pos();
break;
case 5: exit (0);
default: printf(“\n wrong choice”);
}
}
getch();
}
Output :
Enter how many elements ?
5
Enter 5 elements in an array
3 5 1 6 7
Enter the element to be inserted
10
Enter which position to be inserted
3
Array elements are:
3 5 10 1 6 7
Enter the position of an element to be deleted
2
Array elements are:
3 10 1 6 7
#include<stdio.h>
#include<conio.h>
#include<string.h>
char str[20],PAT[10],REP[10],ans[20];
void Read_String()
{
Department of CSE,CITech Page 3
DATA STRUCTURE LABORATORY-18CSL38
void Rep_Pat()
{
main()
{
int ch;
for(;;)
{
printf("\n 1. Read String \n 2. Display string \n 3. exit");
printf("\n enter ur ch");
scanf("%d",&ch);
switch(ch)
{
case 1: Read_String();
break;
case 2: Rep_Pat();
break;
case 3: exit (0);
}
}
// getch();
}
Output:
Enter main string
Om namaha Shivaya
Enter pattern string
namaha
Enter replace string
Sada
The resultant string is
Om Sada Shivaya
#include<stdio.h>
#include<ctype.h>
#define max 5
void push()
{
int data;
if(top==max-1)
{
printf("\nStack Overflow");
return;
}
printf("\nEnter the element to be inserted: ");
scanf("%d",&data);
b[++j]=a[++top]=data;
}
void pop()
{
if(top==-1)
{
printf("\nStack Underflow");
return;
}
printf("\n data %d is deleted ",a[top]);
b[j--]=a[top--];
void Palindrome()
{
int flag=0,i;
for(i=0;i<=top;i++)
{
if(a[i] == b[j--])
flag = 1;
}
if (flag == 1)
{
printf("\n Entered nums in stack is palindrome\n");
j=top;
}
else
{
printf("\n Entered nums in stack is not a palindrome\n");
j=top;
}
}
void Display()
{
int i;
if(top == -1)
{
printf("\nStack is empty\n");
return;
}
printf("\n Elements of the stack are :\n ");
for(i=top;i>=0;i--)
printf("\t%d",a[i]);
}
void main()
{
int i, item, ch;
clrscr();
while(1)
{
printf("\n1.Push \t 2.Pop \t 3.Palindrome \t 4.Display \t 5.Exit \n");
printf(" Ener the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:Palindrome();
break;
case 4:Display();
break;
case 5:exit(0);
Output:
Push Operation:
1 Stack elements
2 1 -do-
3 2 1 -do-
Pop Operations:
2 1
Palindrome
If stack elements are : 1 2 1 then it is palindrome
Suppose if elements of stack are 2 1 then it is not palindrome
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
char stack[MAX];
int top=-1;
char pop()
{
return stack[top--];
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
switch(symbol)
{
case '(' : push(symbol); break;
case ')' : while(stack[top]!='(')
{
postfix[j]=pop();
j++;
}
pop();
break;
case '-':case '*':case '+':case '/':case '^':
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
break;
default: postfix[j++] = symbol;
}
}
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0'; //null terminate string.
}
void main()
{
char infix[20],postfix[20];
clrscr();
printf("Enter the valid infix expression:\n");
gets(infix);
convertip(infix,postfix);
printf("The corresponding postfix expression is:\n");
puts(postfix);
getch();
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
float stack[10];
int top=-1;
float pop()
{
return(stack[top--]);
}
else
{
op2=pop();
op1=pop();
switch(symb)
{
case '+' : push(op1+op2);
break;
case '-' : push(op1-op2);
break;
case '*' : push(op1*op2);
break;
case '/' : push(op1/op2);
break;
}
}
}
return pop();
}
void main()
{
float val;
char postfix[20];
clrscr();
OUTPUT:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\nEnter number of plates:");
scanf("%d",&n);
TOH(n,'A','B','C');
getch();
}
Output:
Enter num of plates
3
Move disk 1 from A B
Move disk 2 from A C
Move disk 1 from B C
Move disk 3 from A B
Move disk 1 from C A
Move disk 2 from C B
Move disk 1 from A B
#include<stdio.h>
#include<conio.h>
#define MAX 4
int CQ[MAX], n;
int r = -1;
int f = 0,ct=0;
void addq()
{
int item;
if (ct == n )
{
printf("Queue Overflow\n");
return;
}
printf("\nenter the element for adding in queue : ");
r = (r+1)%n;
scanf("%d", &CQ[r]);
ct++;
}
void deleteq()
{
if (ct == 0)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n", CQ[f]);
f=(f+1)%n;
ct--;
}
void display()
{
int i,k=f;
if (ct == 0)
{
printf("Queue is empty\n");
return;
}
printf("contents of Queue are :\n");
for (i = 0; i < ct; i++)
{
printf("%d\t", CQ[k]);
k=(k+1)%n;
}
}
main()
{
int choice; clrscr();
printf("enetr the size of the array\n");
scanf("%d",&n);
while (1)
{
printf("\n1.Insert\n 2.Delete\n 3.Display\n 4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1: addq();
break;
case 2: deleteq();
break;
case 3: display();
break;
case 4: exit(1);
default:
printf("Wrong choice\n");
}
}
}
Output:
Enter the element in a queue:
2 3 5 1
Delete:
Element deleted from queue is 2
3 5 1
Delete:
Element deleted from queue is 3
5 1
Insert:
10 5 1
Insert:
10 20 5 1
No insertion possible because Queue is full
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct sll
{
int usn;
char name[10];
char branch[10];
char sem[3];
long int phno;
struct sll *rptr;
};
node *getnode()
{
node *new1;
new1=(node*)malloc(sizeof(node));
printf("Enter the USN\n");
scanf("%d",&new1->usn);
printf("Enter the NAME\n");
scanf("%s",new1->name); flushall();
printf("Enter the BRANCH\n");
scanf("%s",new1->branch); flushall();
printf("Enter the SEMESTER\n");
scanf("%s",new1->sem); flushall();
printf("Enter the PH NUM\n");
scanf("%d",&new1->phno);
new1->rptr=NULL;
return new1;
void ins_beg()
{
node *new1;
new1=getnode();
if(start == NULL)
{
start = new1;
return;
}
new1->rptr=start;
start=new1;
}
void ins_end()
{
node *temp=start,*new1;
if(start == NULL)
{
printf("Insertion is not possible\n");
return;
}
while(temp->rptr!=NULL)
temp=temp->rptr;
new1 = getnode();
temp->rptr = new1;
void del_beg()
{
node *temp=start;
if(start == NULL)
{
printf("deletion not possible");
return;
}
printf("The record named %s is deleted ",temp->name);
start = temp->rptr;
free(temp);
return;
}
void del_end()
{
node *temp=start,*prev;
if(start == NULL)
{
printf("list empty");
return;
}
while(temp->rptr!=NULL)
{
prev=temp;
temp=temp->rptr;
}
prev->rptr = NULL;
printf("The record named %s is deleted ",temp->name);
free(temp);
}
void display()
{
node *temp=start;
int cnt=0;
if(start == NULL)
{
printf("The sll is empty");
return;
}
printf("The contents of sll are \n");
while(temp!=NULL)
{
printf("%d %s %s %s %d\n",temp->usn,temp->name,temp-
>branch,temp->sem,temp->phno);
temp=temp->rptr;
cnt++;
}
printf("num of nodes in the SLL is %d ", cnt);
}
int main()
{
int choice,n,i;
clrscr();
while(1)
{
OUTPUT:
1. Create n students data using front Insertion
2. Insert front
3. Insert end
4. Delete front
5. Delete end
6. Display
7. Exit
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct dll
{
int ssn;
char name[10];
char dept[10];
char desig[10];
int sal;
int phno;
struct dll *lptr,*rptr;
};
node *getnode()
{
node *new1;
new1=(node*)malloc(sizeof(node));
printf("Enter the SSN\n");
scanf("%d",&new1->ssn);
printf("Enter the NAME\n");
scanf("%s",new1->name);
printf("Enter the DEPARTMENT\n");
scanf("%s",new1->dept);
printf("Enter the DESIGNATION\n");
scanf("%s",new1->desig);
printf("Enter the SALARY\n");
scanf("%d",&new1->sal);
printf("Enter the PHONE NUM\n");
scanf("%d",&new1->phno);
new1->lptr=new1->rptr=NULL;
return new1;
void ins_beg()
{
node *new1;
new1=getnode();
if(start == NULL)
{
start = new1;
return;
}
new1->rptr=start;
start->lptr=new1;
start=new1;
}
void ins_end()
{
node *temp=start,*new1;
new1 = getnode();
if(start == NULL)
{
start = new1;
return;
}
while(temp->rptr!=NULL)
temp=temp->rptr;
temp->rptr = new1;
new1->lptr = temp;
void del_beg()
{
node *temp=start;
if(start == NULL)
{
printf("deletion not possible");
return;
}
printf("The record %d is deleted ",temp->ssn);
start = temp->rptr;
start -> lptr = NULL;
free(temp);
return;
}
void del_end()
{
node *temp=start,*prev;
if(start == NULL)
{
printf("list empty");
return;
}
while(temp->rptr!=NULL)
{
prev=temp;
temp=temp->rptr;
}
prev->rptr = NULL;
printf("The record %d is deleted ",temp->ssn);
free(temp);
}
void display()
{
node *temp=start;
int cnt=0;
if(start == NULL)
{
printf("The DLL is empty");
return;
}
int main()
{
int choice,n,i;
clrscr();
while(1)
{
printf("\n 1.Create n students data using Insertion at End \n 2.Insert
front \n3. Insert end \n ");
printf("4.Delete front\n 5.Delete end \n 6. Display \n 7. Exit\n");
printf("Enter choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter how many records?");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter record %d",i);
ins_end();
}
break;
case 2:ins_beg();break;
case 3:ins_end();break;
case 4:del_beg();break;
case 5:del_end();break;
case 6:display();break;
case 7:printf("Exiting ... \n");exit(1);
break;
OUTPUT:
1. Create n students data using front Insertion
2. Insert front
3. Insert end
4. Delete front
5. Delete end
6. Display
7. Exit
Enter choice 3
222 Rama Manager 250001212121233
Enter choice 6
The contents of DLL are
111 Sai Shankar Senior Manager 30000 2348492222
222 Rama Manager 25000 1212121233
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct poly
{
int coef, expo1,expo2,expo3;
struct poly *next;
};
typedef struct poly node;
int a,x,y,z;
char ch;
do
{
printf("\nenter coef & expo1, expo2, expo3\n");
scanf("%d%d%d%d",&a,&x,&y,&z);
head = insert_end(head,a,x,y,z);
printf("do u want to continue(Y/N) ?");
ch=getche();
}while(ch == 'Y' || ch == 'y');
return head;
}
while(p1 != h1)
{
h3 = insert_end(h3, p1->coef, p1->expo1, p1->expo2, p1->expo3);
p1 = p1->next;
}
while(p2 != h2)
{
h3 = insert_end(h3,p2->coef,p2->expo1, p2->expo2, p2->expo3);
p2 = p2->next;
}
return h3;
}
temp = temp->next;
}
printf("\nSum = %d",sum);
main()
{
node *h1,*h2,*h3;
clrscr();
h1 = (node*) malloc(sizeof(node));
h1->next = h1;
h2 = (node*) malloc(sizeof(node));
h2->next = h2;
h3 = (node*) malloc(sizeof(node));
h3->next = h3;
printf("\nenter the first poly");
h1 = read_poly(h1);
printf("\nenter the second poly");
h2 = read_poly(h2);
h3 = add_poly(h1,h2,h3);
printf("\nTHE FIRST POLY IS\n");
display(h1);
printf("\nTHE SEC POLY IS\n");
display(h2);
printf("\nADDition of TWO poly are\n");
display(h3);
evaluate(h3);
getch();
}
OUTPUT:
Enter first polynomial
Enter coeffi, exo1, expo2 and expo3
4 2 2 1 x2y2z
Do you want to continue Y/N? y
-2 0 1 5 yz5
Department of CSE,CITech Page 30
DATA STRUCTURE LABORATORY-18CSL38
10. Design, Develop and Implement a menu driven Program in C for the
following operations on Binary Search Tree (BST) of Integers
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate
message
d. Delete an element(ELEM) from BST
e. Exit
#include<stdio.h>
#include<conio.h>
int success=0;
struct BST
{
int info;
while(cur != NULL)
{
prev = cur;
if( new1->info >= cur->info)
cur = cur->rptr;
else
cur = cur->lptr;
}
return root;
}
if ( root != NULL)
{
inorder (root->lptr);
printf(" %d ",root->info);
inorder (root->rptr);
}
}
if ( root != NULL)
{
printf(" %d ",root->info);
preorder (root->lptr);
preorder (root->rptr);
}
}
if ( root != NULL)
{
postorder (root->lptr);
postorder (root->rptr);
printf(" %d ",root->info);
}
}
if (root == NULL)
{
printf("Tree is empty! Key not found");
return root;
}
parent = NULL;
cur=root;
while( cur != NULL )
{
if ( key == cur->info ) break;
parent = cur;
if (key < cur->info )
cur = cur->lptr;
else
cur = cur->rptr;
}
if (cur == NULL)
{
printf("key not found");
return root;
}
if (cur->lptr == NULL)
temp = cur->rptr;
else if (cur->rptr == NULL)
temp = cur->lptr;
else
{
succ = cur->rptr;
while(succ->lptr != NULL)
succ = succ->lptr;
succ->lptr = cur->lptr;
temp=cur->rptr;
}
if(parent == NULL)
return temp;
if(cur==parent->lptr)
parent->lptr=temp;
else
parent->rptr=temp;
free(cur);
return root;
}
main()
{
node *root = NULL;
int ch, key;
clrscr();
for(;;)
{
printf("\n1.Create BST 2.Inorder 3.Preorder 4.Postorder");
printf(" 5.Delete 6.Search Key 7.exit");
printf("\n enter ur ch");
scanf("%d",&ch);
switch(ch)
{
case 1: root = insert(root);
break;
case 2: if( root == NULL)
printf("tree empty");
else
inorder(root);
break;
case 3: if( root == NULL)
printf("tree empty");
else
preorder(root);
break;
case 4: if( root == NULL)
printf("tree empty");
else
postorder(root);
break;
case 6: if( root == NULL)
printf("tree empty");
else
{
printf("Enter the key value");
scanf("%d", &key);
search_key(root,key);
if(success==1)
printf("key found");
else
printf("key not found");
}
break;
case 5: if( root == NULL)
printf("tree empty");
else
{
printf("Enter key value to be deleted");
scanf("%d",&key);
root=deletenode(root,key);
}
break;
case 7: exit(0);
}
}
getch();
}
OUTPUT:
1.Create tree 2.Inorder 3. Preorder 4. Postorder 5. Delete 6. Search 7. Exit
Enter choice
1
10
Enter choice
23
Enter choice
55
Enter choice 2
10 23 55
Enter choice 5
Enter key value 23
Enter choice 2
10 55
11. Design, Develop and Implement a Program in C for the following operations
on Graph(G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph
using BFS method
c. Check whether a given graph is connected or not using DFS method.
#include<stdio.h>
#include<conio.h>
int a[10][10],visited[10]={0},n;
void bfs(int v)
{
int u,i,j,q[10],f=0,r=-1;
/* for(i=1;i<=n;i++)
visited[i]=0;*/
visited[v]=1;
printf("%d\t",v);
u=v;
while(1)
{
for(j=1;j<=n;j++)
{
if ((a[u][j]==1) && (visited[j]==0))
{ q[++r]=j;
visited[j]=1;
}
}
if (f>r) return;
u=q[f++];
printf("%d\t",u);
}
}
void dfs(int v)
{
int i;
visited[v]=1;
for(i=1;i<=n;i++)
if ((a[v][i]==1)&& (visited[i]==0))
dfs(i);
}
void main()
{
int ch,snode,i,j;
clrscr();
printf("enter the number of nodes\n");
scanf("%d",&n);
printf("enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
while(1)
{
printf("Graph traversal options\n");
printf("1.BFS\t 2. DFS\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter the starting node\n");
scanf("%d",&snode);
printf("The nodes reachable from %d are\n",snode);
bfs(snode);
break;
case 2:printf("enter the starting node\n");
scanf("%d",&snode);
dfs(snode);
for(i=1;i<=n;i++)
if (visited[i]==0)
{ printf("not connected\n");
exit(0);
}
printf("graph connected\n");
break;
default: exit(0);
}
}
getch();
}
Input:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int create(int);
void linear_prob(int[], int, int);
void display (int[]);
void main()
{
int a[MAX],num,key,i;
int ans=1;
printf(" collision handling by linear probing : \n");
for (i=0;i<MAX;i++)
{
a[i] = -1;
}
do
{
printf("\n Enter the data");
scanf("%4d", &num);
key=create(num);
linear_prob(a,key,num);
printf("\n Do you wish to continue ? (1/0) ");
scanf("%d",&ans);
}while(ans);
display(a);
}
display(a);
exit(1);
}
for(i=key+1; i<MAX; i++)
if(a[i] == -1)
{
a[i] = num;
flag =1;
break;
}
i=0;
while((i<key) && (flag==0))
{
if(a[i] == -1)
{
a[i] = num;
flag=1;
break;
}
i++;
}
}
}
OUTPUT:
Enter the data1234
Do you wish to continue ? (1/0) 1
Enter the data2548
Do you wish to continue ? (1/0) 1
Enter the data3256
Do you wish to continue ? (1/0) 1
Enter the data1299
Do you wish to continue ? (1/0) 1
Enter the data1298