Dsa Lab
Dsa Lab
2. #include <stdlib.h>
3. struct Day {
4. char *dayName;
5. int date;
6. char *activity; 7. };
17. }
21. create(&calendar[i]);
22. }
23. }
31. printf("\n");
32. }
33. }
36. free(calendar[i].dayName);
37. free(calendar[i].activity);
38. }
39. }
47. return 1;
48. }
52. free(calendar);
53. return 0;
54. }
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: 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:
Activity: Earning
PROGRAM 2
Code :
1. #include<stdio.h>
8. m++;
9. if (pat[i] == '\0') {
10. flag = 1;
13. }
14. i = 0;
15. c = m;
16. }
17. }
18. else {
21. c++;
22. m = c; 23. i = 0;
24. }
25. }
27. }
30. gets(str);
32. gets(pat);
34. gets(rep);
36. stringmatch();
37. if (flag == 1)
38. printf("\nThe string after pattern match and replace is: \n %s ", res);
39. else
Output :
*************************OUTPUT 1******************************
The string after pattern match and replace is: Designed by Braham
*************************OUTPUT 2******************************
Enter the replace string:Braham The string before pattern match is: Designed by Developer
PROGRAM 3
Design, Develop and Implement a menu driven Program in C for the
following operations on STACKof Integers (Array Implementation of
Stack with maximum size MAX):
A. Push an Element on to Stack.
B. Pop an Element from Stack.
C. Demonstrate how Stack can be used to check Palindrome.
D. Demonstrate Overflow and Underflow situations on Stack.
E. Display the status of Stack.
F. Exit
A. Support the program with appropriate functions for each of the
above operations.
Code :
1. #include<stdio.h>
2. #include<stdlib.h>
3. #define MAX 3
4. int s[MAX];
7. int pop();
8. void palindrome();
9. void display();
18. printf("\n=>5.Exit");
22. case 1:
25. push(item);
26. break;
27. case 2:
31. break;
32. case 3:
33. palindrome();
34. break;
35. case 4:
36. display();
37. break;
38. case 5:
39. exit(1);
40. default:
42. break;
43. }
44. }
45. }
49. return;
50. }
53. }
59. }
63. }
65. int i;
68. return;
69. }
73. }
84. flag = 0;
85. break;
86. }
87. }
90. }
91. else {
93. }
94. }
Output :
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Stack overflow-----------
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
| 13 |
| 12 |
| 11 |
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
| 12 |
| 11 |
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Stack underflow-----------
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Stack is empty-----------
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
| 11 |
| 22 |
| 11 |
| 11 |
| 22 |
| 11 |
It is palindrome number
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
=>3.Palindrome demo
=>4.Display
=>5.Exit
| 22 |
| 33 |
| 11 |
| 11 |
| 33 |
| 22 |
-----------Menu----------- :
=>3.Palindrome demo
=>4.Display
=>5.Exit
2. void push(char);
3. char pop();
8. scanf("%s", infix);
9. evaluate();
13. {
14. int i = 0, j = 0;
16. {
19. {
25. {
29. }
30. break;
39. {
43. }
44. push(symb);
45. break;
46. default:
49. }
50. }
52. {
56. }
63. {
70. int p;
72. {
88. break;
89. }
90. return p;
91. }
Output :
Enter the valid infix expression: a+b The entered infix expression is :
a+b
Code :
1. #include<stdio.h>
2. #include<stdlib.h>
3. #include<math.h>
7. top = top + 1;
8. s[top] = item; 9. }
15. }
21. if (isdigit(symb)) {
23. }
24. Else {
33. break;
36. break;
37. case '/':
39. break;
42. break;
46. break;
47. default:
48. push(0);
49. }
50. }
51. }
54. }
Output :
Enter a valid postfix expression: 623+-382/+*2$3+
Result = 52
2. #include<stdlib.h>
3. struct node{
4. char ssn[25], name[25], dept[10], designation[25];
5. int sal;
18. exit(0);
19. }
\n");
21. scanf("%s %s %s %s %d %ld", enode -> ssn, enode -> name, enode -> dept, enode ->
designation, & enode -> sal, & enode -> phone);
24. count++;
26. }
32. }
36. }
|Phone no:%ld|", nodeno, cur -> ssn, cur -> name, cur -> dept, cur -> designation, cur -> sal, cur ->
phone);
46. nodeno++;
47. }
49. }
55. }
57. printf("\nThe employee node with the ssn:%s is deleted ", first -> ssn);
58. free(first);
59. count--;
61. }
66. printf("\nThe employee node with the ssn:%s is deleted ", temp -> ssn);
67. free(temp);
68. count--;
70. }
76. }
80. }
84. }
90. }
92. printf("\nThe employee node with the ssn:%s is deleted ", first -> ssn);
93. free(first);
94. count--;
96. }
102. }
104. printf("\nThe employee node with the ssn:%s is deleted ", cur -> ssn);
105. free(cur);
107. count--;
109. }
117. case 1:
119. break;
120. case 2:
122. break;
123. case 3:
125. break;
126. case 4:
128. break;
129. case 5:
130. display();
131. break;
132. default:
133. return;
134. }
135. }
136. }
140. {
141. printf("\n\n--------Menu--------");
143. printf("\n2:DisplayStatus");
144. printf("\n3:InsertAtEnd");
145. printf("\n4:DeleteAtEnd");
146. printf("\n5:InsertAtFront");
147. printf("\n6:DeleteAtFront");
153. case 1:
158. break;
159. case 2:
160. display();
161. break;
162. case 3:
164. break
165. ;case 4:
167. break;
168. case 5:
170. break;
171. case 6:
173. break
174. ;case 7:
175. deqdemo();
176. break;
177. case 8:
178. exit(0);
179. default:
182. }
183. }
Output :
--------Menu--------
8476283712
2763578156
--------Menu--------
--------Menu--------
8237462936
--------Menu--------
--------Menu--------
4EPL
Shoaib
40000
2835826437
--------Menu--------
7:Double Ended Queue Demo using DLL 8:ExitPlease enter your choice: 2
|ENode:1| |SSN:4EPL| |Name:Shoaib| |Department:Digital Marketing| |Designation:Manager|
--------Menu--------
--------Menu--------
--------Menu--------
6: Exit
Please enter your choice: 2
6: Exit
6: Exit
6: Exit
--------Menu--------
3:InsertAtEnd
4:DeleteAtEnd 5:InsertAtFront 6:DeleteAtFront
7:Double Ended Queue Demo using DLL 8:ExitPlease enter your choice: 8
PROGRAM 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
CODE
1. #include<stdio.h>
2. #include<stdlib.h>
3. #define MAX 5
4. char circular_queue[MAX];
5. int front = -1, rear = -1;
6. int isEmpty() {
7. if (front == -1 && rear == -1)
8. return 1;
9. else
10. return 0;
11. }
12. int isFull() {
13. if ((rear + 1) % MAX == front)
14. return 1;
15. else
16. return 0;
17. }
18. void insertElement(char element) {
19. if (isFull()){
20. printf("Circular Queue Overflow\n");
21. return;
22. }
23. else if (isEmpty()) {
24. front = rear = 0;
25. }
26. else {
27. rear = (rear + 1) % MAX;
28. }
29. circular_queue[rear] = element;
30. }
31. void deleteElement() {
32. if (isEmpty()) {
33. printf("Circular Queue Underflow\n");
34. return;
35. }
36. else if (front == rear) {
37. front = rear = -1;
38. }
39. else {
40. front = (front + 1) % MAX;
41. }
42. }
43. void display() {
44. int i;
45. if (isEmpty()) {
46. printf("Circular Queue is empty\n");
47. return;
48. }
49. printf("Circular Queue elements: ");
50. i = front;
51. do {
52. printf("%c ", circular_queue[i]); 66. i = (i + 1) % MAX;
53. } while (i != (rear + 1) % MAX);
54. printf("\n");
55. }
56. int main() {
57. int choice;
58. char element;
59. do {
60. printf("\n\n---- Circular Queue Menu ----\n");
61. printf("1. Insert an Element\n");
62. printf("2. Delete an Element\n");
63. printf("3. Display Circular Queue\n");
64. printf("4. Exit\n");
65. printf("Enter your choice: ");
66. scanf("%d", &choice);
67. switch(choice) {
68. case 1:
69. printf("Enter element to be inserted: ");
70. scanf(" %c", &element);
71. insertElement(element);
72. break;
73. case 2:
74. deleteElement();
75. break;
76. case 3:
77. display();
78. break;
79. case 4:
80. printf("Exiting...\n");
81. break;
82. default:
83. printf("Invalid choice! Please enter a valid option.\n");
84. }
a. } while(choice != 4);
85. return 0;
a. }
OUTPUT
---- Circular Queue Menu ----
1. Insert an Element
2. Delete an Element
4. Exit
1. Insert an Element
2. Delete an Element
4. Exit
1. Insert an Element
2. Delete an Element
1. Insert an Element
2. Delete an Element
4. Exit
1. Insert an Element
2. Delete an Element
4. Exit
1. Insert an Element
2. Delete an Element
4. Exit
1. Insert an Element
2. Delete an Element
4. Exit
Exiting...
PROGRAM 10
Design, Develop and Implement a menu driven Program in C for the
following operations onBinary 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 :
1. #include<stdio.h>
2. #include<stdlib.h>
3. struct BST {
4. int data;
5. struct BST * lchild;
6. struct BST * rchild;
7. };
8. typedef struct BST * NODE;
9. NODE create(){
10. NODE temp;
11. temp = (NODE) malloc(sizeof(struct BST));
12. printf("\nEnter The value: ");
13. scanf("%d", & temp -> data);
14. temp -> lchild = NULL;
15. temp -> rchild = NULL;
16. return temp;
17. }
18. void insert(NODE root, NODE newnode);
19. void inorder(NODE root);
20. void preorder(NODE root);
21. void postorder(NODE root);
22. void search(NODE root);
23. void insert(NODE root, NODE newnode) {
24. if (newnode -> data < root -> data) {
25. if (root -> lchild == NULL)
26. root -> lchild = newnode;
27. else
28. insert(root -> lchild, newnode);
29. }
30. if (newnode -> data > root -> data){
31. if (root -> rchild == NULL)
32. root -> rchild = newnode;
33. else
34. insert(root -> rchild, newnode);
35. }
36. }
37. void search(NODE root) {
38. int key;
39. NODE cur;
40. if (root == NULL){
41. printf("\nBST is empty.");
42. return;
43. }
44. printf("\nEnter Element to be searched: ");
45. scanf("%d", & key);
46. cur = root;
47. while (cur != NULL){
48. if (cur -> data == key) {
49. printf("\nKey element is present in BST ");
50. return;
51. }
52. if (key < cur -> data)
53. cur = cur -> lchild;
54. else
55. cur = cur -> rchild;
56. }
57. printf("\nKey element is not found in the BST ");
58. }
59. void inorder(NODE root){
60. if (root != NULL) {
61. inorder(root -> lchild);
62. printf("%d ", root -> data);
63. inorder(root -> rchild);
64. }
65. }
66. void preorder(NODE root) {
67. if (root != NULL) {
68. printf("%d ", root -> data);
69. preorder(root -> lchild);
70. preorder(root -> rchild);
71. }
72. }
73. void postorder(NODE root) {
74. if (root != NULL){
75. postorder(root -> lchild);
76. postorder(root -> rchild);
77. printf("%d ", root -> data);
78. }
79. }
80. void main(){
81. int ch, key, val, i, n;
82. NODE root = NULL, newnode;
83. while (1) {
84. printf("\n-------BST MENU-------");
85. printf("\n1.Create a BST ");
86. printf("\n2.Search ");
87. printf("\n3.BST Traversals: ");
88. printf("\n4.Exit");
89. printf("\nEnter your choice: ");
90. scanf("%d", & ch);
91. switch (ch) {
92. case 1:
93. printf("\nEnter the number of elements: ");
a. scanf("%d", & n); 113.
b. for (i = 1; i <= n; i++){
c. newnode = create();
d. if (root == NULL)
e. root = newnode;
f. else
g. insert(root, newnode);
h. }
94. break;
95. case 2:
96. if (root == NULL)
a. printf("\nTree Is Not Created ");
97. else. {
a. printf("\nThe Preorder display: ");
b. preorder(root);
c. printf("\nThe Inorder display: ");
d. inorder(root);
e. printf("\nThe Postorder display: ");
f. postorder(root);
98. }
99. break;
100. case 3:
101. search(root);
102. break;
103. case 4:
104. exit(0);
a. }
b. }
OUTPUT :
-------BST MENU-------
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
-------BST MENU-------
-------BST MENU-------
-------BST MENU-------
PROGRAM 11
Design, Develop and Implement a Program in C for the following
operations on Graph(G)of Cities:
OUTPUT
*************************case-1*************************
Enter the number of vertices in graph:4 Enter the adjacency matrix:
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
Nodes reachable from starting vertex 2 are: 3 4 The vertex that is not reachable is 1
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