Dsa Lab Manual-1
Dsa Lab Manual-1
Lab Manual
On
“DATA STRUCTURES AND APPLICATION”
BCS305
III Semester of
Bachelor of Engineering in Computer Science and Engineering of
Visvesvaraya Technological University, Belagavi
Mrs .Pallavi K V
Assistant Professor
Dept. of Computer Science and Engineering
AMC Engineering College
1
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
2
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
}
void read()
{
int i;
for(i=0;i<7;i++)
{
printf("Enter day name:");
scanf("%s",calendar[i]->name);
printf("Enter Date:");
scanf("%d",&calendar[i]->date);
printf("Enter activity:");
scanf("%s",calendar[i]->activity);
printf("\n");
}
}
void display()
{
int i;
printf("Weekly activity details:\n");
for(i=0;i<7;i++)
{
printf("Day%d:%s--%d,\tActivity:%s\n",i+1,calendar[i]-
>name,calendar[i]->date,calendar[i]->activity);
}
}
int main()
{
3
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
int i;
printf("Creating calendar:\n");
create();
printf("Reading calendar:\n");
read();
printf("Displaying calendar:\n");
display();
for(i=0;i<7;i++)
{
free(calendar[i]->name);
free(calendar[i]->activity);
free(calendar[i]);
}
return 0;
}
OUTPUT:
Creating calendar:
Reading calendar:
Enter day name: MONDAY
Enter Date:1
Enter activity: RUN
Enter day name: TUESDAY
Enter Date:2
Enter activity: JOG
Enter day name: WEDNESDAY
4
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
Enter Date:3
Enter activity: SWIM
Enter day name: THURSDAY
Enter Date:4
Enter activity: SLEEP
Enter day name: FRIDAY
Enter Date:5
Enter activity: COOK
Enter day name: SATURDAY
Enter Date:6
Enter activity: DANCE
Enter day name: SUNDAY
Enter Date:7
Enter activity: SING
Displaying calendar:
Weekly activity details:
Day1:MONDAY--1, Activity: RUN
Day2:TUESDAY--2, Activity: JOG
Day3:WEDNESDAY--3, Activity: SWIM
Day4:THURSDAY--4, Activity: SLEEP
Day5:FRIDAY--5, Activity: COOK
Day6:SATURDAY--6, Activity: DANCE
Day7:SUNDAY--7, Activity: SING
5
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
6
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
while(str[c]!='\0')
{
if(str[m]==pat[i])
{
i++;
m++;
if(pat[i]=='\0')
{
flag=1;
for(k=0;rep[k]!='\0';k++,j++)
update[j]=rep[k];
i=0;
c=m;
}
}
else
{
update[j]=str[c];
j++;
c++;
m=c;
i=0;
}
}
if(flag==1)
{
7
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
update[j]='\0';
printf("The resultant string is \n%s", update);
}
else
printf("string not found:");
}
OUTPUT :
Enter a string:
GOOD MORNING
Enter a search string:
MORN
Enter a replace string:
EVEN
The resultant string is
GOOD EVENING
8
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
9
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
}
void display()
{
if (top == -1)
{
printf("Stack is empty\n");
return;
}
printf("Contents of the stack:\n");
for (int i = 0; i <= top; i++)
{
printf("%d\n", s[i]);
}
}
void palindrome()
{
int i, j;
for (i = 0, j = top; i <= j; i++, j--) {
if (s[i] != s[j])
{
printf("Not palindrome\n");
return;
}
}
printf("Palindrome\n");
}
10
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
int main()
{
int ch;
while (1)
{
printf("MENU\n");
printf("1.Push\t2.Pop\t3.Check palindrome\t4.Display\t5.Exit\n");
printf("--------------------\n");
printf("Enter your choice:\n");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the item:");
scanf("%d", &item);
push();
break;
case 2:
printf("Popped value=%d\n", pop());
break;
case 3:
palindrome();
break;
case 4:
display();
break;
case 5:
11
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
exit(0);
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
OUTPUT :
MENU
1.Push 2.Pop 3.Check palindrome 4.Display 5.Exit
--------------------
Enter your choice:
4
Stack is empty
MENU
1.Push 2.Pop 3.Check palindrome 4.Display 5.Exit
--------------------
Enter your choice:
1
Enter the item:1
MENU
1.Push 2.Pop 3.Check palindrome 4.Display 5.Exit
--------------------
Enter your choice:
1
12
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
13
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
14
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':
return 1;
case '*':
case '%':
case '/':
return 3;
case '^':
case '$':
return 6;
case '(':
return 9;
case '#':
return 0;
default : return 7;
}
}
void infix_postfix(char infix[],char postfix[])
{
int top,i,j;
char s[30];
15
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
char symbol;
top=-1;
s[++top]='#';
j=0;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol))
postfix[j++]=s[top--];
if(F(s[top])!=G(symbol))
s[++top]=symbol;
else top--;
}
while(s[top]!='#')
postfix[j++]=s[top--];
postfix[j]='\0';
}
void main()
{
char infix[20];
char postfix[20];
printf("Enter the valid infix expression:\n");
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("The postfix expression is:\n");
printf("%s\n",postfix);
16
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
OUTPUT :
Enter the valid infix expression:
A-B-D*E/F+B*C
The postfix expression is:
AB-DE*F/-BC*+
17
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
18
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
return pow(op1,op2);
break;
default:return 0;
}
}
void main()
{
double s[20],res,op1,op2;
int top,i;
char postfix[20],symbol;
printf("\nEnter the postfix expression:\n");
gets(postfix);
top=-1;
for(i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if(isdigit(symbol))
s[++top]=symbol-'0';
else
{
op2=s[top--];
op1=s[top--];
res=compute(symbol,op1,op2);
s[++top]=res;
}
}
19
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
res=s[top--];
printf("\nThe result is :%f\n",res);
}
OUTPUT :
Enter the postfix expression:
65+3-
The result is :8.000000
b) Solving Tower of Hanoi with n disks.
#include<stdio.h>
void transfer(int n,char s,char t,char d)
{
if(n==0)
return;
transfer(n-1,s,d,t);
printf("Move disc %d from %c to %c\n",n,s,d);
transfer(n-1,t,s,d);
}
void main()
{
int n;
printf("Enter the number of discs:\n");
scanf("%d",&n);
transfer(n,'A','B','C');
}
20
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
return -1;
int item = q[front];
front = (front + 1) % QUE_SIZE;
count--;
return item;
}
void display()
{
if (count == 0)
{
printf("Queue is empty\n");
return;
}
printf("Contents of the queue are:\n");
int i, f;
for (i = 0, f = front; i < count; i++)
{
printf("%c\n", q[f]);
f = (f + 1) % QUE_SIZE;
}
}
int main()
{
int choice;
front = 0;
rear = -1;
22
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
for (;;)
{
printf("1.Insert\t 2.Delete\n");
printf("3.Display\t 4.Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the item to be inserted: ");
scanf(" %c", &item);
insertQ();
break;
case 2:
item = deleteQ();
if (item == -1)
{
printf("Queue is empty\n");
}
else
{
printf("Item deleted from queue is %c\n", item);
}
break;
case 3:
display();
23
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
OUTPUT:
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 1
Enter the item to be inserted: A
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 1
Enter the item to be inserted: B
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 1
Enter the item to be inserted: C
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 1
Enter the item to be inserted: D
24
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 1
Enter the item to be inserted: E
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 3
Contents of the queue are:
A
B
C
D
E
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 1
Enter the item to be inserted: F
Queue Overflow
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 2
Item deleted from queue is A
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 3
Contents of the queue are:
25
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
B
C
D
E
1.Insert 2.Delete
3.Display 4.Exit
Enter your choice: 4
26
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int USN, SEM, PHNO;
char NAME[20], BRANCH[10];
struct node *link;
};
NODE insert_front()
{
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
printf("Enter student details:\n");
printf("USN\t Name\tBranch\tSem\tPhno.\n");
scanf("%d\t%s\t%s\t%d\t%d", &USN, name, branch, &SEM, &phno);
temp->USN = USN;
strcpy(temp->NAME, name);
strcpy(temp->BRANCH, branch);
temp->SEM = SEM;
27
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
temp->PHNO = phno;
temp->link = first;
count++;
return temp;
}
void create()
{
int i, n;
printf("Enter number of students:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
first = insert_front();
}
}
NODE delete_front()
{
NODE temp;
if (first == NULL)
{
printf("List is empty cannot delete\n");
return first;
}
temp = first;
first = first->link;
printf("Student usn=%d,name=%s\n", temp->USN, temp->NAME);
free(temp);
count--;
return first;
}
NODE insert_rear()
{
NODE temp;
NODE cur;
temp = (NODE)malloc(sizeof(struct node));
28
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
if (first == NULL)
{
first = temp;
count++;
return first;
}
cur = first;
while (cur->link != NULL)
{
cur = cur->link;
}
cur->link = temp;
count++;
return first;
}
NODE delete_rear()
{
NODE temp, cur, prev;
if (first == NULL)
{
printf("List is empty cannot delete\n");
return first;
}
if (first->link == NULL)
{
printf("Student USN=%d;name=%s\n", first->USN, first->NAME);
29
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
count--;
free(first);
return NULL;
}
prev = NULL;
cur = first;
while (cur->link != NULL)
{
prev = cur;
cur = cur->link;
}
printf("Student USN=%d;name=%s\n", cur->USN, cur->NAME);
free(cur);
prev->link = NULL;
count--;
return first;
}
void display()
{
NODE temp;
if (first == NULL)
{
printf("List is empty\n");
return;
}
printf("The contents of singly linked list:\n");
temp = first;
while (temp != NULL)
{
printf("%d\t%s\t%s\t%d\t%d\n", temp->USN, temp->NAME, temp-
>BRANCH, temp->SEM, temp->PHNO);
temp = temp->link;
}
printf("The number of nodes in singly linked list=%d\n", count);
}
int main()
30
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
{
int choice;
for (;;)
{
printf("\n1.Create\t2.Display\n3.Insert_rear\t4.Delete_rear\n5.Demonstration
of stack Insert_front,Delete_front\nEnter your choice:\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
first = insert_rear();
break;
case 4:
first = delete_rear();
break;
case 5:
printf("Push\n");
first = insert_front();
printf("Pop\n");
first = delete_front();
break;
default:
printf("INVALID CHOICE\n");
exit(0);
}
}
return 0;
}
31
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
OUTPUT :
1.Create 2.Display
3.Insert_rear 4.Delete_rear
5.Demonstration of stack Insert_front,Delete_front
Enter your choice:
1
Enter number of students:
3
Enter student details:
USN Name Branch Sem Phno.
1 XXX CSE 3 100
Enter student details:
USN Name Branch Sem Phno.
2 YYY CSE 3 101
Enter student details:
USN Name Branch Sem Phno.
3 ZZZ CSE 3 102
1.Create 2.Display
3.Insert_rear 4.Delete_rear
5.Demonstration of stack Insert_front,Delete_front
Enter your choice:
2
The contents of singly linked list:
3 ZZZ CSE 3 102
2 YYY CSE 3 101
1 XXX CSE 3 100
The number of nodes in singly linked list=3
1.Create 2.Display
3.Insert_rear 4.Delete_rear
5.Demonstration of stack Insert_front,Delete_front
Enter your choice:
3
Enter student details:
USN Name Branch Sem Phno
0 XYZ CSE 3 099
1.Create 2.Display
3.Insert_rear 4.Delete_rear
32
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
1.Create 2.Display
3.Insert_rear 4.Delete_rear
5.Demonstration of stack Insert_front,Delete_front
Enter your choice:
4
Student USN=0;name=XYZ
1.Create 2.Display
3.Insert_rear 4.Delete_rear
5.Demonstration of stack Insert_front,Delete_front
Enter your choice:
2
The contents of singly linked list:
3 ZZZ CSE 3 102
2 YYY CSE 3 101
1 XXX CSE 3 100
The number of nodes in singly linked list=3
1.Create 2.Display
3.Insert_rear 4.Delete_rear
5.Demonstration of stack Insert_front,Delete_front
Enter your choice:
5
Push
Enter student details:
USN Name Branch Sem Phno.
4 SGK CSE 3 988
Pop
33
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
Student usn=4,name=SGK
1.Create 2.Display
3.Insert_rear 4.Delete_rear
5.Demonstration of stack Insert_front,Delete_front
Enter your choice:
2
The contents of singly linked list:
3 ZZZ CSE 3 102
2 YYY CSE 3 101
1 XXX CSE 3 100
The number of nodes in singly linked list=3
1.Create 2.Display
3.Insert_rear 4.Delete_rear
5.Demonstration of stack Insert_front,Delete_front
Enter your choice: 0
34
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
struct node
{
int ssn;
char name[20], dept[20], desg[20], ph[20];
float sal;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
35
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
x = (NODE)malloc(sizeof(struct node));
if (x == NULL)
{
printf("Out of memory\n");
exit(0);
}
return x;
}
NODE insert_front(EMPL item, NODE first)
{
NODE temp;
temp = getnode();
temp->ssn = item.ssn;
strcpy(temp->name, item.name);
strcpy(temp->dept, item.dept);
strcpy(temp->desg, item.desg);
temp->sal = item.sal;
strcpy(temp->ph, item.ph);
temp->llink = temp->rlink = NULL;
if (first == NULL)
return temp;
temp->rlink = first;
first->llink = temp;
return temp;
}
NODE insert_rear(EMPL item, NODE first)
{
NODE temp, cur;
temp = getnode();
36
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
temp->ssn = item.ssn;
strcpy(temp->name, item.name);
strcpy(temp->dept, item.dept);
strcpy(temp->desg, item.desg);
temp->sal = item.sal;
strcpy(temp->ph, item.ph);
temp->llink = temp->rlink = NULL;
if (first == NULL)
return temp;
cur = first;
while (cur->rlink != NULL)
{
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur;
return first;
}
NODE delete_front(NODE first)
{
NODE second;
if (first == NULL)
{
printf("Employee list is empty\n");
return NULL;
}
if (first->rlink == NULL)
{
printf("Employee details deleted: ssn=%d\n", first->ssn);
37
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
free(first);
return NULL;
}
second = first->rlink;
second->llink = NULL;
printf("Employee details: ssn=%d\n", first->ssn);
free(first);
return second;
}
NODE delete_rear(NODE first)
{
NODE cur, prev;
if (first == NULL)
{
printf("List is empty cannot delete\n");
return first;
}
if (first->rlink == NULL)
{
printf("Employee details deleted: ssn=%d\n", first->ssn);
free(first);
return NULL;
}
prev = NULL;
cur = first;
while (cur->rlink != NULL)
{
prev = cur;
cur = cur->rlink;
38
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
}
printf("Employee details deleted: ssn=%d\n", cur->ssn);
free(cur);
prev->rlink = NULL;
return first;
}
void display(NODE first)
{
NODE cur;
int count = 0;
if (first == NULL)
{
printf("Employee list is empty\n");
return;
}
cur = first;
while (cur != NULL)
{
printf("SSN-%d\nSALARY-%f\nNAME-%s\nDEPARTMENT-%s\nDESIGNATION-
%s\nPHONE-%s\n", cur->ssn, cur->sal, cur->name, cur->dept, cur->desg, cur->ph);
cur = cur->rlink;
count++;
}
printf("Number of employees=%d\n", count);
}
int main()
{
NODE first;
int choice;
EMPL item;
39
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
first = NULL;
for (;;)
{
printf("\n1.Insert_front\t2.Insert_rear\n3.Delete_front\t4.Delete_rear\n5.Display\t6.Exit\n
");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("SSN:");
scanf("%d", &item.ssn);
printf("NAME:");
scanf("%s", &item.name);
printf("DEPARTMENT:");
scanf("%s", &item.dept);
printf("SALARY:");
scanf("%f", &item.sal);
printf("DESIGNATION:");
scanf("%s", &item.desg);
printf("PHONE:");
scanf("%s", &item.ph);
first = insert_front(item, first);
break;
case 2:
printf("SSN:");
scanf("%d", &item.ssn);
printf("NAME:");
scanf("%s", &item.name);
40
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
printf("DEPARTMENT:");
scanf("%s", &item.dept);
printf("SALARY:");
scanf("%f", &item.sal);
printf("DESIGNATION:");
scanf("%s", &item.desg);
printf("PHONE:");
scanf("%s", &item.ph);
first = insert_rear(item, first);
break;
case 3:
first = delete_front(first);
break;
case 4:
first = delete_rear(first);
break;
case 5:
display(first);
break;
case 6:
exit(0);
break;
}
}
return 0;
}
OUTPUT :
1.Insert_front 2.Insert_rear
3.Delete_front 4.Delete_rear
41
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
5.Display 6.Exit
Enter your choice:
1
SSN:1
NAME : XXX
DEPARTMENT : HR
SALARY : 100000
DESIGNATION : MANAGER
PHONE : 100
1.Insert_front 2.Insert_rear
3.Delete_front 4.Delete_rear
5.Display 6.Exit
Enter your choice :
1
SSN : 2
NAME : YYY
DEPARTMENT : HR
SALARY : 50000
DESIGNATION : ASSISTANT
PHONE : 200
1.Insert_front 2.Insert_rear
3.Delete_front 4.Delete_rear
5.Display 6.Exit
Enter your choice :
5
SSN - 2
SALARY - 50000.000000
NAME - YYY
DEPARTMENT - HR
42
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
DESIGNATION - ASSISTANT
PHONE - 200
SSN - 1
SALARY - 100000.000000
NAME - XXX
DEPARTMENT - HR
DESIGNATION - MANAGER
PHONE - 100
Number of employees=2
1.Insert_front 2.Insert_rear
3.Delete_front 4.Delete_rear
5.Display 6.Exit
Enter your choice :
2
SSN : 3
NAME : ZZZ
DEPARTMENT : PR
SALARY : 150000
DESIGNATION : SEN.MANAGER
PHONE : 234
1.Insert_front 2.Insert_rear
3.Delete_front 4.Delete_rear
5.Display 6.Exit
Enter your choice :
5
SSN - 2
SALARY - 50000.000000
NAME - YYY
43
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
DEPARTMENT - HR
DESIGNATION - ASSISTANT
PHONE - 200
SSN - 1
SALARY - 100000.000000
NAME - XXX
DEPARTMENT - HR
DESIGNATION - MANAGER
PHONE - 100
SSN - 3
SALARY - 150000.000000
NAME - ZZZ
DEPARTMENT - PR
DESIGNATION - SEN.MANAGER
PHONE - 234
Number of employees = 3
1.Insert_front 2.Insert_rear
3.Delete_front 4.Delete_rear
5.Display 6.Exit
Enter your choice :
3
Employee details: ssn = 2
1.Insert_front 2.Insert_rear
3.Delete_front 4.Delete_rear
5.Display 6.Exit
Enter your choice :
4
44
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
1.Insert_front 2.Insert_rear
3.Delete_front 4.Delete_rear
5.Display 6.Exit
Enter your choice :
5
SSN - 1
SALARY - 100000.000000
NAME - XXX
DEPARTMENT - HR
DESIGNATION - MANAGER
PHONE - 100
Number of employees = 1
1.Insert_front 2.Insert_rear
3.Delete_front 4.Delete_rear
5.Display 6.Exit
Enter your choice :
6
45
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
46
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
temp=getnode();
temp->coef=coef;
temp->xexp=xexp;
temp->yexp=yexp;
temp->zexp=zexp;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
NODE read_poly(NODE head)
{
int i,j,coef,xexp,yexp,zexp,n;
printf("Enter the no of terms in the polynomials:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the %d term:",i+1);
printf("\nCoef=");
scanf("%d",&coef);
printf("\n\t\tEnter Pow(x) Pow(y) Pow(z):");
scanf("%d",&xexp);
scanf("%d",&yexp);
scanf("%d",&zexp);
head=attach(coef,xexp,yexp,zexp,head);
47
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
}
return head;
}
void display(NODE head)
{
NODE temp;
if(head->link==head)
{
printf("\npolynomial does not exist");
return;
}
temp=head->link;
while(temp!=head)
{
printf("%dx^%dy^%dz^%d",temp->coef,temp->xexp,temp->yexp,temp->zexp);
temp=temp->link;
if(temp!=head)
printf("+");
}
}
int poly_evaluate(NODE head)
{
int x,y,z,sum=0;
NODE poly;
printf("\nEnter the value of x,y,z;\n");
scanf("%d %d %d",&x,&y,&z);
poly=head->link;
while(poly!=head)
{
48
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
sum+=poly->coef*pow(x,poly->xexp)*pow(y,poly->yexp)*pow(z,poly->zexp);
poly=poly->link;
}
return sum;
}
NODE poly_sum(NODE head1,NODE head2,NODE head3)
{
NODE a,b;
a=head1->link;
b=head2->link;
while(a!=head1 && b!=head2)
{
while(1)
{
if(a->xexp==b->xexp && a->yexp==b->yexp && a->zexp==b->zexp)
{
int coef=a->coef+b->coef;
head3=attach(coef,a->xexp,a->yexp,a->zexp,head3);
a=a->link;
b=b->link;
break;
}
if(a->xexp!=0||b->xexp!=0)
{
switch(COMPARE(a->xexp,b->xexp))
{
case -1: head3=attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b=b->link;
break;
49
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
case 0: if(a->yexp>b->yexp)
{
head3=attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a=a->link;
break;
}
else if(a->yexp<b->yexp)
{
head3=attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b=b->link;
break;
}
else if(a->zexp>b->zexp)
{
head3=attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a=a->link;
break;
}
else if(a->zexp<b->zexp)
{
head3=attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b=b->link;
break;
}
case 1: head3=attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a=a->link;
break;
}
break;
50
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
}
if(a->yexp!=0||b->yexp!=0)
{
switch(COMPARE(a->yexp,b->yexp))
{
case -1: head3=attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b=b->link;
break;
case 0: if(a->zexp>b->zexp)
{
head3=attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a=a->link;
break;
}
else if(a->zexp<b->zexp)
{
head3=attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b=b->link;
break;
}
case 1: head3=attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a=a->link;
break;
}
break;
}
if(a->zexp!=0||b->zexp!=0)
{
switch(COMPARE(a->zexp,b->zexp))
51
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
{
case -1: head3=attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b=b->link;
break;
case 1: head3=attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a=a->link;
break;
}
break;
}
}
}
while(a!=head1)
{
head3= attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a=a->link;
}
while(b!=head2)
{
head3=attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b=b->link;
}
return head3;
}
int main()
{
NODE head,head1,head2,head3;
int res,ch;
head=getnode();
52
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
head1=getnode();
head2=getnode();
head3=getnode();
head->link=head;
head->link=head;
head1->link=head1;
head2->link=head2;
head3->link=head3;
while(1)
{
printf("MENU\n");
printf("\n1.Represent and evaluate a polynomial P(x,y,z)");
printf("\n2.Find the sum of two polynomials POLY(x,y,z)");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nPolynomial evaluation P(x,y,z)\n");
head=read_poly(head);
printf("\nRepresentation of a polynomial:\n");
display(head);
res=poly_evaluate(head);
printf("\nResult of Polynomial evaluation is:%d\n",res);
break;
case 2:printf("\nEnter POLY1(x,y,z):");
head1=read_poly(head1);
printf("\nEnter POLY2(x,y,z):");
head2=read_poly(head2);
printf("\nPolynomial 1 is\n");
53
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
display(head1);
printf("\nPolynomial 2 is\n");
display(head2);
printf("\nPolynomial addition result:\n");
head3=poly_sum(head1,head2,head3);
display(head3);
break;
case 3: exit(0);
}
}
return 0;
}
OUTPUT:
MENU
54
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
55
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
56
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
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 .
b) Traverse the BST in Inorder , Postorder , Preorder
c) Search the BST for a given element(KEY)and report the appropriate
message.
d) Exit.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
void preorder(NODE root)
57
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
{
if(root!=NULL)
{
printf("%d\n",root->info);
preorder(root->llink);
preorder(root->rlink);
}
}
void postorder(NODE root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\n",root->info);
}
}
void inorder(NODE root)
{
if(root!=NULL)
{
inorder(root->llink);
printf("%d\n",root->info);
inorder(root->rlink);
}
}
58
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
59
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
{
if(root==NULL)
return root;
if(item==root->info)
return root;
if(item<root->info)
return search(item,root->llink);
return search(item,root->rlink);
}
void main()
{
NODE root,cur;
int choice,item;
root=NULL;
for(;;)
{
printf("1.Insert\t2.Preorder\t3.Postorder\t4.Inorder\t5.Search\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the item to be inserted\n");
scanf("%d",&item);
root=insert(item,root);
break;
case 2:
60
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
if(root==NULL)
{
printf("Tree is empty\n");
break;
}
printf("The given tree in tree form\n");
printf("Preorder traversal is\n");
preorder(root);
printf("\n");
break;
case 3:
if(root==NULL)
{
printf("Tree is empty\n");
break;
}
printf("The given tree in tree form\n");
printf("Postorder traversal is\n");
postorder(root);
printf("\n");
break;
case 4:
if(root==NULL)
{
printf("Tree is empty\n");
break;
61
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
}
printf("The given tree in tree form\n");
printf("Inorder traversal is\n");
inorder(root);
printf("\n");
break;
case 5:
printf("Enter the item to be searched:\n");
scanf("%d",&item);
cur=search(item,root);
if(cur==NULL)
printf("Item not found\n");
else
printf("Item found\n");
break;
default:exit(0);
}
}
}
OUTPUT:
1.Insert 2.Preorder 3.Postorder 4.Inorder 5.Search
1
Enter the item to be inserted
6
1.Insert 2.Preorder 3.Postorder 4.Inorder 5.Search
1
62
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
63
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
64
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
8
7
8
15
14
24
65
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
66
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
67
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
68
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
if (count == n - 1)
printf("\nGraph is connected: ");
else
printf("\nGraph is not connected:");
break;
case 3:
exit(0);
default:
printf("\nInvalid choice\n");
}
return 0;
}
OUTPUT :
Enter the number of vertices:
4
Enter graph data in matrix form:
0110
0011
0001
0000
1.BFS 2.DFS 3.Exit
Enter your choice:
1
Enter the starting vertex:
1
The nodes which are reachable from 1: 2 3 4
Enter the number of vertices:
4
Enter graph data in matrix form:
0110
0011
0001
0000
1.BFS 2.DFS 3.Exit
Enter your choice:
1
Enter the starting vertex:
69
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
2
The nodes which are reachable from 2: 3 4
Enter the number of vertices:
4
Enter graph data in matrix form:
0110
0011
0001
0000
1.BFS 2.DFS 3.Exit
Enter your choice:
1
Enter the starting vertex:
3
The nodes which are reachable from 3: 4
Enter the number of vertices:
4
Enter graph data in matrix form:
0110
0011
0001
0000
1.BFS 2.DFS 3.Exit
Enter your choice:
2
Enter the starting vertex:
1
1->2
2->3
3->4
Graph is connected:
Enter the number of vertices:
4
Enter graph data in matrix form:
0110
0011
0001
0000
70
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
71
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
12. Given File of N employee records with a set K of keys(4 – digits) 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 C that uses hash functions H:K->L as
H(K)=K mod m ( remainder method ) and implement hashing technique to map
a given key K to the addresses space L Resolve the collision (if any) using linear
probing.
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
struct employee
{
int id;
char name[15];
};
typedef struct employee EMP;
EMP emp[MAX];
int a[MAX];
int create(int num)
{
int key;
key = num % 100;
return key;
}
int getemp(EMP emp[], int key)
{
printf("\nEnter emp id: ");
scanf("%d", &emp[key].id);
printf("\nEnter emp name: ");
getchar();
fgets(emp[key].name, sizeof(emp[key].name), stdin);
return key;
}
void display()
{
int i, ch;
72
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
73
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
else
i++;
printf("\nCollision avoided successfully using LINEAR
PROBING\n");
if(count == MAX)
{
printf("\n Hash table is full");
display(emp);
exit(1);
}
for(i = key; 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++;
}
}
}
}
int main()
{
int num, key, i;
int ans = 1;
printf("\nCollision handling by linear probing: ");
for(i = 0; i < MAX; i++)
{
74
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
a[i] = -1;
}
do
{
printf("\nEnter the data: ");
scanf("%d", &num);
key = create(num);
linear_prob(key, num);
printf("\nDo you wish to continue? (1/0): ");
scanf("%d", &ans);
}
while(ans);
display(emp);
getchar();
return 0;
}
OUTPUT :
1.Display ALL
2.Filtered Display
Enter the choice: 1
75
DATA STRUCTURES AND APPLICATION LAB MANUAL BCS305
2 0
3 0
4 562 YYY
5 0
6 683 ZZZ
7 0
8 0
9 0
4 562 YYY
6 683 ZZZ
76