0% found this document useful (0 votes)
8 views18 pages

Program 1

The document contains multiple C programs that demonstrate various data structure operations. These include menu-driven implementations for 1-D and 2-D arrays, singly linked lists, doubly linked lists, and circular linked lists. Each program provides functionalities such as summing numbers, insertion, deletion, and searching elements.
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)
8 views18 pages

Program 1

The document contains multiple C programs that demonstrate various data structure operations. These include menu-driven implementations for 1-D and 2-D arrays, singly linked lists, doubly linked lists, and circular linked lists. Each program provides functionalities such as summing numbers, insertion, deletion, and searching elements.
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/ 18

Program 1: Write a menu-driven C Program to implement the

following in 1-D array:


a. Sum of even and odd numbers
b. The sum of prime number

C CODE:
1. 1. #include <stdio.h>
2. 2. #include <stdlib.h>
3. 3. int isPrime(int n)
4. 4. {
5. 5. if (n <= 1)
6. 6. return 0;
7. 7. for (int i = 2; i * i <= n; i++)
8. 8. {
9. 9. if (n % i == 0)
10. 10. return 0;
11. 11. }
12. 12. return 1;
13. 13. }
14. 14. int main()
15. 15. {
16. 16. int size, i, choice;
17. 17. int even_sum = 0, odd_sum = 0, prime_sum = 0;
18. 18. printf("Enter the size of the array: ");
19. 19. scanf("%d", &size);
20. 20. int arr[size];
21. 21. printf("Enter the elements of the array:\n");
22. 22. for (i = 0; i < size; i++)
23. 23. {
24. 24. scanf("%d", &arr[i]);
25. 25. }
26. 26. do
27. 27. {
28. 28. printf("\nMenu:\n");
29. 29. printf("1. Calculate Sum of Even and Odd Numbers\n");
30. 30. printf("2. Calculate Sum of Prime Numbers\n");
31. 31. printf("3. Exit\n");
32. 32. printf("Enter your choice: ");
33. 33. scanf("%d", &choice);
34. 34. switch (choice)
35. 35. {
36. 36. case 1:
37. 37. even_sum = 0;
38. 38. odd_sum = 0;
39. 39. for (i = 0; i < size; i++)
40. 40. {
41. 41. if (arr[i] % 2 == 0)
42. 42. {
43. 43. even_sum += arr[i];
44. 44. }
45. 45. else
46. 46. {
47. 47. odd_sum += arr[i];
48. 48. }
49. 49. }
50. 50. printf("Sum of even numbers: %d\n", even_sum);
51. 51. printf("Sum of odd numbers: %d\n", odd_sum);
52. 52. break;
53. 53. case 2:
54. 54. prime_sum = 0;
55. 55. for (i = 0; i < size; i++)
56. 56. {
57. 57. if (isPrime(arr[i]))
58. 58. {
59. 59. prime_sum += arr[i];
60. 60. }
61. 61. }
62. 62. printf("Sum of prime numbers: %d\n", prime_sum);
63. 63. break;
64. 64. case 3:
65. 65. printf("Exiting program.\n");
66. 66. break;
67. 67. default:
68. 68. printf("Invalid choice. Please try again.\n");
69. 69. }
70. 70. } while (choice != 3);
71. 71. return 0;
72. 72. }
73.
73.

OUTPUT:
Program 2: Write a menu-driven C Program to implement the
following in 2-D
array: a. Addition of two 2-D arrays
b. The row-wise sum of elements

C CODE:
1. #include <stdio.h>
2. #include <stdlib.h>
3. int main()
4. {
5. int rows, cols, i, j, choice;
6. printf("Enter the number of rows: ");
7. scanf("%d", &rows);
8. printf("Enter the number of columns: ");
9. scanf("%d", &cols);
10. int matrix1[rows][cols], matrix2[rows][cols], result[rows][cols];
11. printf("Enter the elements of the first matrix:\n");
12. for (i = 0; i < rows; i++)
13. {
14. for (j = 0; j < cols; j++)
15. {
16. scanf("%d", &matrix1[i][j]);
17. }
18. }
19. printf("Enter the elements of the second matrix:\n");
20. for (i = 0; i < rows; i++)
21. {
22. for (j = 0; j < cols; j++)
23. {
24. scanf("%d", &matrix2[i][j]);
25. }
26. }
27. do
28. {
29. printf("\nMenu:\n");
30. printf("1. Add two 2-D arrays\n");
31. printf("2. Calculate row-wise sum\n");
32. printf("3. Exit\n");
33. printf("Enter your choice: ");
34. scanf("%d", &choice);
35. switch (choice)
36. {
37. case 1:
38. // Addition of two matrices
39. printf("Sum of the matrices:\n");
40. for (i = 0; i < rows; i++)
41. {
42. for (j = 0; j < cols; j++)
43. {
44. result[i][j] = matrix1[i][j] + matrix2[i][j];
45. printf("%d ", result[i][j]);
46. }
47. printf("\n");
48. }
49. break;
50. case 2:
51. // Row-wise sum
52. printf("Row-wise sum of the first matrix:\n");
53. for (i = 0; i < rows; i++)
54. {
55. int rowSum = 0;
56. for (j = 0; j < cols; j++)
57. {
58. rowSum += matrix1[i][j];
59. }
60. printf("Sum of row %d: %d\n", i + 1, rowSum);
61. }
62. break;
63. case 3:
64. printf("Exiting program.\n");
65. break;
66. default:
67. printf("Invalid choice. Please try again.\n");
68. }
69. } while (choice != 3);
70. return 0;
71. }
72.

OUTPUT:
Program 3: Write C programs to illustrate the following operations
on a singly linked
list: a. Insertion
b. Deletion
c. Searching an element

C CODE:
1. #include <stdio.h>
2. #include <stdlib.h>
3. // Define the structure for a node
4. struct Node
5. {
6. int data;
7. struct Node *next;
8. };
9. // Function to insert a node at the beginning of the list
10. void insertAtBeginning(struct Node **head_ref, int new_data)
11. {
12. struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
13. new_node->data = new_data;
14. new_node->next = (*head_ref);
15. (*head_ref) = new_node;
16. }
17. // Function to insert a node at the end of the list
18. void insertAtEnd(struct Node **head_ref, int new_data)
19. {
20. struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
21. new_node->data = new_data;
22. new_node->next = NULL;
23. if (*head_ref == NULL)
24. {
25. *head_ref = new_node;
26. return;
27. }
28. struct Node *last = *head_ref;
29. while (last->next != NULL)
30. {
31. last = last->next;
32. }
33. last->next = new_node;
34. }
35. // Function to delete a node from the beginning of the list
36. void deleteFromBeginning(struct Node **head_ref)
37. {
38. if (*head_ref == NULL)
39. {
40. printf("List is empty. Cannot delete.\n");
41. return;
42. }
43. struct Node *temp = *head_ref;
44. *head_ref = (*head_ref)->next;
45. free(temp);
46. }
47. // Function to delete a node from the end of the list
48. void deleteFromEnd(struct Node **head_ref)
49. {
50. if (*head_ref == NULL)
51. {
52. printf("List is empty. Cannot delete.\n");
53. return;
54. }
55. if ((*head_ref)->next == NULL)
56. {
57. free(*head_ref);
58. *head_ref = NULL;
59. return;
60. }
61. struct Node *secondLast = *head_ref;
62. while (secondLast->next->next != NULL)
63. {
64. secondLast = secondLast->next;
65. }
66. free(secondLast->next);
67. secondLast->next = NULL;
68. }
69. // Function to search for an element in the list
70. int searchElement(struct Node *head, int key)
71. {
72. struct Node *current = head;
73. while (current != NULL)
74. {
75. if (current->data == key)
76. {
77. return 1; // Found
78. }
79. current = current->next;
80. }
81. return 0; // Not found
82. }
83. // Function to print the linked list
84. void printList(struct Node *node)
85. {
86. while (node != NULL)
87. {
88. printf("%d -> ", node->data);
89. node = node->next;
90. }
91. printf("NULL\n");
92. }
93. int main()
94. {
95. struct Node *head = NULL;
96. // Insert elements
97. insertAtEnd(&head, 1);
98. insertAtBeginning(&head, 2);
99. insertAtEnd(&head, 3);
100. insertAtBeginning(&head, 4);
101. printf("Linked list after insertions: ");
102. printList(head);
103. // Delete elements
104. deleteFromBeginning(&head);
105. deleteFromEnd(&head);
106. printf("Linked list after deletions: ");
107. printList(head);
108. // Search for an element
109. int searchKey = 3;
110. if (searchElement(head, searchKey))
111. {
112. printf("%d found in the list\n", searchKey);
113. }
114. else
115. {
116. printf("%d not found in the list\n", searchKey);
117. }
118. return 0;
119. }
120.

OUTPUT:
Program 4: Write C programs to illustrate the following operations
on a doubly linkedlist:
a. Insertion
b. Deletion

C CODE:
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct Node {
5. int data;
6. struct Node* prev;
7. struct Node* next;
8. };
9.
10. void insertFront(struct Node** head, int data) {
11. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
12. if (!newNode) {
13. printf("Memory allocation failed!\n");
14. return;
15. }
16. newNode->data = data;
17. newNode->prev = NULL;
18. newNode->next = *head;
19. if (*head != NULL)
20. (*head)->prev = newNode;
21. *head = newNode;
22. }
23.
24. void deleteNode(struct Node** head, struct Node* delNode) {
25. if (*head == NULL || delNode == NULL) return;
26.
27. if (*head == delNode)
28. *head = delNode->next;
29.
30. if (delNode->next != NULL)
31. delNode->next->prev = delNode->prev;
32.
33. if (delNode->prev != NULL)
34. delNode->prev->next = delNode->next;
35.
36. free(delNode);
37. }
38.
39. void printList(struct Node* head) {
40. struct Node* temp = head;
41. printf("List: ");
42. while (temp != NULL) {
43. printf("%d ", temp->data);
44. temp = temp->next;
45. }
46. printf("\n");
47. }
48.
49. int main() {
50. struct Node* head = NULL;
51.
52. insertFront(&head, 10);
53. insertFront(&head, 20);
54. insertFront(&head, 30);
55.
56. printList(head); // List: 30 20 10
57.
58. deleteNode(&head, head->next); // Delete 20
59.
60. printList(head); // List: 30 10
61.
62. return 0;
63. }

OUTPUT:
Program 5: Write C programs to illustrate various operations on a
circular linked list.

C CODE:
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct Node {
5. int data;
6. struct Node* next;
7. };
8.
9. void insertEnd(struct Node** head, int data) {
10. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
11. if (!newNode) {
12. printf("Memory allocation failed!\n");
13. return;
14. }
15.
16. newNode->data = data;
17. newNode->next = NULL;
18.
19. if (*head == NULL) {
20. *head = newNode;
21. newNode->next = newNode; // Self-loop for single node
22. } else {
23. struct Node* temp = *head;
24. while (temp->next != *head)
25. temp = temp->next;
26. temp->next = newNode;
27. newNode->next = *head;
28. }
29. }
30.
31. void printCircularList(struct Node* head) {
32. if (head == NULL) {
33. printf("List is empty.\n");
34. return;
35. }
36.
37. struct Node* temp = head;
38. printf("Circular List: ");
39. do {
40. printf("%d ", temp->data);
41. temp = temp->next;
42. } while (temp != head);
43. printf("\n");
44. }
45.
46. int main() {
47. struct Node* head = NULL;
48.
49. insertEnd(&head, 10);
50. insertEnd(&head, 20);
51. insertEnd(&head, 30);
52.
53. printCircularList(head); // Output: Circular List: 10 20 30
54.
55. return 0;
56. }
57.

OUTPUT:
Program 6: Write C Programs to illustrate the Implementation of a
Stack using an Array.

C CODE:
1. #include <stdio.h>
2. #define MAX 100
3.
4. int stack[MAX], top = -1;
5.
6. void push(int item) {
7. if (top >= MAX - 1)
8. printf("Stack Overflow\n");
9. else
10. stack[++top] = item;
11. }
12.
13. int pop() {
14. if (top < 0) {
15. printf("Stack Underflow\n");
16. return -1;
17. }
18. return stack[top--];
19. }
20.
21. int main() {
22. push(10);
23. push(20);
24. push(30);
25.
26. printf("Popped element: %d\n", pop());
27. printf("Popped element: %d\n", pop());
28. printf("Popped element: %d\n", pop());
29. printf("Popped element: %d\n", pop()); // Underflow case
30.
31. return 0;
32. }
33.

OUTPUT:
Program 7: Write C Programs to illustrate the Implementation of a
Queue using an Array.

C CODE:
1. #include <stdio.h>
2. #define SIZE 100
3.
4. int queue[SIZE], front = -1, rear = -1;
5.
6. void enqueue(int item) {
7. if (rear == SIZE - 1) {
8. printf("Queue Full\n");
9. } else {
10. if (front == -1) front = 0;
11. queue[++rear] = item;
12. }
13. }
14.
15. int dequeue() {
16. if (front == -1 || front > rear) {
17. printf("Queue Empty\n");
18. return -1;
19. }
20. return queue[front++];
21. }
22.
23. void display() {
24. if (front == -1 || front > rear) {
25. printf("Queue is empty\n");
26. return;
27. }
28. printf("Queue elements: ");
29. for (int i = front; i <= rear; i++) {
30. printf("%d ", queue[i]);
31. }
32. printf("\n");
33. }
34.
35. int main() {
36. enqueue(10);
37. enqueue(20);
38. enqueue(30);
39.
40. display(); // Output: 10 20 30
41.
42. printf("Dequeued: %d\n", dequeue()); // Output: 10
43. printf("Dequeued: %d\n", dequeue()); // Output: 20
44.
45. display(); // Output: 30
46.
47. return 0;
48. }
49.

OUTPUT:
Program 8: Write C Programs to illustrate the Implementation of a
Circular Queue using an Array.

C CODE:
1. #include <stdio.h>
2. #define CQ_SIZE 100
3.
4. int cq[CQ_SIZE], cq_front = -1, cq_rear = -1;
5.
6. void cEnqueue(int item) {
7. if ((cq_rear + 1) % CQ_SIZE == cq_front) {
8. printf("Queue Full\n");
9. } else {
10. cq_rear = (cq_rear + 1) % CQ_SIZE;
11. cq[cq_rear] = item;
12. if (cq_front == -1)
13. cq_front = 0;
14. }
15. }
16.
17. int cDequeue() {
18. if (cq_front == -1) {
19. printf("Queue Empty\n");
20. return -1;
21. }
22. int item = cq[cq_front];
23. if (cq_front == cq_rear)
24. cq_front = cq_rear = -1;
25. else
26. cq_front = (cq_front + 1) % CQ_SIZE;
27. return item;
28. }
29.
30. void displayCQ() {
31. if (cq_front == -1) {
32. printf("Queue is empty\n");
33. return;
34. }
35.
36. printf("Circular Queue: ");
37. int i = cq_front;
38. while (1) {
39. printf("%d ", cq[i]);
40. if (i == cq_rear)
41. break;
42. i = (i + 1) % CQ_SIZE;
43. }
44. printf("\n");
45. }
46.
47. int main() {
48. cEnqueue(10);
49. cEnqueue(20);
50. cEnqueue(30);
51. displayCQ(); // Output: 10 20 30
52.
53. printf("Dequeued: %d\n", cDequeue()); // Output: 10
54. displayCQ(); // Output: 20 30
55.
56. cEnqueue(40);
57. displayCQ(); // Output: 20 30 40
58.
59. return 0;
60. }
61.
OUTPUT:
Program 9: Write C Programs to illustrate the Implementation of a
Stack using a Linked List.

C CODE:
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct StackNode {
5. int data;
6. struct StackNode* next;
7. };
8.
9. void push(struct StackNode** top, int data) {
10. struct StackNode* newNode = (struct StackNode*)malloc(sizeof(struct StackNode));
11. if (!newNode) {
12. printf("Memory allocation failed\n");
13. return;
14. }
15. newNode->data = data;
16. newNode->next = *top;
17. *top = newNode;
18. }
19.
20. int pop(struct StackNode** top) {
21. if (*top == NULL) {
22. printf("Stack Underflow\n");
23. return -1;
24. }
25. struct StackNode* temp = *top;
26. int popped = temp->data;
27. *top = (*top)->next;
28. free(temp);
29. return popped;
30. }
31.
32. void display(struct StackNode* top) {
33. if (top == NULL) {
34. printf("Stack is empty\n");
35. return;
36. }
37. printf("Stack elements: ");
38. while (top != NULL) {
39. printf("%d ", top->data);
40. top = top->next;
41. }
42. printf("\n");
43. }
44.
45. int main() {
46. struct StackNode* top = NULL;
47.
48. push(&top, 10);
49. push(&top, 20);
50. push(&top, 30);
51. display(top); // Output: 30 20 10
52.
53. printf("Popped: %d\n", pop(&top)); // Output: 30
54. display(top); // Output: 20 10
55.
56. return 0;
57. }

OUTPUT:
Program 10: Write C Programs to illustrate the Implementation of a
Queue using LinkedList.

C CODE:
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct QNode {
5. int data;
6. struct QNode* next;
7. };
8.
9. struct Queue {
10. struct QNode *front, *rear;
11. };
12.
13. void enqueue(struct Queue* q, int data) {
14. struct QNode* newNode = (struct QNode*)malloc(sizeof(struct QNode));
15. if (!newNode) {
16. printf("Memory allocation failed\n");
17. return;
18. }
19. newNode->data = data;
20. newNode->next = NULL;
21.
22. if (q->rear == NULL) {
23. q->front = q->rear = newNode;
24. return;
25. }
26.
27. q->rear->next = newNode;
28. q->rear = newNode;
29. }
30.
31. int dequeue(struct Queue* q) {
32. if (q->front == NULL) {
33. printf("Queue Empty\n");
34. return -1;
35. }
36.
37. struct QNode* temp = q->front;
38. int item = temp->data;
39. q->front = q->front->next;
40.
41. if (q->front == NULL)
42. q->rear = NULL;
43.
44. free(temp);
45. return item;
46. }
47.
48. void display(struct Queue* q) {
49. if (q->front == NULL) {
50. printf("Queue is empty\n");
51. return;
52. }
53.
54. struct QNode* temp = q->front;
55. printf("Queue elements: ");
56. while (temp != NULL) {
57. printf("%d ", temp->data);
58. temp = temp->next;
59. }
60. printf("\n");
61. }
62.
63. int main() {
64. struct Queue q = {NULL, NULL};
65.
66. enqueue(&q, 10);
67. enqueue(&q, 20);
68. enqueue(&q, 30);
69. display(&q); // Output: 10 20 30
70.
71. printf("Dequeued: %d\n", dequeue(&q)); // Output: 10
72. display(&q); // Output: 20 30
73.
74. return 0;
75. }
76.

OUTPUT:
Program 11: Linear Search in C

C CODE:
1. #include <stdio.h>
2. int linearSearch(int array[], int n, int x)
3. {
4. for (int i = 0; i < n; i++)
5. {
6. if (array[i] == x)
7. return i; // Element found
8. }
9. return -1; // Element not found
10. }
11. int main()
12. {
13. int array[] = {2, 4, 0, 1, 9};
14. int x = 1;
15. int n = sizeof(array) / sizeof(array[0]);
16. int result = linearSearch(array, n, x);
17.
18. if (result == -1)
19. printf("Element not found\n");
20. else
21. printf("Element found at index: %d\n", result);
22. return 0;
23. }
24.

OUTPUT:
Program 12: Binary Search in C

C CODE:
1. #include <stdio.h>
2. int binarySearch(int array[], int x, int low, int high)
3. {
4. while (low <= high)
5. {
6. int mid = low + (high - low) / 2;
7. if (array[mid] == x)
8. return mid; // Element found
9. if (array[mid] < x)
10. low = mid + 1; // Narrow down to right half
11. else
12. high = mid - 1; // Narrow down to left half
13. }
14. return -1; // Element not found
15. }
16. int main()
17. {
18. int array[] = {3, 4, 5, 6, 7, 8, 9};
19. int x = 4;
20. int n = sizeof(array) / sizeof(array[0]);
21. int result = binarySearch(array, x, 0, n - 1);
22. if (result == -1)
23. printf("Element not found\n");
24. else
25. printf("Element found at index: %d\n", result);
26. return 0;
27. }
28.

OUTPUT:

You might also like