DS Lab Manual With Output
DS Lab Manual With Output
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NO_OF_DAYS 7
typedef struct
{
char *name;
int date;
char *activity;
}CALENDER;
for(i=0;i<NO_OF_DAYS;i++)
{
scanf("%s", name);
scanf("%d",&date);
scanf("%s",activity);
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
create_calender(a,i,name,date,activity);\
}
}
void main()
{
CALENDER a[NO_OF_DAYS];
print_weeks_activity(a);
}
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
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.
CODE:
#include<stdio.h>
char str[50], pat[20], rep[20], ans[50];
int c=0, m=0, i=0, j=0, k, flag=0;
void stringmatch()
{
while(str[c] !='\0')
{
if(str[m] == pat[i])
{
i++;
m++;
if(pat[i] == '\0')
{
flag = 1;
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
OUTPUT
CASE 1:
CASE 2:
int s[MAX];
int top = -1;
void main()
{
int choice, item;
while(1)
{
printf("\n\n\n\n~~~~~~Menu~~~~~~ : ");
printf("\n=>1.Push an Element to Stack and Overflow demo ");
printf("\n=>2.Pop an Element from Stack and Underflow demo");
printf("\n=>3.Palindrome demo ");
printf("\n=>4.Display ");
printf("\n=>5.Exit"); printf("\
nEnter your choice: ");
scanf("%d", &choice);
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
switch(choice)
{
case 1: printf("\nEnter an element to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2: item = pop();
if(item != -1)
printf("\nElement popped is: %d", item);
break;
case 3: palindrome();
break;
case 4: display();
break;
case 5: exit(1);
default: printf("\nPlease enter valid choice ") ;
break;
}
}
}
top = top + 1 ;
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
s[top] = item;
}
int pop()
{
int item;
if(top == -1)
{
printf("\n~~~~Stack underflow~~~~");
return -1;
}
item = s[top];
top = top - 1;
return item;
}
void display()
{
int i;
if(top == -1)
{
printf("\n~~~~Stack is empty~~~~");
return;
}
printf("\nStack elements are:\n ");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
}
void palindrome()
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
{
int flag=1,i;
printf("\nStack content are:\n");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
OUTPUT:
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 20
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 29
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 30
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 98
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
| 20 |
Reverse of stack content are:
| 20 |
| 29 |
| 30 |
| 98 |
It is not a palindrome number
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 98
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice:
2
Element popped is: 30
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice:
2
Element popped is: 29
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice:
2
Element popped is: 20
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
void evaluate();
void
push(char); char
pop();
int prec(char);
void main()
{
printf("\nEnter the valid infix expression:\t");
scanf("%s", infix);
evaluate();
printf("\nThe entered infix expression is :\n %s \n", infix); printf("\
nThe corresponding postfix expression is :\n %s \n", postfix);
}
void evaluate()
{
int i = 0, j = 0;
char symb, temp;
push('#');
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
char pop()
{
char item;
item = stack[top];
top = top-1;
return item;
}
switch(symb)
{
case '#' : p = -1;
break;
case '(' :
case ')' : p = 0;
break;
case '+' :
case '-' : p = 1;
break;
case '*' :
case '/' :
case '%' : p = 2;
break;
case '^' :
case '$' : p = 3;
break
;
}
return p;
}
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
OUTPUT:
/tmp/ZxDBrtCYLw.o
Enter the valid infix expression: (a+b)+c/d*e
(a+b)+c/d*e
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int pop()
{
int item;
item = s[top];
top = top-1;
return item;
}
void main()
{
printf("\nEnter a valid postfix expression:\n");
scanf("%s", postfix);
for(i=0; postfix[i]!='\0'; i++)
{
symb = postfix[i];
if(isdigit(symb))
{
push(symb - '0');
}
else
{
op2 = pop();
op1 = pop();
switch(symb)
{
case '+': push(op1+op2);
break;
case '-': push(op1-op2);
break;
case '*': push(op1*op2);
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
break;
case '/': push(op1/op2);
break;
case '%': push(op1%op2);
break;
case '$':
case '^': push(pow(op1, op2));
break;
default : push(0);
}
}
}
res = pop();
printf("\n Result = %d", res);
}
Output:
/tmp/ZxDBrtCYLw.o
Enter a valid postfix expression:
623+-382/+*2$3+
Result = 52
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
6 Develop 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.
CODE:
#include <stdio.h>
#include<stdlib.h>
#include<stdio_ext.h>
#define MAX 3
char cq[MAX];
int front = -1, rear = -1;
void insert(char);
void delete();
void display();
void main()
{
int ch;
char item;
while(1)
{
printf("\n\n~~Main Menu~~");
printf("\n==> 1. Insertion and Overflow Demo");
printf("\n==> 2. Deletion and Underflow Demo");
printf("\n==> 3. Display");
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
printf("\n==> 4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
fpurge(stdin);
switch(ch)
{
case 1: printf("\n\nEnter the element to be inserted: ");
scanf("%c", &item);
insert(item);
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\n\nPlease enter a valid choice");
}
}
}
else
rear = (rear+1)%MAX;
cq[rear] = item;
}
}
void delete()
{
char item;
if(front == -1)
{
printf("\n\n~~Circular Queue Underflow~~");
}
else
{
item = cq[front];
printf("\n\nDeleted element from the queue is: %c ",item );
front = (front+1)%MAX;
}
}
void display ()
{
int i ;
if(front == -1)
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
OUTPUT:
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 1
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 2
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 3
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 4
~~Circular Queue Overflow~~
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice:
2
Deleted element from the queue is: 1
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice:
3
Circular Queue contents
are: Front[1]-> 2 3 <-
[2]Rear
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 4
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
7. Develop a menu driven Program in C for the following operations on Singly Linked
List (SLL) of Student Data with the fields: USN, Name, Programme, 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
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
char usn[25],name[25],branch[25];
int sem;
long int phone;
struct node *link;
};
typedef struct node * NODE;
NODE create()
{
NODE snode;
snode = (NODE)malloc(sizeof(struct node));
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
if(snode == NULL)
{
printf("\nMemory is not available");
exit(1);
}
printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld",snode->usn, snode->name, snode->branch, &snode->sem,
&snode->phone);
snode->link=NULL;
count++;
return snode;
}
NODE insertfront()
{
NODE temp;
temp = create();
if(start ==
NULL)
{
return temp;
}
temp->link = start;
return temp;
}
NODE deletefront()
{
NODE temp;
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
if(start == NULL)
{
printf("\nLinked list is empty");
return NULL;
}
if(start->link == NULL)
{
printf("\nThe Student node with usn:%s is deleted ",start->usn);
count--;
free(start);
return NULL;
}
temp = start;
start = start->link;
printf("\nThe Student node with usn:%s is deleted",temp->usn);
count--;
free(temp);
return start;
}
NODE insertend()
{
NODE cur,temp;
temp = create();
if(start == NULL)
{
return temp;
}
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
cur = start;
while(cur->link !=NULL)
{
cur = cur->link;
}
cur->link = temp;
return start;
}
NODE deleteend()
{
NODE cur,prev;
if(start == NULL)
{
printf("\nLinked List is empty");
return NULL;
}
if(start->link == NULL)
{
printf("\nThe student node with the usn:%s is deleted",start->usn);
free(start);
count--;
return
NULL;
}
prev = NULL;
cur = start;
while(cur->link!=NULL)
{
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
prev = cur;
cur = cur->link;
}
void display()
{
NODE cur;
int num=1;
if(start == NULL)
{
}
printf("\n No of student nodes is %d \n",count);
}
void stackdemo()
{
int ch;
while(1)
{
printf("\n~~~Stack Demo using SLL~~~\n");
printf("\n1:Push operation \n2: Pop operation \n3: Display \n4:Exit \n");
printf("\nEnter your choice for stack demo");
scanf("%d",&ch);
switch(ch)
{
case 1: start = insertfront();
break;
case 2: start = deletefront();
break;
case 3: display();
break;
default : return;
}
}
return;
}
int main()
{
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
int ch,i,n;
while(1)
{
printf("\n~~~Menu~~~");
printf("\nEnter your choice for SLL operation \
n"); printf("\n1:Create SLL of Student Nodes");
printf("\n2:DisplayStatus"); printf("\
n3:InsertAtEnd"); printf("\n4:DeleteAtEnd");
printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");
printf("\n6:Exit \n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\nEnter the no of students: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
start = insertfront();
break;
case 2: display();
break;
case 5: stackdemo();
break;
case 6: exit(0);
}
}
}
OUTPUT:
/tmp/Ot0YdYmkYC.o
~~~Menu~~~
Enter your choice for SLL operation
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
~~~Menu~~~
Enter your choice for SLL operation
No of student nodes is 4
~~~Menu~~~
Enter your choice for SLL operation
~~~Menu~~~
Enter your choice for SLL operation
~~~Menu~~~
Enter your choice for SLL operation
~~~Menu~~~
Enter your choice for SLL operation
1:Push operation
2: Pop operation
3: Display
4:Exit
ec
8
7259854289
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
1:Push operation
2: Pop operation
3: Display
4:Exit
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
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
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
char usn[25],name[25],branch[25];
int sem;
long int phone;
struct node *link;
};
typedef struct node * NODE;
NODE create()
{
NODE snode;
snode = (NODE)malloc(sizeof(struct node));
if(snode == NULL)
{
printf("\nMemory is not available");
exit(1);
}
printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld",snode->usn, snode->name, snode->branch, &snode->sem,
&snode->phone);
snode->link=NULL;
count++;
return snode;
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
NODE insertfront()
{
NODE temp;
temp = create();
if(start ==
NULL)
{
return temp;
}
temp->link = start;
return temp;
}
NODE deletefront()
{
NODE temp;
if(start == NULL)
{
printf("\nLinked list is empty");
return NULL;
}
if(start->link == NULL)
{
printf("\nThe Student node with usn:%s is deleted ",start->usn);
count--;
free(start);
return NULL;
}
temp = start;
start = start->link;
printf("\nThe Student node with usn:%s is deleted",temp->usn);
count--;
free(temp);
return start;
}
NODE insertend()
{
NODE cur,temp;
temp = create();
if(start == NULL)
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
{
return temp;
}
cur = start;
while(cur->link !=NULL)
{
cur = cur->link;
}
cur->link = temp;
return start;
}
NODE deleteend()
{
NODE cur,prev;
if(start == NULL)
{
printf("\nLinked List is empty");
return NULL;
}
if(start->link == NULL)
{
printf("\nThe student node with the usn:%s is deleted",start->usn);
free(start);
count--;
return
NULL;
}
prev = NULL;
cur = start;
while(cur->link!=NULL)
{
prev = cur;
cur = cur->link;
}
void display()
{
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
NODE cur;
int num=1;
if(start == NULL)
{
void stackdemo()
{
int ch;
while(1)
{
printf("\n~~~Stack Demo using SLL~~~\n");
printf("\n1:Push operation \n2: Pop operation \n3: Display \n4:Exit \n");
printf("\nEnter your choice for stack demo");
scanf("%d",&ch);
switch(ch)
{
case 1: start = insertfront();
break;
case 2: start = deletefront();
break;
case 3: display();
break;
default : return;
}
}
return;
}
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
int main()
{
int ch,i,n;
while(1)
{
printf("\n~~~Menu~~~");
printf("\nEnter your choice for SLL operation \
n"); printf("\n1:Create SLL of Student Nodes");
printf("\n2:DisplayStatus"); printf("\
n3:InsertAtEnd"); printf("\n4:DeleteAtEnd");
printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");
printf("\n6:Exit \n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\nEnter the no of students: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
start = insertfront();
break;
case 2: display();
break;
case 5: stackdemo();
break;
case 6: exit(0);
}
}
}
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
OUTPUT:
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 1
Enter the no of Employees: 2
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 2
ENode:1||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|Phone
no:11111
ENode:2||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|Phone
no:22222
No of employee nodes is 2
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 3
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 2
ENode:1||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|Phone
no:11111
ENode:2||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|Phone
no:22222
ENode:3||SSN:333|Name:ccc|Department:dept3|Designation:des3|Salary:3000|Phone
no:33333
No of employee nodes is 3
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 5
des4
4000
44444
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
ENode:1||SSN:444|Name:ddd|Department:dept4|Designation:des4|Salary:4000|Phone
no:44444
ENode:2||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|Phone
no:11111
ENode:3||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|Phone
no:22222
ENode:4||SSN:333|Name:ccc|Department:dept3|Designation:des3|Salary:3000|Phone
no:33333
No of employee nodes is 4
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 4
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 6
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 2
ENode:1||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|Phone
no:11111
ENode:2||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|Phone
no:22222
No of employee nodes is 2
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 7
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8: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
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
CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define COMPARE(x, y) ( (x == y) ? 0 : (x > y) ? 1 : -1)
struct node
{
int coef;
int xexp, yexp, zexp;
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if(x == NULL)
{
printf("Running out of memory \n");
return NULL;
}
return x;
}
NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
{
NODE temp, cur;
temp = getnode();
temp->coef = coef;
temp->xexp = xexp;
temp->yexp = yexp;
temp->zexp = zexp;
cur = head->link;
while(cur->link != head)
{
cur = cur->link;
}
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
cur->link = temp;
temp->link = head;
return head;
}
while(temp != head)
{
printf("%dx^%dy^%dz^%d", temp->coef, temp->xexp, temp->yexp, temp-
>zexp);
temp = temp->link;
if(temp != head)
printf(" + ");
}
}
poly = head->link;
while(poly != head)
{
sum += poly->coef * pow(x,poly->xexp)* pow(y,poly->yexp) * pow(z,poly-
>zexp);
poly = poly->link;
}
return sum;
}
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;
} //switch ends here
break;
} //if ends here
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;
}
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
break;
}
if(a->zexp!=0 || b->zexp!=0)
{
switch(COMPARE(a->zexp,b->zexp))
{
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;
}
void main()
{
NODE head, head1, head2, head3;
int res, ch;
head = getnode(); /* For polynomial evalaution */
head1 = getnode(); /* To hold POLY1 */
head2 = getnode(); /* To hold POLY2 */
head3 = getnode(); /* To hold POLYSUM
*/
head->link=head;
head1->link=head1;
head2->link=head2;
head3->link=
head3;
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
while(1)
{
printf("\n~~~Menu~~~");
printf("\n1.Represent and Evaluate a Polynomial P(x,y,z)");
printf("\n2.Find the sum of two polynomials POLY1(x,y,z)");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n~~~~Polynomial evaluation P(x,y,z)~~~\n");
head = read_poly(head);
printf("\nRepresentation of Polynomial for evaluation: \n");
display(head);
res = poly_evaluate(head);
printf("\nResult of polynomial evaluation is : %d \n", res);
break;
OUTPUT:
~~~Menu~~~
1. Represent and Evaluate a Polynomial P(x,y,z)
2. Find the sum of two polynomials POLY1(x,y,z) and
POLY2(x,y,z) Enter your choice: 1
Enter the 1
term:
Coef = 6
Enter Pow(x) Pow(y) and Pow(z): 2 2 1
Enter the 2 term:
Coef = -4
Enter Pow(x) Pow(y) and Pow(z): 0 1 5
Enter the 3 term:
Coef = 3
Enter Pow(x) Pow(y) and Pow(z): 3 1 1
Enter the 4 term:
Coef = 2
Enter Pow(x) Pow(y) and Pow(z): 1 5 1
Enter the 5 term:
Coef = -2
Enter Pow(x) Pow(y) and Pow(z): 1 1 3
Representation of Polynomial for evaluation:
6x^2y^2z^1 + -4x^0y^1z^5 + 3x^3y^1z^1 + 2x^1y^5z^1 + -2x^1y^1z^3
Enter the value of x,y and z: 1 1 1
Result of polynomial evaluation is : 5
~~~Menu~~~
1. Represent and Evaluate a Polynomial P(x,y,z)
2. Find the sum of two polynomials POLY1(x,y,z) and
POLY2(x,y,z) Enter your choice: 2
Coef = 8
Enter Pow(x) Pow(y) and Pow(z): 4 4 4
Enter the 2 term:
Coef = 4
Enter Pow(x) Pow(y) and Pow(z): 4 2 1
Enter the 3 term:
Coef = 30
Enter Pow(x) Pow(y) and Pow(z): 0 1 0
Enter the 4 term:
Coef = 20
Enter Pow(x) Pow(y) and Pow(z): 0 0 1
Enter the 5 term:
Coef = 3
Enter Pow(x) Pow(y) and Pow(z): 0 0 0
Polynomial 2 is:
8x^4y^4z^4 + 4x^4y^2z^1 + 30x^0y^1z^0 + 20x^0y^0z^1 + 3x^0y^0z^0
~~~Menu~~~
1. Represent and Evaluate a Polynomial P(x,y,z)
2. Find the sum of two polynomials POLY1(x,y,z) and
POLY2(x,y,z) Enter your choice:3
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
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, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
CODE:
#include<stdio.h>
#include<stdlib.h>
struct BST
{
int data;
struct BST *lchild;
struct BST *rchild;
};
typedef struct BST * NODE;
NODE create()
{
NODE temp;
temp = (NODE) malloc(sizeof(struct BST));
printf("\nEnter The value: ");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild =
NULL; return temp;
}
if (root->lchild == NULL)
root->lchild = newnode;
else
insert(root->lchild, newnode);
}
if (newnode->data > root->data)
{
if (root->rchild == NULL)
root->rchild = newnode;
else
insert(root->rchild, newnode);
}
}
void main()
{
int ch, key, val, i, n;
NODE root = NULL, newnode;
while(1)
{
printf("\n~~~~BST MENU~~~~");
printf("\n1.Create a BST");
printf("\n2.Search"); printf("\
n3.BST Traversals: "); printf("\
n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter the number of elements:
"); scanf("%d", &n);
for(i=1;i<=n;i++)
{
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
}
break;
case 2: if (root == NULL)
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
break;
case 3: search(root);
break;
case 4: exit(0);
}
}
}
OUTPUT:
~~~~BST MENU~~~~
1.Create a BST
2.Search
3. BST Traversals:
4. Exit
Enter your choice: 1
~~~~BST MENU~~~~
1.Create a BST
2.Search
3. BST
Traversals:
4. Exit
Enter your choice: 3
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
~~~~BST MENU~~~~
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 2
~~~~BST MENU~~~~
1.Create a BST
2.Search
3. BST Traversals:
4. Exit
Enter your choice: 2
~~~~BST MENU~~~~
1.Create a BST
2.Search
3. BST
Traversals:
4. Exit
Enter your choice: 4
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
CODE:
#include<stdio.h>
#include<stdlib.h>
void bfs(int v)
{
int i, cur;
visited[v] = 1;
q[++rear] = v;
while(front!=rear)
{
cur = q[++front];
for(i=1;i<=n;i++)
{
if((a[cur][i]==1)&&(visited[i]==0))
{
q[++rear] = i;
visited[i] = 1;
printf("%d ", i);
}
}
}
}
void dfs(int v)
{
int i;
visited[v]=1; s[+
+top] = v;
for(i=1;i<=n;i++)
{
if(a[v][i] == 1&& visited[i] == 0 )
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
{
printf("%d ", i);
dfs(i);
}
}
}
int main()
{
for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the starting vertex: ");
scanf("%d",&start);
printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
printf("\n==>2. DFS: Print all nodes reachable from a given starting
node"); printf("\n==>3:Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\nThe vertex that is not reachable is %d" ,i);
}
break;
OUTPUT:
Case 1:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 1
Enter the starting vertex: 1
Nodes reachable from starting vertex 1 are: 2 4 3
Case 2:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
Case 3:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 2
Enter the starting vertex: 1
Nodes reachable from starting vertex 1 are: 2 3 4
Case 4:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 2
Enter the starting vertex: 2
Nodes reachable from starting vertex 2 are: 3 4
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 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.
CODE:
#include<stdio.h>
#include<stdlib.h>
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
int key[20],n,m;
int *ht,index;
int count = 0;
void display()
{
int i;
if(count == 0)
{
printf("\nHash Table is
empty"); return;
}
void main()
{
int i;
printf("\nEnter the number of employee records (N) : ");
scanf("%d", &n);
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;
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
for(i=0; i<n; i++)
scanf("%d", &key[i]);
DATA DTRUCTURE LAB MANUL(BCSL305) 2023-24
for(i=0;i<n;i++)
{
if(count == m)
{
printf("\n~~~Hash table is full. Cannot insert the record %d key~~~",i+1);
break;
}
insert(key[i]);
}
OUTPUT:
Enter the number of employee records (N) : 12
Enter the two digit memory locations (m) for hash table: 15
Enter the four digit key values (K) of 'N' Employee Records:
1234
5678
3456
2345
6799
1235
7890
3214
3456
1235
5679
2346