0% found this document useful (0 votes)
68 views41 pages

DSA Lab Progs2016

Uploaded by

Rules hack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views41 pages

DSA Lab Progs2016

Uploaded by

Rules hack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

1.

Design, Develop and Implement a menu driven Program in C for the


following Array operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Inserting an Element (ELEM) at a given valid Position (POS)
d. Deleting an Element at a given valid Position(POS)
e. Exit.
Support the program with functions for each of the above operations.

#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;

printf("enter the element to be inserted");


scanf("%d",&key);
printf("“enter which position to be inserted");
scanf("%d",&pos);
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;

printf("enter which position element to be deleted");


scanf("%d",&pos);
for(i=pos;i<n;i++)
a[i-1]=a[i];
n--;

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

2. Design, Develop and Implement a Program in C for the following


operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace String
(REP)
b. 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>
#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()
{

int i = 0,m = 0,c = 0,j = 0,k;


while ( str[c] != '\0')
{
if ( str[m] == PAT[i] ) // ...... matching
{
i++;
m++;
if ( PAT[i] == '\0') //.....found occ
{
//.... copy replace string in ans string .....
for(k=0; REP[k] != '\0';k++,j++)
ans[j] = REP[k];
i=0;
c=m;
}
}
else //... mismatch
{
ans[j] = str[c];
j++;
c++;
m = c;
i=0;
}
}//while
ans[j] = '\0';
printf("\nThe resultant string is\n%s" ,ans);

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

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)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above
operations.

#include<stdio.h>
#include<ctype.h>
#define max 5

int top=-1,j=-1, a[10],b[10];

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);

default: printf("wrong entry");


}
}
}

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

4. Design, Develop and Implement a Program in C for converting an Infix


expression to Postfix expression. Program should support for both
parenthesized and free parenthesized expressions with the operators: +, -, *,
/, %(Remainder), ^(Power) and alphanumeric operands.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20

char stack[MAX];
int top=-1;

void push(char item)


{
stack[++top]=item;
}

char pop()
{
return stack[top--];

int prcd(char symbol)


{
switch(symbol)
{
case '^': return 4;
case '*': case '/': return 3;
case '+': case '-': return 2;
case '(': case ')': case '#': return 1;
}
}

void convertip(char infix[],char postfix[])


{
int i,symbol,j=0;
push('#');

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:

Enter valid infix expression:


(a+b)*c/(d*e)
The corresponding postfix expression is:
ab+cde*/*

5. Design, Develop and Implement a Program in C for the following Stack


Applications
a. Evaluation of Suffix expression with single digit operands and
operators: +, -, *, /, %, ^

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>

float stack[10];
int top=-1;

void push(float num)


{
stack[++top]=num;
}

float pop()
{
return(stack[top--]);
}

float eval(char postfix[])


{
int i;
float op1,op2;
char symb;
for(i=0;i<strlen(postfix);i++)
{
symb=postfix[i];
if (isdigit(symb)) // '1' ascii val is 49 & '0' is 48. 49-48=1
push(symb-'0');
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();

printf("Enter a valid postfix expression\t");


gets(postfix);
printf("\nThe result of postfix expression %s is = %f",postfix,eval(postfix));
getch();
}
OUTPUT:

Enter a valid postfix expression


234+*
The result of postfix expression 234+* is = 14

b. Solving Tower of Hanoi problem with n disks

#include<stdio.h>
#include<conio.h>

void TOH(int n,char x,char y,char z)


{
if(n>0)
{
TOH(n-1,x,z,y); // Recursive call 1
printf("\nMove disk %d from %c -> %c",n,x,y);
TOH(n-1,z,y,x); // Recursive call 2
}
}

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;
};

typedef struct sll node;

node *start = NULL;

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

Enter the choice


1
Enter the n value
2
Enter record 1
Enter USN
123411
Enter the NAME
Raman
Enter the BRANCH
CSE
Enter the SEMESTER
3rd
Enter the PH NUM
234849
Enter record 2
Enter USN
14414
Enter the NAME
Somanath
Enter the BRANCH
ISE
Enter the SEMESTER
4th
Enter the PH NUM
2348499120

The contents of Sll are


14414 Somanath ISE 4th 2348499120
12341 Raman CSE 3rd 2348491111

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 *start = NULL;

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 the choice


2
Enter SSN
111
Enter the NAME
Sai Shankar
Enter the DESIGNATION
Senior Manager
Enter the SALARY
30000
Enter the PH NUM
2348492222
The contents of DLL are
111 Sai Shankar Senior Manager 30000 2348492222

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

9. Design, Develop and Implement a Program in C for the following operations


on Singly Circular Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z -
4yz5+3x3yz+2xy5z-2xyz3
b. 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<conio.h>
#include<math.h>
struct poly
{
int coef, expo1,expo2,expo3;
struct poly *next;
};
typedef struct poly node;

node* insert_end(node *h, int a, int x, int y, int z)


{
node *temp = h->next, *new1;
new1 = (node*) malloc(sizeof(node));
new1->coef = a;
new1->expo1 = x;
new1->expo2 = y;
new1->expo3 = z;
while(temp->next != h)
temp = temp -> next;
temp -> next = new1;
new1 -> next = h;
return h;
}

node* read_poly(node *head)


{
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;
}

node* add_poly(node *h1,node *h2,node *h3)


{
node *p1=h1->next, *p2=h2->next;
int x;

while( p1 != h1 && p2 != h2)


{
if( p1->expo1 > p2->expo1 && p1->expo2 > p2->expo2 && p1->expo3 > p2-
>expo3)
{
h3 = insert_end(h3,p1->coef,p1->expo1, p1->expo2, p1->expo3);
p1 = p1->next;
}
else if(p1->expo1 < p2->expo1 && p1->expo2 < p2->expo2 && p1->expo3 < p2-
>expo3)
{
h3 = insert_end(h3,p2->coef,p2->expo1, p2->expo2, p2->expo3);
p2 = p2->next;
}
else
{
x = p1->coef + p2->coef;
h3 = insert_end(h3,x,p1->expo1,p1->expo2,p1->expo3);
p1 = p1->next;
p2 = p2->next;
}
}
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;
}

void display(node *h)


{
node *temp = h->next;
if(temp == h)
{
printf("list empty\n");
return;
}
while(temp != h)
{
printf("%+dx^%dy^%dz^%d ",temp->coef,temp->expo1,temp->expo2,temp-
>expo3);
temp=temp->next;
}
}

void evaluate(node *h)


{
int x,y,z,sum=0;
node *temp = h->next;

printf("\nEvauate the resultant polynomial by giving values for X, Y and Z");


scanf("%d%d%d",&x,&y,&z);
while(temp != h)
{
sum = sum + temp->coef * pow(x,temp->expo1) * pow(y,temp->expo2) *
pow(z,temp->expo3);
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
Do you want to continue Y/N? y
3311
Do you want to continue Y/N? n

Enter second polynomial


Enter coeffi, exo1, expo2 and expo3
2221
Do you want to continue Y/N? y
-2 0 1 5
Do you want to continue Y/N? y
1151
Do you want to continue Y/N? n

THE FIRST POLY IS:


4x2y2z-2yz5+3x3yz

THE SEC POLY IS:


2x2y2z-2yz5+xy5z

ADDITION OF TWO POLY IS:


6x2y2z-4yz5+3x3yz+xy5z

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));

printf("enter the info");


scanf("%d",&new1->info);
new1->lptr = new1->rptr = NULL;

if( root == NULL)


{
root = new1;
return root;
}

while(cur != NULL)
{
prev = cur;
if( new1->info >= cur->info)
cur = cur->rptr;
else
cur = cur->lptr;
}

if( new1->info > prev->info)


prev->rptr=new1;
else
prev->lptr=new1;

return root;
}

void inorder(node *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);
}
}

void postorder(node *root)


{

if ( root != NULL)
{
postorder (root->lptr);
postorder (root->rptr);
printf(" %d ",root->info);
}
}

void search_key(node *root, int key)


{
if(root != NULL)
{
search_key(root->lptr, key);
if(root->info == key) {success=1;return;}
search_key(root->rptr, key);
}

node* deletenode(node* root, int key)


{

node *temp, *parent,*cur,*succ;

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:

Enter the number of nodes


4
enter the adjacency matrix
0110
1001
1001
0110
Graph Traversal options
1. BFS 2.DFS
Enetr your choice
1
Enter the starting node
1
The nodes reachable from 1 are
1234
Graph Traversal options are
1. BFS 2.DFS
Enter your choice
2
Graph connected

12. Given a File of N employee records with a set K of Keys(4-digit) which


uniquely determine the records in the 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>
#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);
}

int create(int num)


{
int key;
key=num%100;
return key;
}

void linear_prob(int a[MAX], int key, int num)


{
int flag, i, count=0;
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++;
}
printf("Collision avoided successfully using LINEAR PROBING\n");
if(count == MAX)
{
printf("\n Hash table is full");
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++;
}
}
}

void display(int a[MAX])


{
int i,choice;
printf("1.Display ALL\n 2.Filtered Display\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
printf("%d\t %d\n ", i, a[i]);
break;
case 2: printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
{
if(a[i]!=-1)
printf("%d \t %d\n ", i, a[i]);
}
break;
default:printf("invalid choice\n");
}
}

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

You might also like