Final DSA Lab
Final DSA Lab
Final DSA Lab
#include <stdio.h>
#include <stdlib.h>
struct Day {
char * dayName;
int date;
char * activity;
};
// Assuming activity descriptions are less than 100 characters // Input the day details
scanf("%d", & day -> date); printf("Enter the activity for the day:");
scanf(" %[^\n]s", day -> activity); // Read the entire line, including spaces
} // Function to read data from the keyboard and create the calendar
printf("\n");
free(calendar[i].dayName);
free(calendar[i].activity);
} int main() {
int size;
struct Day * calendar = (struct Day * ) malloc(sizeof(struct Day) * size); // Check if memory
allocation is successful
if (calendar == NULL) {
return 1;
read(calendar, size);
freeMemory(calendar, size); // Free the memory allocated for the calendar array
free(calendar); return 0;
Output
Enter the number of days in the week: 7 Enter details for Day 1:
Enter the activity for the day: Learning Enter details for Day 2:
Enter the activity for the day: Coding Enter details for Day 3:
Enter the activity for the day: Testing Enter details for Day 4:
Enter the activity for the day: Debugging Enter details for Day 5:
Enter the activity for the day: Publishing Enter details for Day 6:
Enter the activity for the day: Marketing Enter details for Day 7:
Enter the activity for the day: Earning Week's Activity Details:
Day 1:
Date: 1
Date: 2
Date: 3
Activity: Testing Day 4:
Date: 4
Date: 5
Date: 6
Date: 7
Activity: Earning
2)
int c = 0, m = 0, i = 0, j = 0, k, flag = 0;
void stringmatch() {
if (str[m] == pat[i]) {
i++;
m++;
if (pat[i] == '\0') {
flag = 1;
res[j] = rep[k];
i = 0;
c = m;
} else {
res[j] = str[c];
j++;
c++;
m = c;
i = 0;
res[j] = '\0';
void main() {
gets(str);
gets(pat);
gets(rep);
stringmatch();
if (flag == 1)
printf("\nThe string after pattern match and replace is: \n %s ", res);
else
O/P
Enter the main string:Designed by vtucode Enter the pat string:vtucode Enter the replace
string:Braham The string before pattern match is:
Designed by vtucode
Designed by Braham
Enter the main string:Designed by Developer Enter the pat string:vtucode Enter the replace
string:Braham The string before pattern match is:
Designed by Developer
Pattern string is not found
3)
#include<stdio.h> #include<stdlib.h> #define MAX 3 //you can change this size according to your
requirement int s[MAX];
int pop();
void palindrome();
while (1) {
printf("\n\n\n\n~~~~~~Menu~~~~~~ : ");
printf("\n=>4.Display ");
printf("\n=>5.Exit");
switch (choice) {
case 1:
push(item);
break;
case 2:
item = pop();
if (item != -1)
break;
case 3:
palindrome();
break;
case 4:
display();
break;
case 5:
exit(1);
default:
break;
if (top == MAX - 1) {
printf("\n~~~~Stack overflow~~~~");
return;
} top = top + 1;
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;
} void palindrome() {
int flag = 1, i;
flag = 0;
break;
if (flag == 1) {
} else {
O/P
~~~~~~Menu~~~~~~ :
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
| 13 |
| 12 |
| 11 | ~~~~~~Menu~~~~~~ :
=>4.Display
=>3.Palindrome demo
=>4.Display
| 12 |
| 11 | ~~~~~~Menu~~~~~~ :
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
| 11 |
| 22 |
| 11 | Reverse of stack content are:
| 11 |
| 22 |
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
=>3.Palindrome demo
=>4.Display
| 22 |
| 33 |
| 11 |
| 33 |
=>3.Palindrome demo
=>4.Display
4)
void push(char);
char pop();
scanf("%s", infix);
evaluate();
} void evaluate() {
int i = 0, j = 0;
symb = infix[i];
switch (symb) {
case '(':
push(symb);
temp = pop();
postfix[j] = temp;
j++;
temp = pop();
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
case '$':
temp = pop();
postfix[j] = temp;
j++;
push(symb);
break;
default:
postfix[j] = symb;
j++;
postfix[j] = temp;
j++;
postfix[j] = '\0';
top = top + 1;
stack[top] = item;
} char pop() {
char item;
item = stack[top];
top = top - 1;
return item;
int p;
switch (symb) {
case '#':
p = -1;
case ')':
p = 0;
case '-':
p = 1;
case '/':
case '%':
p = 2;
case '$':
p = 3;
break;
return p;
O/P
ab+c*d5^/1%
5)
top = top + 1;
s[top] = item;
} int pop() {
int item;
item = s[top];
top = top - 1;
return item;
} void main() {
scanf("%s", postfix);
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);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
case '$':
case '^':
push(pow(op1, op2));
break;
default:
push(0);
res = pop();
O/P
Enter a valid postfix expression:
623+-382/+*2$3+
Result = 52
5b)
#include <stdio.h> void tower(int n, int source, int temp, int destination) {
if (n == 0)
return;
void main() {
int n;
O/P
6)
#include <stdio.h> #include<stdlib.h> #include<stdio_ext.h> #define MAX 3 char cq[MAX];
void delete();
void display();
void main() {
int ch;
char item;
while (1) {
printf("\n\n~~Main Menu~~");
printf("\n==> 3. Display");
printf("\n==> 4. Exit");
__fpurge(stdin);
switch (ch) {
case 1:
insert(item);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
} else {
if (front == -1)
front = rear = 0;
else
cq[rear] = item;
} void delete() {
char item;
if (front == -1) {
} else {
item = cq[front];
printf("\n\nDeleted element from the queue is: %c ", item); if (front == rear) //only one element
else
} void display() {
int i;
if (front == -1) {
} else {
O/P
~~Main Menu~~
==> 3. Display
==> 3. Display
==> 3. Display
==> 3. Display
==> 3. Display
==> 3. Display
==> 3. Display
==> 3. Display
7)
int sem;
NODE snode;
exit(1);
scanf("%s %s %s %d %ld", snode -> usn, snode -> name, snode -> branch, & snode -> sem, & snode -
> phone);
count++;
return snode;
} NODE insertfront() {
NODE temp;
temp = create();
if (start == NULL) {
return temp;
return temp;
} NODE deletefront() {
NODE temp;
if (start == NULL) {
return NULL;
printf("\nThe Student node with usn:%s is deleted ", start -> usn);
count--;
free(start);
return NULL;
}
temp = start;
count--;
free(temp);
return start;
} NODE insertend() {
return temp;
cur = start;
return start;
} NODE deleteend() {
if (start == NULL) {
return NULL;
printf("\nThe student node with the usn:%s is deleted", start -> usn);
free(start);
count--;
return NULL;
} prev = NULL;
cur = start;
} printf("\nThe student node with the usn:%s is deleted", cur -> usn);
free(cur);
count--;
return start;
} void display() {
NODE cur;
return;
cur = start;
printf("\n||%d|| USN:%s| Name:%s| Branch:%s| Sem:%d| Ph:%ld|", num, cur -> usn, cur -> name,
cur -> branch, cur -> sem, cur -> phone);
num++;
} void stackdemo() {
int ch;
while (1) {
case 1:
start = insertfront();
break;
case 2:
start = deletefront();
break;
case 3:
display();
break;
default:
return;
return;
} int main() {
int ch, i, n;
while (1) {
printf("\n~~~Menu~~~");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n6:Exit \n");
case 1:
start = insertfront();
break; case 2:
display();
break; case 3:
start = insertend();
break; case 4:
start = deleteend();
break; case 5:
stackdemo();
break; case 6:
exit(0); default:
O/P
~~~Menu~~~
Enter your choice for SLL operation 1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
1ME21CS017
Braham
CSE
1ME21CS015
Bikash
CSE
Shoaib
AI&ML
6748353877 ~~~Menu~~~
Enter your choice for SLL operation 1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
Enter your choice for SLL operation 1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
6:Exit Enter your choice:3 Enter the usn,Name,Branch, sem,PhoneNo of the student:
1ME21CS068
Rajan
CSE
3426527765 ~~~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)
Enter your choice for SLL operation 1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
Enter your choice for SLL operation 1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
Enter your choice for SLL operation 1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
Enter your choice for SLL operation 1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
2: Pop operation
3: Display
4:Exit Enter your choice for stack demo:1 Enter the usn,Name,Branch, sem,PhoneNo of the student:
1ME21CS005
Aman
CSE
2: Pop operation
3: Display
2: Pop operation
3: Display
4: Exit Enter your choice for stack demo:1 Enter the usn,Name,Branch, sem,PhoneNo of the
student:
1ME21CS092
Shubham
CSE
5
9869754354 ~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
2: Pop operation
3: Display
The Student node with usn:1ME21CS092 is deleted ~~~Stack Demo using SLL~~~ 1:Push operation
2: Pop operation
3: Display
2: Pop operation
3: Display
Enter your choice for SLL operation 1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
int sal;
};
NODE enode;
if (enode == NULL) {
exit(0);
scanf("%s %s %s %s %d %ld", enode -> ssn, enode -> name, enode -> dept, enode -> designation, &
enode -> sal, & enode -> phone);
count++;
return enode;
} NODE insertfront() {
NODE temp;
temp = create();
if (first == NULL) {
return temp;
}
temp -> rlink = first;
return temp;
} void display() {
NODE cur;
int nodeno = 1;
cur = first;
if (cur == NULL)
printf("\nENode:%d||SSN:%s|Name:%s|Department:%s|Designation:%s|Salary:%d|Phone no:%ld",
nodeno, cur -> ssn, cur -> name, cur -> dept, cur -> designation, cur -> sal, cur -> phone);
nodeno++;
} NODE deletefront() {
NODE temp;
if (first == NULL) {
return NULL;
printf("\nThe employee node with the ssn:%s is deleted", first -> ssn);
free(first);
count--;
return NULL;
temp = first;
printf("\nThe employee node with the ssn:%s is deleted", temp -> ssn);
free(temp);
count--;
return first;
} NODE insertend() {
return temp;
cur = first;
return first;
} NODE deleteend() {
if (first == NULL) {
return NULL;
printf("\nThe employee node with the ssn:%s is deleted", first -> ssn);
free(first);
count--;
return NULL;
} prev = NULL;
prev = cur;
free(cur);
count--;
return first;
} void deqdemo() {
int ch;
while (1) {
case 1:
first = insertfront();
break;
case 2:
first = deletefront();
break;
case 3:
first = insertend();
break;
case 4:
first = deleteend();
break;
case 5:
display();
break;
default:
return;
}
} void main() {
int ch, i, n;
while (1) {
printf("\n\n~~~Menu~~~");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront");
printf("\n6:DeleteAtFront");
printf("\n8:Exit \n");
case 1:
first = insertend();
break; case 2:
display();
break; case 3:
first = insertend();
break; case 4:
first = deleteend();
break; case 5:
first = insertfront();
break; case 6:
first = deletefront();
break; case 7:
deqdemo();
break; case 8:
exit(0);
default:
O/P
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
1EPL
Braham
Developer
Senior
13627
2EPL
Aman
Trader
Manager
20000
2763578156 ~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
ENode:2||SSN:2EPL|Name:Aman|Department:Trader|Designation:Manager|Salary:20000|Phone
no:2763578156
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
3EPL
Bikash
Meeting
Manager
30000
8237462936 ~~~Menu~~~
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
ENode:2||SSN:2EPL|Name:Aman|Department:Trader|Designation:Manager|Salary:20000|Phone
no:2763578156
ENode:3||SSN:3EPL|Name:Bikash|Department:Meeting|Designation:Manager|Salary:30000|Phone
no:8237462936
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
4EPL
Shoaib
Digital Marketing
Manager
40000
2835826437 ~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
ENode:2||SSN:1EPL|Name:Braham|Department:Developer|Designation:Senior|Salary:13627|
Phone no:8476283712
ENode:3||SSN:2EPL|Name:Aman|Department:Trader|Designation:Manager|Salary:20000|Phone
no:2763578156
ENode:4||SSN:3EPL|Name:Bikash|Department:Meeting|Designation:Manager|Salary:30000|Phone
no:8237462936
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
Please enter your choice: 4 The employee node with the ssn:3EPL is deleted ~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
Please enter your choice: 6 The employee node with the ssn:4EPL is deleted ~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
ENode:2||SSN:2EPL|Name:Aman|Department:Trader|Designation:Manager|Salary:20000|Phone
no:2763578156
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
Please enter your choice: 2 The employee node with the ssn:1EPL is deleted Demo Double Ended
Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
Please enter your choice: 4 The employee node with the ssn:2EPL is deleted Demo Double Ended
Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
Please enter your choice: 2 Doubly Linked List is empty Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
9)
#include<stdio.h> #include<stdlib.h> #include<math.h> #define COMPARE(x, y)((x == y) ? 0 : (x >
y) ? 1 : -1) struct node {
int coef;
};
NODE x;
if (x == NULL) {
return NULL;
return x;
} NODE attach(int coef, int xexp, int yexp, int zexp, NODE head) {
temp = getnode();
return head;
printf("\n\t\tCoef = ");
return head;
NODE temp;
return;
printf("%dx^%dy^%dz^%d", temp -> coef, temp -> xexp, temp -> yexp, temp -> zexp);
if (temp != head)
printf(" + ");
int x, y, z, sum = 0;
scanf("%d %d %d", & x, & y, & z); poly = head -> link;
sum += poly -> coef * pow(x, poly -> xexp) * pow(y, poly -> yexp) * pow(z, poly -> zexp);
return sum;
NODE a, b;
int coef;
while (1) {
if (a -> xexp == b -> xexp && a -> yexp == b -> yexp && a -> zexp == b -> zexp) {
a = a -> link;
b = b -> link;
break;
case -1:
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break; case 0:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
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;
case -1:
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
case 0:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
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;
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;
printf("\n~~~Menu~~~");
switch (ch) {
case 1:
head = read_poly(head);
display(head);
res = poly_evaluate(head);
break; case 2:
head1 = read_poly(head1);
head2 = read_poly(head2);
break;
case 3:
exit(0);
O/P
~~~Menu~~~
Coef = 6
Coef = -4
Coef = 3
Coef = 2
Coef = -2
Coef = 6
Coef = 3
Coef = 5
Coef = 10
Coef = 5
Polynomial 1 is:
Coef = 8
Coef = 4
Coef = 30
Coef = 20
Coef = 3
Polynomial 2 is:
10)
int data;
};
NODE temp;
return temp;
} void insert(NODE root, NODE newnode);
else
else
int key;
NODE cur;
if (root == NULL) {
printf("\nBST is empty.");
return;
cur = root;
else
if (root != NULL) {
if (root != NULL) {
if (root != NULL) {
} void main() {
while (1) {
printf("\n~~~~BST MENU~~~~");
printf("\n1.Create a BST");
printf("\n2.Search");
printf("\n4.Exit");
switch (ch) {
case 1:
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
break;
case 2:
if (root == NULL)
else {
preorder(root);
inorder(root);
postorder(root);
} break;
case 3:
search(root);
break; case 4:
exit(0);
O/P
~~~~BST MENU~~~~
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
11)
int i, cur;
visited[v] = 1;
q[++rear] = v;
cur = q[++front];
for (i = 1; i <= n; i++) {
q[++rear] = i;
visited[i] = 1;
} void dfs(int v) {
int i;
visited[v] = 1;
s[++top] = v;
dfs(i);
visited[i] = 0;
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: ");
switch (ch) {
case 1:
bfs(start);
if (visited[i] == 0)
break; case 2:
dfs(start);
break;
case 3:
exit(0);
default:
O/P
0101
0010
0001
0000
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
0101
0010
0001
0000
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
0101
0010
0001
0000
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
0010
0001
0000
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
12)
index = key % m;
index = (index + 1) % m;
ht[index] = key;
count++;
} void display() {
int i;
if (count == 0) {
return;
} void main() {
int i;
scanf("%d", & n); printf("\nEnter the two digit memory locations (m) for hash table:");
ht[i] = -1; printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
if (count == m) {
break;
insert(key[i]);
display();
O/P
Enter the two digit memory locations (m) for hash table:15
Enter the four digit key values (K) for N Employee Records:
4020
4560
9908
6785
0423
7890
6547
3342
9043
6754
T[6] --> -1
T[9] --> -1
T[10] --> -1
T[11] --> -1
T[14] --> -1