BCSL305 Updated Lab Manual-1
BCSL305 Updated Lab Manual-1
BCSL305 Updated Lab Manual-1
Course objectives
This laboratory course enables students to get practical experience in design, develop,
implement, analyze and evaluation/testing of
● Dynamic memory management
● Linear data structures and their applications such as stacks, queues and lists
● Non-Linear data structures and their applications such as trees and graphs.
Course Outcomes
After studying this course, students will be able to:
CO mapping to PO/PSOs
CO /
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
PO & PSO
CO 1 2 3 3 2 3 1 3 2
CO2 3 3 3 2 3 2 3 3
CO3 3 3 3 2 3 2 3 3
CO4 3 3 3 3 3 2 2 3
Data Structures Laboratory
Evaluation Rubrics
8 Develop 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
30
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
9 Develop 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
30
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.
10 Develop 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
36
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. Exit
11 Develop a Program in C for the following operations on Graph(G) of Cities:
a. Create a Graph of N cities using Adjacency Matrix.
42
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method.
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. Develop a Program in 45
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.
III Semester BCSL305 – Data Structures Laboratory
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char *day;
int date;
char *activity;
} calendar;
calendar* create()
{
calendar *week;
week=(calendar*)calloc(7,sizeof(calendar));
return week;
}
calendar *week;
while(1)
{
printf("1.Create 2.Read 3.Display 4.Exit choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: week = create();
if(week!=NULL)
printf("Calander is successfully created\n");
break;
case 2: read(week);
break;
case 3: display(week);
break;
case 4: return 0;
default: printf("Invalid choice\n");
}
}
}
Tuesday 12 Assignment
Wednesday 13 Projects
Thursday 14 Coding
Friday 15 Dance
Saturday 16 Music
Sunday 17 Relax
1.Create 2.Read 3.Display 4.Exit choice:4
#include<stdio.h>
char str[100],pat[100],rep[100],ans[100];
void read()
{
printf("enter the string: ");
gets(str);
printf(" \n enter the patter string: ");
flushall();
gets(pat);
printf("\n enter the replacement string: ");
flushall();
gets(rep);
}
void pat_match()
{
int i,j,c,m,k;
int flag=0;
i=m=c=j=0;
while(str[c]!='\0')
{
if(str[m]==pat[i])//Pattern matching
{
i++;
m++;
if(pat[i]=='\0')
{
printf("\n pat:%s is found at position %d",pat,c);
for(k=0;rep[k]!='\0';k++,j++)
ans[j]=rep[k];
i=0;
c=m;
flag=1;
}
}
else//pattern mismatch
{
ans[j]=str[c];
j++;
c++;
m=c;
i=0;
}
}
ans[j]='\0';
if(flag==0)
printf("\n PAT:%s is not found in STR:%s",pat,str);
else
printf("\n The resulting string is: %s",ans);
}
void main()
{
clrscr();
read();
pat_match();
getch();
}
OUTPUT:-
enter the string: hi rns hi
enter the pattern string: hi
enter the replacement string: hello
pat:hi is found at position 0
pat:hi is found at position 7
The resulting string is: hello rns hello
#include<stdio.h>
#include<math.h>
#define max 5
int stack_list[max],top=-1;
void push(int m)
{
if(top==max-1)
printf("\n Stack overflow");
else
{
top++;
stack_list[top]=m;
}
}
int pop()
{
if(top==-1)
{
printf("\n Stack underflow");
return -1;
}
else
return stack_list[top--];
}
void display()
{
int i;
if(top==-1)
printf("\n Stack is empty");
else
{
printf("\n The elements are\n");
for(i=top;i>=0;i--)
printf("%d\n",stack_list[i]);
}
}
void palindrome()
{
int n,num,rem,i;
printf("\n Enter n");
scanf("%d",&n);
top=-1;
num=n;
while(num!=0)
{
rem=num%10;
push(rem);
num=num/10;
}
num=0;
for(i=0;top!=-1;i++)
num=pop()*pow(10,i)+num;
if(n==num)
printf("\n It is a palindrome");
else
printf("\n It is not a palindrome");
}
int main()
{
int c,m;
while(1)
{ printf("\n Enter 1-push\n2-pop\n3-display\n4-palindrome");
scanf("%d",&c);
switch(c)
{
case 1: printf("\n Enter an element\t");
scanf("%d",&m);
push(m);
break;
case 2:m=pop();
if(m!=-1)
printf("\n The popped elementis %d",m);
break;
case 3: display();
break;
case 4: palindrome();
break;
default:return 0;
}
}
}
Stack overflow
Enter your choice
1-push
2-pop
3-display
4-palindrome
3
2-pop
3-display
4-palindrome
2
Stack underflow
Enter n
123
It is not a palindrome
Enter your choice
1-push
2-pop
3-display
4-palindrome
4
Enter n
121
It is a palindrome
Enter your choice
1-push
2-pop
3-display
4-palindrome
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
char stk[100];
int tos = -1;
void push(char opr)
{
stk[++tos] = opr;
}
char pop()
{
return(stk[tos--]);
}
int preced(char opr)
{
if(opr=='^'||opr=='%') return(4);
if(opr=='*'||opr=='/') return(3);
if(opr=='+'||opr=='-') return(2);
if(opr=='('||opr=='#') return(1);
}
void main()
{
char infix[20],postfix[20];
int i,j=0;
printf("\nEnter valid INFIX expression\n");
scanf("%s",infix);
push('#');
for(i=0; infix[i]!='\0'; i++)
{
if(infix[i]=='(')
push('(');
else if(isalnum(infix[i]))
postfix[j++] = infix[i];
else if(infix[i]==')')
{
while(stk[tos] != '(')
postfix[j++] = pop();
pop();
}
else
{
while(preced(stk[tos]) >= preced(infix[i]))
postfix[j++] = pop();
push(infix[i]);
}
}
while(stk[tos] != '#')
postfix[j++] = pop();
postfix[j]='\0';
printf("\n INFIX EXPRESSION = %s",infix);
printf("\n POSTFIX EXPRESSION = %s",postfix);
getch();
}
OUTPUT:-
5 Design, Develop and Implement a program in C for the following stack operations
a. Evaluation of suffix expression with single digit operands and operators:+,-
,*,/,%,^.
b. Solving tower of Hanoi problem with n disks.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int stk[25],tos=-1;
void push(int item)
{
stk[++tos]=item;
}
int pop()
{
return (stk[tos--]);
}
int main()
{
char post[25],sym;
int op1,op2,i;
printf("Enter the postfix expression:\n");
scanf("%s",post);
for(i=0;i<strlen(post);i++)
{
sym=post[i];
switch(sym)
{
case '+':op2=pop();
op1=pop();
push(op1+op2);
break;
case '-':op2=pop();
op1=pop();
push(op1-op2);
break;
case '*':op2=pop();
op1=pop();
push(op1*op2);
break;
case '/':op2=pop();
op1=pop();
push(op1/op2);
break;
default:push(sym-'0');
break;
}
}
printf("The result if %d\n",pop());
}
Output1:
Enter the postfix expression:
6523+8*+3+*
The result if 288
Output2:
Enter the postfix expression:
93*1-2+4*5-
The result if 107
#include<stdio.h>
void tower(int num,char src,char tmp,char dest)
{
if(num==1)
{
printf("Move disk 1 from peg %c to peg %c\n",src,dest);
return;
}
tower(num-1,src,dest,tmp);
printf("Move disk %d from peg %c to peg %c\n",num,src,dest);
tower(num-1,tmp,src,dest);
}
int main()
{
int num;
printf("Enter number of disks\n");
scanf("%d",&num);
tower(num,'A','B','C');
}
Output1:
Enter number of disks
3
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
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<stdlib.h>
#define qmax 5
char q[qmax];
int front=0,rear=-1;
void qinsert();
void qdelete();
void qdisplay();
void main()
{
int ch;
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: qinsert(); break;
case 2: qdelete(); break;
case 3: qdisplay(); break;
case 4: exit(1);
default: printf("Invalid option\n");
}
}
}
void qinsert()
{
char x;
if((front==0&&rear==qmax-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("\nEnter element to be insert:");
scanf("\n%c",&x);
if(rear==qmax-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
printf("%c is successfully inserted", x);
}
}
void qdelete()
{
char a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
return;
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else if(front==qmax-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%c\n", a);
}
void qdisplay()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
return;
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%c",q[i]);
for(j=front;j<=qmax-1;j++)
printf("\t%c",q[j]);
printf("\nrear is at %c\n",q[rear]);
printf("\nfront is at %c\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
printf("\t%c",q[i]);
printf("\nrear is at %c\n",q[rear]);
printf("\nfront is at %c\n",q[front]);
}
printf("\n");
}
OUTPUT:-
Circular Queue operations
1.insert
2.delete
3.display
4.exit
Enter your choice: 1
Enter element to be insert: A
A is successfully inserted
Enter your choice:1
Enter element to be insert: B
B is successfully inserted
Enter your choice:1
Enter element to be insert: C
C is successfully inserted
Enter your choice: 3
A B C
rear is at C
front is at A
Enter your choice: 2
Deleted element is:A
Enter your choice: B C
rear is at C
front is at B
Enter your choice: 4
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 / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int count=0;
struct node
{
int sem,phno;
char name[20],branch[20],usn[10];
struct node *next;
}*first=NULL,*last=NULL,*temp=NULL,*temp1=NULL;
void create()
{
int sem,phno;
char name[20],usn[10],branch[20];
temp=(struct node *)malloc(sizeof(struct node));
strcpy(temp->usn,usn);
strcpy(temp->name,name);
strcpy(temp->branch,branch);
temp->sem=sem;
temp->phno=phno;
temp->next=NULL;
count++;
}
void deletefront()
{
temp=first;
if(first==NULL)
{
{
create();
if(first==NULL)
{
first=temp;
last=first;
}
else
{
temp->next=first;
first=temp;
}
}
void insertatlast()
{
create();
if(first==NULL)
{
first=temp;
last=first;
}
else
{
last->next=temp;
last=temp;
}
}
void display()
{
if(first==NULL)
{
printf("\n list is empty");
}
else
{
temp=first;
printf("The node is \n");
while(temp!=NULL)
{
printf("%s\t%s\t%s\t%d\t%d--->",temp->name,temp->usn,temp-
>branch,temp->sem,temp->phno);
temp=temp->next;
//printf("\n");
}
}
}
void main()
{
int ch,i,n;
clrscr();
while(1)
{
printf("\n1.Insert n details student ");
printf("\n2.Insert at beginning");
printf("\n3.Insert at last");
printf("\n4.Delete from begining");
printf("\n5.Delete from last");
printf("\n6.Display");
printf("\n7.Exit");
printf("\nEneter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\nEnter the value of n ");
scanf("%d",&n);
for(i=0;i<n;i++)
insertatfirst();
break;
case 2 : insertatfirst();
break;
case 3 : insertatlast();
break;
case 4 : deletefront();
break;
case 5 : deleteatend();
break;
case 6 : display();
break;
case 7 : exit(1);
default: printf("\n Wrong Input, try again");
}
}
}
OUTPUT:-
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
5.Delete from last
6.Display
7.Exit
Eneter your choice : 1
Enter the value of n 2
Enter the details
Name: ARUNA
USN: 16IS002
Branch:ISE
Sem:3
Phone Number:1234
Enter the details
Name: VINAY
USN:16CS095
Branch:CSE
Sem:3
Phone Number:2345
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
5.Delete from last
6.Display
7.Exit
Eneter your choice : 6
The node is
VINAY 16CS095 CSE 3 2345--->ARUNA 16IS002
ISE 3 1234--->
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
5.Delete from last
6.Display
7.Exit
Eneter your choice :2
Enter the details
Name: AJAY
USN:16EC006
Branch:ECE
Sem:3
Phone Number:3456
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
5.Delete from last
6.Display
7.Exit
Eneter your choice : 6
The node is
AJAY 16EC006 ECE 3 3456--->VINAY 16CS095
CSE 3 2345--->ARUNA 16IS002 ISE 3 1234--->
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
5.Delete from last
6.Display
7.Exit
Eneter your choice :3
Enter the details
Name: ROOPA
USN:16EE030
Branch:EEE
Sem:3
Phone Number:4567
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
5.Delete from last
6.Display
7.Exit
Eneter your choice : 6
The node is
AJAY 16EC006 ECE 3 3456--->VINAY 16CS095
CSE 3 2345--->ARUNA 16IS002 ISE 3 1234--->ROOPA
16EE030 EEE 3 4567
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
5.Delete from last
6.Display
7.Exit
Eneter your choice :4
The deleted node is
AJAY 16EC006 ECE 3 3456
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
5.Delete from last
6.Display
7.Exit
Eneter your choice : 6
The node is
VINAY 16CS095 CSE 3 2345--->ARUNA 16IS002
ISE 3 1234--->ROOPA 16EE030 EEE 3 4567
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
5.Delete from last
6.Display
7.Exit
Eneter your choice : 5
The deleted node is
ROOPA 16EE030 EEE 3 4567
1.Insert n details student
2.Insert at beginning
3.Insert at last
4.Delete from begining
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<conio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct node
{
struct node *prev;
int ssn;
long int phno;
char name[20],dept[20],desg[10];
float sal;
struct node *next;
}*first=NULL,*last=NULL,*temp=NULL,*temp1=NULL;
void create()
{
int ssn;
long int phno;
char name[20],dept[20],desg[10];
float sal;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the employee details: ssn,name,dept,desg,sal,phno");
scanf("%d%s%s%s%f%ld",&ssn,name,dept,desg,&sal,&phno);
strcpy(temp->name,name);
strcpy(temp->dept,dept);
strcpy(temp->desg,desg);
temp->ssn=ssn;
temp->sal=sal;
temp->phno=phno;
temp->prev=NULL;
temp->next=NULL;
count++;
}
void deleteatfirst()
{
temp=first;
if(first==NULL)
{
printf("\n DLL is empty");
return;
}
if(temp->next==NULL)
{
printf("\nDeleted node is:");
printf("%d\t%s\t%s\t%s\t%f\t%ld",temp->ssn,temp->name,temp->dept,temp-
>desg,temp->sal,temp->phno);
free(temp);
first=NULL;
}
else
{
first=temp->next;
printf("\nDeleted node is:");
printf("%d\t%s\t%s\t%s\t%f\t%ld",temp->ssn,temp->name,temp->dept,temp-
>desg,temp->sal,temp->phno);
free(temp);
first->prev=NULL;
}
count--;
}
void deleteatlast()
{
temp=first;
if(first==NULL) {
printf("\n DLL is empty");
return;
}
if(temp->next==NULL)
{
printf("\nDeleted node is:");
printf("%d\t%s\t%s\t%s\t%f\t%ld",temp->ssn,temp->name,temp->dept,temp-
>desg,temp->sal,temp->phno);
free(temp);
first=NULL;
}
else
{
printf("\nThe deleted node is:");
temp1=last->prev;
printf("%d\t%s\t%s\t%s\t%d\t%ld",last->ssn,last->name,last->dept,last-
>desg,last->sal,last->phno);
free(last);
last=temp1;
last->next=NULL;
}
count--;
}
void insertatfirst()
{
create();
if(first==NULL)
{
first=temp;
last=first;
}
else
{
first->prev=temp;
temp->next=first;
first=temp;
}
}
void insertatlast()
{
create();
if(first==NULL)
{
first=temp;
last=first;
}
else
{
last->next=temp;
temp->prev=last;
last=temp;
}
}
void display()
{
if(first==NULL)
{
printf("\nDLL is empty.");
return;
}
else
{
temp=first;
printf("\n DLL is\n");
while(temp!=NULL)
{
printf("%d\t%s\t%s\t%s\t%f\t%ld\n",temp->ssn,temp->name,temp-
>dept,temp->desg,temp->sal,temp->phno);
temp=temp->next;
}
printf("\nThe number of nodes in the linked list is %d.",count);
}
}
void main()
{
int ch,i,n;
clrscr();
while(1)
{
printf("\nEnter the details:");
printf("\n1.Enter n employee details.");
printf("\n2.Insert at beginning.");
printf("\n3.Insert at last");
printf("\n4.Delete at beginning");
printf("\n5.Delete at last");
printf("\n6.Display");
printf("\n7.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter n value:");
scanf("%d",&n);
for(i=0;i<n;i++)
insertatlast();
break;
case 2:insertatfirst();
break;
case 3:insertatlast();
break;
case 4:deleteatfirst();
break;
case 5:deleteatlast();
break;
case 6:display();
break;
case 7:exit(1);
default:printf("Invalid input.");
}
}
}
OUTPUT:-
Enter the details:
1.Enter n employee details.
2.Insert at beginning.
3.Insert at last
4.Delete at beginning
5.Delete at last
6.Display
7.Exit
Enter your choice:1
Enter n value: 2
Enter the employee details:ssn,name,dept,desg,sal,phno
NODE* getnode()
{
NODE *x;
x=(NODE*)malloc(sizeof(NODE));
if(x==NULL)
{
printf("Insufficient memory\n");
exit(0);
}
return x;
}
NODE *temp;
if(head->link==head)
{
printf("Polynomial does not exist\n");
return;
}
temp=head->link;
printf("\n");
while(temp!=head)
{
printf("%d x^%d y^%d z^%d",temp->cf,temp->px,temp->py,temp->pz);
if(temp->link != head)
printf(" + ");
temp=temp->link;
}
printf("\n");
}
NODE *temp,*cur;
temp=getnode();
temp->cf=cf;
temp->px=x;
temp->py=y;
temp->pz=z;
temp->flag=0;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
z1=p1->pz;
cf1=p1->cf;
p2=h2->link;
while(p2!=h2)
{
x2=p2->px;
y2=p2->py;
z2=p2->pz;
cf2=p2->cf;
if(x1==x2 && y1==y2 && z1==z2)
break;
p2=p2->link;
}
if(p2!=h2)
{
cf=cf1+cf2;
p2->flag=1;
if(cf!=0)
h3=insert_rear(cf,x1,y1,z1,h3);
}
else
h3=insert_rear(cf1,x1,y1,z1,h3);
p1=p1->link;
}
p2=h2->link;
while(p2!=h2)
{
if(p2->flag==0)
h3=insert_rear(p2->cf,p2->px,p2->py,p2->pz,h3);
p2=p2->link;
}
return h3;
}
void evaluate(NODE *he)
{
NODE *head;
int x, y, z;
float result=0.0;
head=he;
printf("\nEnter x, y, z, terms to evaluate:\n");
scanf("%d%d%d", &x, &y, &z);
he=he->link;
while(he != head)
{
result = result + (he->cf * pow(x,he->px) * pow(y,he->py) * pow(z,he->pz));
he=he->link;
}
printf("\nPolynomial result is: %f", result);
}
void main()
{
NODE *h1,*h2,*h3,*he;
int ch;
clrscr();
while(1)
{
printf("\n\n1.Evaluate polynomial\n2.Add two polynomials\n3.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: he=getnode();
he->link=he;
printf("\nEnter polynomial to evaluate:\n");
he=read_poly(he);
display(he);
evaluate(he);
free(he);
break;
case 2: h1=getnode();
h2=getnode();
h3=getnode();
h1->link=h1;
h2->link=h2;
h3->link=h3;
printf("\nEnter the first polynomial:");
h1=read_poly(h1);
printf("\nEnter the second polynomial:");
h2=read_poly(h2);
h3=add_poly(h1,h2,h3);
printf("\nFirst polynomial is: ");
display(h1);
printf("\nSecond polynomial is: ");
display(h2);
printf("\nThe sum of 2 polynomials is: "); display(h3);
break;
case 3:exit(0);
break;
default:printf("\nInvalid entry");
break;
}
getch();
}
}
OUTPUT:-
1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter coeff: 6
Enter x, y, z powers(0-indiacate NO term):
221
If you wish to continue press 1 otherwise 0: 1
Enter coeff: -4
Enter x, y, z powers(0-indiacate NO term):
015
If you wish to continue press 1 otherwise 0: 1
Enter coeff: 3
Enter x, y, z powers(0-indiacate NO term):
311
If you wish to continue press 1 otherwise 0: 1
Enter coeff: 2
Enter x, y, z powers(0-indiacate NO term):
151
If you wish to continue press 1 otherwise 0: 1
Enter coeff: -2
Enter x, y, z powers(0-indiacate NO term):
113
If you wish to continue press 1 otherwise 0: 0
6 x^2 y^2 z^1 + -4 x^0 y^1 z^5 + 3 x^3 y^1 z^1 + 2 x^1 y^5 z^1 + -2 x^1 y^1 z^3
Enter x, y, z, terms to evaluate:
222
Polynomial result is: 224.000000
1.Evaluate polynomial
2.Add two polynomials
3.Exit
Enter your choice: 2
Enter the first polynomial:
Enter coeff: 1
Enter x, y, z powers(0-indiacate NO term):
111
If you wish to continue press 1 otherwise 0: 1
Enter coeff: 3
Enter x, y, z powers(0-indiacate NO term):
345
If you wish to continue press 1 otherwise 0: 0
Enter the second polynomial:
Enter coeff: 2
Enter x, y, z powers(0-indiacate NO term):
546
If you wish to continue press 1 otherwise 0: 1
Enter coeff: 1
Enter x, y, z powers(0-indiacate NO term):
111
If you wish to continue press 1 otherwise 0: 0
First polynomial is:
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 from BST
e. Exit
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct BST
{
int item;
struct BST *llink, *rlink;
};
typedef struct BST* NODE;
NODE insert(NODE);
void inorder(NODE);
void preorder(NODE);
void postorder(NODE);
NODE search(NODE, int);
NODE Delete(NODE, int);
void main()
{
int choice, key,n,i;
NODE root = NULL, tmp, parent;
clrscr();
while(1)
{
printf("\n1.Create");
printf("\n2.Traverse the Tree in Preorder, Inorder, Postorder");
printf("\n3.Search");
printf("\n4.Delete an element from the Tree");
printf("\n5.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice)
{
case 1: printf("\n enter the number of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
root = insert(root);
break;
if (root == NULL)
return temp;
prev = NULL;
cur = root;
while(cur != NULL)
{
prev = cur;
if (item < cur-> item)
cur = cur->llink;
else
cur = cur->rlink;
}
if (item < prev->item)
prev->llink = temp;
else
prev->rlink = temp;
return root;
}
else
cur = cur->rlink;
}
return NULL;
}
NODE Delete(NODE root, int data)
{
NODE temp;
int min;
if (root == NULL)
{
return NULL;
}
if (data < root->item)
{ // data is in the left sub tree.
root->llink = Delete(root->llink, data);
}
else if (data > root->item)
{ // data is in the right sub tree.
root->rlink = Delete(root->rlink, data);
}
else
{
// case 1: no children
if (root->llink == NULL && root->rlink == NULL)
{
free(root); // wipe out the memory, in C, use free function
root = NULL;
}
// case 2: one child (right)
else if (root->llink == NULL)
{
temp = root; // save current node as a backup
root = root->rlink;
free(temp);
}
// case 3: one child (left)
else if (root->rlink == NULL)
{
temp = root; // save current node as a backup
root = root->llink;
free(temp);
}
// case 4: two children
else
{
min = FindMin(root->rlink); // find minimal value of right sub tree
root->item = min; // duplicate the node
root->rlink = Delete(root->rlink, min); // delete the duplicate node
}
}
return root; // parent node can update reference
}
int FindMin(NODE root) {
if (root == NULL) {
return -1; // or undefined.
}
if (root->llink != NULL) {
return FindMin(root->llink); // left tree is smaller
}
return root->item;
}
OUTPUT:-
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :1
enter the number of nodes 7
Enter The Element 16
Enter The Element 9
Enter The Element 11
Enter The Element 24
Enter The Element 27
Enter The Element 4
Enter The Element 17
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :2
The Inorder display : 4 9 11 16 17 24 27
The Preorder display : 16 9 4 11 24 17 27
The Postorder display : 4 11 9 17 27 24 16
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :3
Enter Element to be searched :11
The element 11 found
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :3
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :4
Enter Element to be deleted :16
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice : 2
The Inorder display : 4 9 11 17 24 27
The Preorder display : 17 9 4 11 24 27
The Postorder display : 4 11 9 27 24 17
1.Create
2.Traverse the Tree in Preorder, Inorder, Postorder
3.Search
4.Delete an element from the Tree
5.Exit
Enter your choice :5
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
DFS/BFS method
#include<stdio.h>
#include<stdlib.h>
int n,a[10][10],i,j,source,s[10],reach[10],choice,count=0;
scanf("%d",&a[i][j]);
while(1)
{
printf("\nEnter your choice\n");
printf("1.BFS\n 2.DFS\n 3.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n Enter the source :");
scanf("%d",&source); //Provide source for BFS
for(i=1;i<=n;i++)
s[i]=0;
bfs(n,a,source,s);
for(i=1;i<=n;i++)
{
if(s[i]==0)
printf("\n The node %d is not reachable",i);
else
printf("\n The node %d is reachable",i);
}
break;
case 2: dfs(1);
if(count==n-1)
printf("\nThe graph is connected.");
else
printf("\nThe graph is not connected.");
break;
case 3: exit(0);
}
}
}
OUTPUT:-
Enter the number of nodes : 5
Enter the adjacency matrix
01010
00010
01000
00001
00100
Enter your choice
1.BFS
2.DFS
3.Exit
1
Enter the source :2
The node 1 is not reachable
The node 2 is reachable
The node 3 is reachable
The node 4 is reachable
The node 5 is reachable
#include<stdio.h>
#include<stdlib.h>
FILE *fp;
struct employee
{
char name[20];
int key,salary;
}emp[20];
int n,m;
int *ht,index;
int count = 0;
index = key % m;
while(ht[index] != -1)
{
printf("\ncollision detected for %d and resloved using linear
probing",key);
index = (index+1)%m;
}
ht[index] = key;
count++;
}
void display()
{
int i;
if(count == 0)
{
printf("\nHash Table is empty");
return;
}
printf("\nEnter the two digit memory locations (m) for hash table: ");
scanf("%d", &m);
ht = (int *)malloc(m*sizeof(int));
for(i=0; i<m; i++)
ht[i] = -1;
fp=fopen("C:\\PROGRAM1.txt","w");
printf("\nEnter the four digit key,name,salary values (K) for N Employee Records:\n
");
for(i=0; i<n; i++)
{
scanf("%d%s%d",&emp[i].key,&emp[i].name,&emp[i].salary);
fprintf(fp,"%d\t%s\t%d\n",emp[i].key,emp[i].name,emp[i].salary);
}
fclose(fp);
fp=fopen("C:\\PROGRAM1.txt","r");
for(i=0;i<n;i++)
{
if(count == m)
{
printf("\n~~~Hash table is full. Cannot insert the record %d
key~~~",i+1);
break;
}
fscanf(fp,"%d",&emp[i].key);
insert(emp[i].key);
}
fclose(fp);
//Displaying Keys inserted into hash table
display();
getch();
}
OUTPUT:-
Enter the number of employee records (N) : 5
Enter the two digit memory locations (m) for hash table: 10
Enter the four digit key,name,salary values (K) for N Employee Records:
1111 ajay 12345
7689 bhavya 12323
2222 chaitra 13212
2341 divya 14231
2342 girija 15231
collision detected for 2341 and resloved using linear probing
collision detected for 2341 and resloved using linear probing
collision detected for 2342 and resloved using linear probing
collision detected for 2342 and resloved using linear probing
Hash Table contents are:
T[0] --> -1
T[1] --> 1111
T[2] --> 2222
T[3] --> 2341
T[4] --> 2342
T[5] --> -1
T[6] --> -1
T[7] --> -1
T[8] --> -1
T[9] --> 7689