0% found this document useful (0 votes)
6 views61 pages

Dsa Lab

The document contains three C programs. The first program creates a calendar for a week with dynamic memory allocation for day names and activities. The second program performs string pattern matching and replacement without using built-in functions, and the third program implements a stack with operations for pushing, popping, checking for palindromes, and handling overflow/underflow situations.

Uploaded by

anikethhn2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views61 pages

Dsa Lab

The document contains three C programs. The first program creates a calendar for a week with dynamic memory allocation for day names and activities. The second program performs string pattern matching and replacement without using built-in functions, and the third program implements a stack with operations for pushing, popping, checking for palindromes, and handling overflow/underflow situations.

Uploaded by

anikethhn2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

PROGRAM 1 :

Declare a calendar as an array of 7 elements (A dynamically Created


array) to represent 7 days of a week. Each Element of the array is a
structure having three fields. The first field is the name of the Day (A
dynamically allocated String), The second field is the date of the Day
(A integer), the third field is the description of the activity for a
particular day (A dynamically allocated String).
Write functions create(), read() and display(); to create the calendar,
to read the data from the keyboard and to print weeks activity details
report on screen.
Code :
1. #include <stdio.h>

2. #include <stdlib.h>

3. struct Day {

4. char *dayName;

5. int date;

6. char *activity; 7. };

8. void create(struct Day *day) {

9. day->dayName = (char *)malloc(sizeof(char) * 20);

10. day->activity = (char *)malloc(sizeof(char) * 100);

11. printf("Enter the day name: ");

12. scanf("%s", day->dayName);

13. printf("Enter the date: ");

14. scanf("%d", &day->date);

15. printf("Enter the activity for the day: ");

16. scanf(" %[^\n]s", day->activity);

17. }

18. void read(struct Day *calendar, int size) {

19. for (int i = 0; i < size; i++) {

20. printf("Enter details for Day %d:\n", i + 1);

21. create(&calendar[i]);

22. }

23. }

24. void display(struct Day *calendar, int size) {

25. printf("\nWeek's Activity Details:\n");

26. for (int i = 0; i < size; i++) {


27. printf("Day %d:\n", i + 1);

28. printf("Day Name: %s\n", calendar[i].dayName);


29. printf("Date: %d\n", calendar[i].date);

30. printf("Activity: %s\n", calendar[i].activity);

31. printf("\n");

32. }

33. }

34. void freeMemory(struct Day *calendar, int size) {

35. for (int i = 0; i < size; i++) {

36. free(calendar[i].dayName);

37. free(calendar[i].activity);

38. }

39. }

40. int main() {

41. int size;

42. printf("Enter the number of days in the week: ");

43. scanf("%d", &size);

44. struct Day *calendar = (struct Day *)malloc(sizeof(struct Day) * size);

45. if (calendar == NULL) {

46. printf("Memory allocation failed. Exiting program.\n");

47. return 1;

48. }

49. read(calendar, size);

50. display(calendar, size);

51. freeMemory(calendar, size);

52. free(calendar);

53. return 0;

54. }

Output :
Enter the number of days in the week: 7 Enter details for Day 1:

Enter the day name: Sunday Enter the date: 1

Enter the activity for the day: Learning Enter details for Day 2:

Enter the day name: Monday Enter the date: 2

Enter the activity for the day: Coding Enter details for Day 3:

Enter the day name: Tuesday Enter the date: 3


Enter the activity for the day: Testing
Enter details for Day 4:

Enter the day name: Wednesday Enter the date: 4

Enter the activity for the day: Debugging Enter details for Day 5:

Enter the day name: Thrusday Enter the date: 5

Enter the activity for the day: Publishing Enter details for Day 6:

Enter the day name: Friday Enter the date: 6

Enter the activity for the day: Marketing Enter details for Day 7:

Enter the day name: Saturday Enter the date: 7

Enter the activity for the day: Earning Week's Activity Details:

Day 1:

Day Name: Sunday Date: 1

Activity: Learning Day 2:

Day Name: Monday Date: 2

Activity: Coding Day 3:

Day Name: Tuesday Date: 3

Activity: Testing Day 4:

Day Name: Wednesday


Date: 4

Activity: Debugging Day 5:

Day Name: Thrusday Date: 5

Activity: Publishing Day 6:

Day Name: Friday Date: 6

Activity: Marketing Day 7:

Day Name: Saturday Date: 7

Activity: Earning

PROGRAM 2

Design, Develop and Implement a Program in C for the following


operations on Strings:
Read a main String (STR), a Pattern String (PAT) and a Replace String
(REP).
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 :
1. #include<stdio.h>

2. char str[50], pat[20], rep[20], res[50];


3. int c = 0, m = 0, i = 0, j = 0, k, flag = 0;

4. void stringmatch() { 5. while (str[c] != '\0') {

6. if (str[m] == pat[i]) { 7. i++;

8. m++;

9. if (pat[i] == '\0') {

10. flag = 1;

11. for (k = 0; rep[k] != '\0'; k++, j++) {

12. res[j] = rep[k];

13. }

14. i = 0;

15. c = m;

16. }

17. }

18. else {

19. res[j] = str[c]; 20. j++;

21. c++;

22. m = c; 23. i = 0;

24. }

25. }

26. res[j] = '\0';

27. }

28. void main() {

29. printf("Enter the main string:");

30. gets(str);

31. printf("\nEnter the pat string:");

32. gets(pat);

33. printf("\nEnter the replace string:");

34. gets(rep);

35. printf("\nThe string before pattern match is:\n %s", str);

36. stringmatch();

37. if (flag == 1)

38. printf("\nThe string after pattern match and replace is: \n %s ", res);

39. else

40. printf("\nPattern string is not found");


41. }

Output :
*************************OUTPUT 1******************************

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

The string after pattern match and replace is: Designed by Braham

*************************OUTPUT 2******************************

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

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];

5. int top = -1;

6. void push(int item);

7. int pop();

8. void palindrome();

9. void display();

10. void main() {

11. int choice, item;

12. while (1) {

13. printf("\n\n\n\n-----------Menu----------- : ");

14. printf("\n=>1.Push an Element to Stack and Overflow demo ");

15. printf("\n=>2.Pop an Element from Stack and Underflow demo");

16. printf("\n=>3.Palindrome demo ");

17. printf("\n=>4.Display ");

18. printf("\n=>5.Exit");

19. printf("\nEnter your choice: ");

20. scanf("%d", & choice);

21. switch (choice) {

22. case 1:

23. printf("\nEnter an element to be pushed: ");


24. scanf("%d", & item);

25. push(item);

26. break;

27. case 2:

28. item = pop();

29. if (item != -1)

30. printf("\nElement popped is: %d", item);

31. break;

32. case 3:

33. palindrome();

34. break;

35. case 4:

36. display();

37. break;

38. case 5:

39. exit(1);

40. default:

41. printf("\nPlease enter valid choice ");

42. break;

43. }

44. }

45. }

46. void push(int item){

47. if (top == MAX - 1){

48. printf("\n-----------Stack overflow-----------");

49. return;

50. }

51. top = top + 1;

52. s[top] = item;

53. }

54. int pop() {

55. int item;

56. if (top == -1) {

57. printf("\n-----------Stack underflow-----------");


58. return -1;

59. }

60. item = s[top];

61. top = top - 1;

62. return item;

63. }

64. void display() {

65. int i;

66. if (top == -1) {

67. printf("\n-----------Stack is empty-----------");

68. return;

69. }

70. printf("\nStack elements are:\n ");

71. for (i = top; i >= 0; i--)


72. printf("| %d |\n", s[i]);

73. }

74. void palindrome() {

75. int flag = 1, i;

76. printf("\nStack content are:\n");

77. for (i = top; i >= 0; i--) 78. printf("| %d |\n", s[i]);

79. printf("\nReverse of stack content are:\n");

80. for (i = 0; i <= top; i++) 81. printf("| %d |\n", s[i]);

82. for (i = 0; i <= top / 2; i++) {

83. if (s[i] != s[top - i]) {

84. flag = 0;

85. break;

86. }

87. }

88. if (flag == 1){

89. printf("\nIt is palindrome number");

90. }

91. else {

92. printf("\nIt is not a palindrome number");

93. }

94. }

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: 11

-----------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: 12

-----------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: 13

-----------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: 14

-----------Stack overflow-----------

-----------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: 4 Stack elements are:

| 13 |

| 12 |

| 11 |

-----------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: 13

-----------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: 4 Stack elements are:

| 12 |

| 11 |

-----------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: 12

-----------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: 11

-----------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

-----------Stack underflow-----------

-----------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: 4

-----------Stack is empty-----------

-----------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: 11

-----------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: 22

-----------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: 11

-----------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: 3 Stack content are:

| 11 |

| 22 |

| 11 |

Revese of stack content are:

| 11 |

| 22 |

| 11 |

It is 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: 11

-----------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: 22

-----------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: 33

-----------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: 22


-----------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: 3 Stack content are:

| 22 |

| 33 |

| 11 |

Reverse of stack content are:

| 11 |

| 33 |

| 22 |

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: 5


PROGRAM 4
Design, Develop and Implement a Program in C for converting an Infix
Expression to Postfix Expression. Program should support for both
parenthesized and free parenthesized expressions with the operators:
+,-, *, /, % (Remainder), ^ (Power) and alphanumeric operands.
A. Declare a calendar as an array of 7 elements (A dynamically
Created array) to represent 7 days of a week. Each Element of the
array is a structure having three fields. The first field is the name
of the Day (A dynamically allocated String), The second field is
the date of the Day (A integer), the third field is the description of
the activity for a particular day (A dynamically allocated String).
B. Write functions create(), read() and display(); to create the
calendar, to read the data from the keyboard and to print weeks
activity details report on screen.
Code :
1. #include<stdio.h>#include<stdlib.h>void evaluate();

2. void push(char);

3. char pop();

4. int prec(char);char infix[30], postfix[30], stack[30];

5. int top = -1;void main() 6. {

7. printf("\n Enter the valid infix expression:");

8. scanf("%s", infix);

9. evaluate();

10. printf("\nThe entered infix expression is :\n %s \n", infix);

11. printf("\nThe corresponding postfix expression is :\n %s \n", postfix);

12. }void evaluate()

13. {

14. int i = 0, j = 0;

15. char symb, temp;push('#');for (i = 0; infix[i] != '\0'; i++)

16. {

17. symb = infix[i];

18. switch (symb)

19. {

20. case '(':


21. push(symb);

22. break;case ')':

23. temp = pop();

24. while (temp != '(')

25. {

26. postfix[j] = temp; 27. j++;

28. temp = pop();

29. }

30. break;

31. case '+':

32. case '-':

33. case '*':

34. case '/':

35. case '%':

36. case '^':

37. case '$':

38. while (prec(stack[top]) >= prec(symb))

39. {

40. temp = pop();

41. postfix[j] = temp; 42. j++;

43. }

44. push(symb);

45. break;

46. default:

47. postfix[j] = symb; 48. j++;

49. }

50. }

51. while (top > 0)

52. {

53. temp = pop();

54. postfix[j] = temp; 55. j++;

56. }

57. postfix[j] = '\0';

58. }void push(char item)


59. {

60. top = top + 1;

61. stack[top] = item;

62. }char pop()

63. {

64. char item;

65. item = stack[top];

66. top = top - 1;

67. return item;

68. }int prec(char symb)


69. {

70. int p;

71. switch (symb)

72. {

73. case '#': 74. p = -1;

75. break;case '(':

76. case ')': 77. p = 0;

78. break;case '+':

79. case '-': 80. p = 1;

81. break;case '*':

82. case '/':

83. case '%': 84. p = 2;

85. break;case '^':

86. case '$': 87. p = 3;

88. break;

89. }

90. return p;

91. }

Output :
Enter the valid infix expression: a+b The entered infix expression is :

a+b

The corresponding postfix expression is : ab+

Enter the valid infix expression:(a+b)*c/d^5%1 The entered infix expression is :

(a+b)*c/d^5%1The corresponding postfix expression is : ab+c*d5^/1%


PROGRAM 5
Design, Develop and Implement a Program in C for the following Stack
Applications:
A. Evaluation of Suffix expression with single digit operands and
operators: +,-, *, /, %,^.

Code :
1. #include<stdio.h>

2. #include<stdlib.h>

3. #include<math.h>

4. int i, top = -1;

5. int op1, op2, res, s[20];

6. char postfix[90], symb;void push(int item) {

7. top = top + 1;

8. s[top] = item; 9. }

10. int pop() {

11. int item;

12. item = s[top];

13. top = top - 1;

14. return item;

15. }

16. void main() {

17. printf("\nEnter a valid postfix expression:\n");

18. scanf("%s", postfix);

19. for (i = 0; postfix[i] != '\0'; i++) {

20. symb = postfix[i];

21. if (isdigit(symb)) {

22. push(symb - '0');

23. }

24. Else {

25. op2 = pop();

26. op1 = pop();

27. switch (symb){

28. case '+':

29. push(op1 + op2);


30. break;

31. case '-':

32. push(op1 - op2);

33. break;

34. case '*':

35. push(op1 * op2);

36. break;
37. case '/':

38. push(op1 / op2);

39. break;

40. case '%':

41. push(op1 % op2);

42. break;

43. case '$':

44. case '^':

45. push(pow(op1, op2));

46. break;

47. default:

48. push(0);

49. }

50. }

51. }

52. res = pop();

53. printf("\n Result = %d", res);

54. }

Output :
Enter a valid postfix expression: 623+-382/+*2$3+

Result = 52

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 :
1. #include<stdio.h>

2. #include<stdlib.h>

3. struct node{
4. char ssn[25], name[25], dept[10], designation[25];

5. int sal;

6. long int phone;

7. struct node * llink;


8. struct node * rlink; 9. };

10. typedef struct node * NODE;

11. NODE first = NULL;

12. int count = 0;

13. NODE create() {

14. NODE enode;

15. enode = (NODE) malloc(sizeof(struct node));

16. if (enode == NULL) {

17. printf("\n Running out of memory ");

18. exit(0);

19. }

20. printf("\n Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:

\n");

21. scanf("%s %s %s %s %d %ld", enode -> ssn, enode -> name, enode -> dept, enode ->
designation, & enode -> sal, & enode -> phone);

22. enode -> llink = NULL;

23. enode -> rlink = NULL;

24. count++;

25. return enode;

26. }

27. NODE insertfront() {

28. NODE temp;

29. temp = create();

30. if (first == NULL) {

31. return temp;

32. }

33. temp -> rlink = first;

34. first -> llink = temp;

35. return temp;

36. }

37. void display(){

38. NODE cur;

39. int nodeno = 1;

40. cur = first;


41. if (cur == NULL)

42. printf("\nNo Contents to display in DLL ");

43. while (cur != NULL) {

44. 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);

45. cur = cur -> rlink;

46. nodeno++;

47. }

48. printf("\nNo of employee nodes is %d", count);

49. }

50. NODE deletefront(){

51. NODE temp;


52. if (first == NULL) {

53. printf("\nDoubly Linked List is empty ");

54. return NULL;

55. }

56. if (first -> rlink == NULL) {

57. printf("\nThe employee node with the ssn:%s is deleted ", first -> ssn);

58. free(first);

59. count--;

60. return NULL;

61. }

62. temp = first;

63. first = first -> rlink;

64. temp -> rlink = NULL;

65. first -> llink = NULL;

66. printf("\nThe employee node with the ssn:%s is deleted ", temp -> ssn);

67. free(temp);

68. count--;

69. return first;

70. }

71. NODE insertend() {

72. NODE cur, temp;

73. temp = create();

74. if (first == NULL) {

75. return temp;

76. }

77. cur = first;

78. while (cur -> rlink != NULL) {

79. cur = cur -> rlink;

80. }

81. cur -> rlink = temp;

82. temp -> llink = cur;

83. return first;

84. }

85. NODE deleteend() {


86. NODE prev, cur;

87. if (first == NULL) {

88. printf("\nDoubly Linked List is empty ");

89. return NULL;

90. }

91. if (first -> rlink == NULL) {

92. printf("\nThe employee node with the ssn:%s is deleted ", first -> ssn);

93. free(first);

94. count--;

95. return NULL;

96. }

97. prev = NULL;

98. cur = first;

99. while (cur -> rlink != NULL) {


100. prev = cur;

101. cur = cur -> rlink;

102. }

103. cur -> llink = NULL;

104. printf("\nThe employee node with the ssn:%s is deleted ", cur -> ssn);

105. free(cur);

106. prev -> rlink = NULL;

107. count--;

108. return first;

109. }

110. void deqdemo() {

111. int ch;

112. while (1) {

113. printf("\nDemo Double Ended Queue Operation ");

114. printf("\n1:InsertQueueFront\n 2: DeleteQueueFront\n 3:InsertQueueRear\n


4:DeleteQueueRear\n 5:DisplayStatus\n 6: Exit \n");

115. scanf("%d", & ch);

116. switch (ch) {

117. case 1:

118. first = insertfront();

119. break;

120. case 2:

121. first = deletefront();

122. break;

123. case 3:

124. first = insertend();

125. break;

126. case 4:

127. first = deleteend();

128. break;

129. case 5:

130. display();

131. break;

132. default:
133. return;

134. }

135. }

136. }

137. void main() {

138. int ch, i, n;

139. while (1)

140. {

141. printf("\n\n--------Menu--------");

142. printf("\n1:Create DLL of Employee Nodes ");

143. printf("\n2:DisplayStatus");

144. printf("\n3:InsertAtEnd");

145. printf("\n4:DeleteAtEnd");

146. printf("\n5:InsertAtFront");
147. printf("\n6:DeleteAtFront");

148. printf("\n7:Double Ended Queue Demo using DLL ");

149. printf("\n8:Exit \n");

150. printf("\nPlease enter your choice: ");

151. scanf("%d", & ch);

152. switch (ch) {

153. case 1:

154. printf("\nEnter the no of Employees: ");

155. scanf("%d", & n);

156. for (i = 1; i <= n; i++)

157. first = insertend();

158. break;

159. case 2:

160. display();

161. break;

162. case 3:

163. first = insertend();

164. break

165. ;case 4:

166. first = deleteend();

167. break;

168. case 5:

169. first = insertfront();

170. break;

171. case 6:

172. first = deletefront();

173. break

174. ;case 7:

175. deqdemo();

176. break;

177. case 8:

178. exit(0);

179. default:

180. printf("\nPlease Enter the valid choice ");


181. }

182. }

183. }

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

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee: 1EPL

Braham Developer Senior 13627

8476283712

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee: 2EPL

Aman Trader Manager 20000

2763578156

--------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:1EPL| |Name:Braham| |Department:Developer| |Designation:Senior |

|Salary:13627| |Phone no:8476283712|


|ENode:2| |SSN:2EPL| |Name:Aman | |Department:Trader | |Designation:Manager|

|Salary:20000| |Phone no:2763578156| 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: 3

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee: 3EPL

Bikash Meeting Manager 30000

8237462936

--------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:1EPL| |Name:Braham| |Department:Developer| |Designation:Senior |

|Salary:13627| |Phone no:8476283712|


|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| 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: 5Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the


employee:

4EPL

Shoaib

Digital Marketing Manager

40000

2835826437

--------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:ExitPlease enter your choice: 2
|ENode:1| |SSN:4EPL| |Name:Shoaib| |Department:Digital Marketing| |Designation:Manager|

|Salary:40000| |Phone no:2835826437|

|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| 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

The employee node with the ssn:3EPL is deleted

--------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

The employee node with the ssn:4 EPL is deleted


--------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:1EPL| |Name:Braham| |Department:Developer| |Designation:Senior |

|Salary:13627| |Phone no:8476283712|

|ENode:2| |SSN:2EPL| |Name:Aman | |Department:Trader | |Designation:Manager|

|Salary:20000| |Phone no:2763578156| 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

Demo Double Ended Queue Operation 1:InsertQueueFront

2: DeleteQueueFront 3:InsertQueueRear 4:DeleteQueueRear 5:DisplayStatus

6: Exit
Please enter your choice: 2

The employee node with the ssn:1

EPL is deletedDemo 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:2

EPL is deletedDemo 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 emptyDemo Double Ended Queue Operation 1:InsertQueueFront

2: DeleteQueueFront 3:InsertQueueRear 4:DeleteQueueRear 5:DisplayStatus

6: Exit

Please enter your choice: 6

--------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: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

3. Display Circular Queue

4. Exit

Enter your choice: 1

Enter element to be inserted: A

---- Circular Queue Menu ----

1. Insert an Element

2. Delete an Element

3. Display Circular Queue

4. Exit

Enter your choice: 1

Enter element to be inserted: B

---- Circular Queue Menu ----

1. Insert an Element

2. Delete an Element

3. Display Circular Queue


4. Exit

Enter your choice: 1

Enter element to be inserted: C

---- Circular Queue Menu ----

1. Insert an Element

2. Delete an Element

3. Display Circular Queue

4. Exit

Enter your choice: 3

Circular Queue elements: A B C

---- Circular Queue Menu ----

1. Insert an Element

2. Delete an Element

3. Display Circular Queue

4. Exit

Enter your choice: 2 149.

---- Circular Queue Menu ----

1. Insert an Element

2. Delete an Element

3. Display Circular Queue

4. Exit

Enter your choice: 3

Circular Queue elements: B C 158.

---- Circular Queue Menu ----

1. Insert an Element

2. Delete an Element

3. Display Circular Queue

4. Exit

Enter your choice: 4

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

Enter your choice: 1 137.

Enter the number of elements: 12

Enter The value: 6

Enter The value: 9

Enter The value: 5

Enter The value: 2

Enter The value: 8

Enter The value: 15

Enter The value: 24

Enter The value: 14

Enter The value: 7

Enter The value: 8

Enter The value: 5

Enter The value: 2

-------BST MENU-------

1.Create a BST 2.Search

3.BST Traversals: 4.Exit


Enter your choice: 3

The Preorder display: 65 2 9 8 7 15 14 24

The Inorder display: 2 5 6 7 8 9 14 15 24

The Postorder display: 2 5 7 8 14 24 15 9 6

-------BST MENU-------

1.Create a BST 2.Search

3.BST Traversals: 4.Exit

Enter your choice: 2

Enter Element to be searched: 66 Key element is not found in the BST

-------BST MENU-------

1.Create a BST 2.Search

3.BST Traversals: 4.Exit

Enter your choice: 2

Enter Element to be searched: 14 Key element is present in B


-------BST MENU-------

1.Create a BST 2.Search

3.BST Traversals: 4.Exit

Enter your choice: 4

PROGRAM 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/BFSmethod.
CODE :
1. #include<stdio.h>
2. #include<stdlib.h>
3. int a[50][50], n, visited[50];
4. int q[20], front = -1, rear = -1;
5. int s[20], top = -1, count = 0; 8.
6. void bfs(int v){
7. int i, cur;
8. visited[v] = 1;
9. q[++rear] = v;
10. while (front != rear){
11. cur = q[++front];
12. for (i = 1; i <= n; i++) {
13. if ((a[cur][i] == 1) && (visited[i] == 0)) {
14. q[++rear] = i;
15. visited[i] = 1;
16. printf("%d ", i);
a. }
b. }
c. }
17. }
18. void dfs(int v) {
19. int i;
20. visited[v] = 1;
21. s[++top] = v;
22. for (i = 1; i <= n; i++) {
23. if (a[v][i] == 1 && visited[i] == 0) {
24. printf("%d ", i);
25. dfs(i);
a. }
26. }
27. }
28. int main() {
29. int ch, start, i, j;
30. printf("\nEnter the number of vertices in graph:");
31. scanf("%d", & n);
32. printf("\nEnter the adjacency matrix:\n");
33. for (i = 1; i <= n; i++) {
34. for (j = 1; j <= n; j++)
35. scanf("%d", & a[i][j]);
a. }
36. for (i = 1; i <= n; i++)
37. visited[i] = 0;
38. printf("\nEnter the starting vertex: ");
39. scanf("%d", & start); 61.
40. printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
41. printf("\n==>2. DFS: Print all nodes reachable from a given starting node");
42. printf("\n==>3:Exit");
43. printf("\nEnter your choice: ");
44. scanf("%d", & ch);
45. switch (ch) {
46. case 1:
47. printf("\nNodes reachable from starting vertex %d are: ", start);
48. bfs(start);
49. for (i = 1; i <= n; i++) {
50. if (visited[i] == 0)
51. printf("\nThe vertex that is not reachable is %d", i);
52. }
a. break;
53. case 2:
54. printf("\nNodes reachable from starting vertex %d are:\n", start);
55. dfs(start);
56. break;
57. case 3:
58. exit(0);
59. default:
60. printf("\nPlease enter valid choice:");
a. }
61. }

OUTPUT
*************************case-1*************************
Enter the number of vertices in graph:4 Enter the adjacency matrix:

0101

0010

0001

0000

Enter the starting vertex: 1

==>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

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

0010

0001

0000

Enter the starting vertex: 2

==>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

Nodes reachable from starting vertex 2 are: 3 4 The vertex that is not reachable is 1

*************************case-3************************* Enter the number of vertices in


graph:4

Enter the adjacency matrix: 0 1 0 1

0010

0001

0000

Enter the starting vertex: 1

==>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

Nodes reachable from starting vertex 1 are: 2 3 4


*************************case-4*************************

Enter the number of vertices in graph:4 Enter the adjacency matrix:

0101

0010

0001

0000

Enter the starting vertex: 2

==>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

Nodes reachable from starting vertex 2 are: 3 4

You might also like