DSA Lab Progs2016
DSA Lab Progs2016
#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;
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()
{
printf("Enter main string"); flushall();
gets(str);
printf(" Enter pattern string"); flushall();
gets(PAT);
printf(" Enter replace string"); flushall();
gets(REP);
}
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--]);
}
void main()
{
float val;
char postfix[20];
clrscr();
#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
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)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above
operations
#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
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
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of SLL
d. Perform Insertion and Deletion at Front of SLL
e. Demonstrate how this SLL can be used as STACK and QUEUE
f. Exit
#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)
{
printf("\n 1.Create n students data using front Insertion \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_beg();
}
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;
default : printf("Invalid choice\n");
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
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
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. 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;
};
typedef struct dll node;
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;
}
printf("The contents of DLL are \n");
while(temp!=NULL)
{
printf("%d %s %s %s %d\n",temp->ssn,temp->name,temp-
>desig,temp->sal,temp->phno);
temp=temp->rptr;
cnt++;
}
printf("num of nodes in the DLL is %d ", cnt);
}
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;
default : printf("Invalid choice\n");
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 25000 1212121233
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;
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
Do you want to continue Y/N? y
3311
Do you want to continue Y/N? n
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;
struct BST *lptr,*rptr;
};
typedef struct BST node;
node* insert(node *root)
{
node *new1,*cur = root, *prev=NULL;
new1 = (node*) malloc(sizeof(node));
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);
}
}
void preorder(node *root)
{
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);
}
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
Do you wish to continue ? (1/0) 1
Enter the data1398
Collision Detected...!!!
Collision avoided successfully using LINEAR PROBING
Do you wish to continue ? (1/0) 0
1.Display ALL
2.Filtered Display
2
the hash table is
0 1398
34 1234
48 2548
56 3256
98 1298
99 1299