Experiment
Experiment
Experiment: 1
WAP for implementation of stack using Array.
1. #include <stdio.h>
2. #define MAX 5
3.
4. int stack[MAX];
5. int top = -1;
6.
7. int isFull() {
8. return top == MAX - 1;
9. }
10.
11. int isEmpty() {
12. return top == -1;
13. }
14.
15. void push(int value) {
16. if (isFull()) {
17. printf("Stack Overflow\n");
18. } else {
19. stack[++top] = value;
20. }
21. }
22.
23. int pop() {
24. if (isEmpty()) {
25. printf("Stack Underflow\n");
26. return -1;
27. } else {
28. return stack[top--];
29. }
30. }
31.
32. int peek() {
33. if (isEmpty()) {
34. printf("Stack is empty\n");
35. return -1;
36. } else {
37. return stack[top];
38. }
39. }
40.
41. void display() {
42. if (isEmpty()) {
Page |2
Output :
Page |3
Experiment: 2
WAP for implementation of Queue using Array.
1. #include <stdio.h>
2. #define MAX 5
3.
4. int queue[MAX];
5. int front = -1;
6. int rear = -1;
7.
8. int isFull() {
9. return (rear + 1) % MAX == front;
10. }
11.
12. int isEmpty() {
13. return front == -1;
14. }
15.
16. void enqueue(int value) {
17. if (isFull()) {
18. printf("Queue Overflow\n");
19. } else {
20. if (front == -1) {
21. front = 0;
22. }
23. rear = (rear + 1) % MAX;
24. queue[rear] = value;
25. }
26. }
27.
28. int dequeue() {
29. if (isEmpty()) {
30. printf("Queue Underflow\n");
31. return -1;
32. } else {
33. int value = queue[front];
Page |4
72. display();
73.
74. return 0;
75. }
Output :
Page |6
Experiment: 3
WAP for implementation of stack using Linked list.
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct Node {
5. int data;
6. struct Node* next;
7. };
8.
9. struct Node* top = NULL;
10.
11. int isEmpty() {
12. return top == NULL;
13. }
14.
15. void push(int value) {
16. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
17. if (newNode == NULL) {
18. printf("Stack Overflow\n");
19. } else {
20. newNode->data = value;
21. newNode->next = top;
22. top = newNode;
23. }
24. }
25. int pop() {
26. if (isEmpty()) {
27. printf("Stack Underflow\n");
28. return -1;
29. } else {
30. struct Node* temp = top;
31. int value = temp->data;
32. top = top->next;
33. free(temp);
Page |7
Output :
Page |9
Experiment: 4
WAP for creation and implementation of linked list.
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. // Define a node structure
5. struct Node {
6. int data;
7. struct Node* next;
8. };
9.
10. // Function to create a new node
11. struct Node* createNode(int data) {
12. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
13. if (!newNode) {
14. printf("Memory allocation failed\n");
15. exit(1);
16. }
17. newNode->data = data;
18. newNode->next = NULL;
19. return newNode;
20. }
21.
22. // Function to insert a node at the beginning of the linked list
23. void insertAtBeginning(struct Node** head, int data) {
24. struct Node* newNode = createNode(data);
25. newNode->next = *head;
26. *head = newNode;
27. }
28.
29. // Function to print the linked list
30. void printList(struct Node* head) {
31. struct Node* temp = head;
32. while (temp != NULL) {
33. printf("%d -> ", temp->data);
34. temp = temp->next;
35. }
36. printf("NULL\n");
37. }
38.
P a g e | 10
Output:
P a g e | 11
Experiment:5
WAP for insertion in singly linked list from beginning, end
any position .
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. // Define a node structure
5. struct Node {
6. int data;
7. struct Node* next;
8. };
9.
10. // Function to create a new node
11. struct Node* createNode(int data) {
12. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
13. if (!newNode) {
14. printf("Memory allocation failed\n");
15. exit(1);
16. }
17. newNode->data = data;
18. newNode->next = NULL;
19. return newNode;
20. }
21.
22. // Function to insert a node at the beginning of the linked list
23. void insertAtBeginning(struct Node** head, int data) {
24. struct Node* newNode = createNode(data);
25. newNode->next = *head;
26. *head = newNode;
27. }
28.
29. // Function to insert a node at the end of the linked list
30. void insertAtEnd(struct Node** head, int data) {
31. struct Node* newNode = createNode(data);
32. if (*head == NULL) {
33. *head = newNode;
34. } else {
35. struct Node* temp = *head;
36. while (temp->next != NULL) {
P a g e | 12
81. }
82. }
83.
84. int main() {
85. struct Node* head = NULL;
86.
87. // Insert elements into the linked list
88. insertAtBeginning(&head, 21); // Insert at beginning
89. insertAtEnd(&head, 42); // Insert at end
90. insertAtPosition(&head, 52, 2); // Insert at position 2
91. insertAtPosition(&head, 33, 3); // Insert at position 3
92.
93. // Print the linked list
94. printList(head);
95.
96. // Free the linked list memory
97. freeList(head);
98.
99. return 0;
100. }
Output:
P a g e | 14
Experiment: 6
WAP for creation and implementation of doubly linked list.
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. // Define a node structure
5. struct Node {
6. int data;
7. struct Node* next;
8. struct Node* prev;
9. };
10.
11. // Function to create a new node
12. struct Node* createNode(int data) {
13. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
14. if (!newNode) {
15. printf("Memory allocation failed\n");
16. exit(1);
17. }
18. newNode->data = data;
19. newNode->next = NULL;
20. newNode->prev = NULL;
21. return newNode;
22. }
23.
24. // Function to insert a node at the beginning of the doubly linked list
25. void insertAtBeginning(struct Node** head, int data) {
26. struct Node* newNode = createNode(data);
27. if (*head != NULL) {
28. (*head)->prev = newNode;
29. }
30. newNode->next = *head;
31. *head = newNode;
32. }
33.
34. // Function to insert a node at the end of the doubly linked list
35. void insertAtEnd(struct Node** head, int data) {
36. struct Node* newNode = createNode(data);
37. if (*head == NULL) {
38. *head = newNode;
P a g e | 15
39. } else {
40. struct Node* temp = *head;
41. while (temp->next != NULL) {
42. temp = temp->next;
43. }
44. temp->next = newNode;
45. newNode->prev = temp;
46. }
47. }
48.
49. // Function to print the doubly linked list
50. void printList(struct Node* head) {
51. struct Node* temp = head;
52. while (temp != NULL) {
53. printf("%d -> ", temp->data);
54. temp = temp->next;
55. }
56. printf("NULL\n");
57. }
58.
59. // Function to free the doubly linked list memory
60. void freeList(struct Node* head) {
61. struct Node* temp;
62. while (head != NULL) {
63. temp = head;
64. head = head->next;
65. free(temp);
66. }
67. }
68.
69. int main() {
70. struct Node* head = NULL;
71.
72. // Insert elements into the doubly linked list
73. insertAtBeginning(&head, 21); // Insert at beginning
74. insertAtEnd(&head, 34); // Insert at end
75. insertAtEnd(&head, 26); // Insert at end
76. insertAtEnd(&head, 39); // Insert at end
77.
78. // Print the doubly linked list
79. printList(head);
80.
81. // Free the doubly linked list memory
82. freeList(head);
P a g e | 16
83. return 0;
84. }
Output:
P a g e | 17
Experiment: 7
WAP to search an element using linear search.
1. #include <stdio.h>
2.
3. // Function to perform linear search
4. int linearSearch(int arr[], int size, int target) {
5. for (int i = 0; i < size; i++) {
6. if (arr[i] == target) {
7. return i; // Return the index of the target element
8. }
9. }
10. return -1; // Return -1 if the target element is not found
11. }
12.
13. int main() {
14. int arr[] = {10, 20, 30, 40, 50};
15. int size = sizeof(arr) / sizeof(arr[0]);
16. int target = 30;
17.
18. int result = linearSearch(arr, size, target);
19.
20. if (result != -1) {
21. printf("Element %d found at index %d\n", target, result);
22. } else {
23. printf("Element %d not found in the array\n", target);
24. }
25.
26. return 0;
27. }
P a g e | 18
Output:
P a g e | 19
Experiment: 8
WAP to search an element using Binary search.
1. #include <stdio.h>
2. // Function to perform binary search
3. int binarySearch(int arr[], int size, int target) {
4. int left = 0, right = size - 1;
5. while (left <= right) {
6. int mid = left + (right - left) / 2;
7.
8. // Check if target is present at mid
9. if (arr[mid] == target) {
10. return mid; // Return the index of the target element
11. }
12.
13. // If target is greater, ignore the left half
14. if (arr[mid] < target) {
15. left = mid + 1;
16. } else {
17. // If target is smaller, ignore the right half
18. right = mid - 1;
19. }
20. }
21. return -1; // Return -1 if the target element is not found
22. }
23.
24. int main() {
25. int arr[] = {11, 29, 37, 45, 59};
26. int size = sizeof(arr) / sizeof(arr[0]);
27. int target = 45;
28.
29. int result = binarySearch(arr, size, target);
30.
31. if (result != -1) {
32. printf("Element %d found at index %d\n", target, result);
33. } else {
34. printf("Element %d not found in the array\n", target);
35. }
36.
37. return 0;
38. }
P a g e | 20
Output:
P a g e | 21
Experiment: 9
WAP to sort a list of numbers using bubble sort.
1. #include <stdio.h>
2. // Function to perform bubble sort
3. void bubbleSort(int arr[], int size) {
4. for (int i = 0; i < size - 1; i++) {
5. for (int j = 0; j < size - 1 - i; j++) {
6. if (arr[j] > arr[j + 1]) {
7. // Swap arr[j] and arr[j + 1]
8. int temp = arr[j];
9. arr[j] = arr[j + 1];
10. arr[j + 1] = temp;
11. }
12. }
13. }
14. }
15.
16. // Function to print the array
17. void printArray(int arr[], int size) {
18. for (int i = 0; i < size; i++) {
19. printf("%d ", arr[i]);
20. }
21. printf("\n");
22. }
23.
24. int main() {
25. int arr[] = {64, 34, 25, 12, 22, 11, 90};
26. int size = sizeof(arr) / sizeof(arr[0]);
27.
28. printf("Original array: \n");
29. printArray(arr, size);
30.
31. bubbleSort(arr, size);
32.
33. printf("Sorted array: \n");
34. printArray(arr, size);
35.
36. return 0;
37. }
P a g e | 22
Output:
P a g e | 23
Experiment: 10
WAP to sort a list of numbers using selection sort.
1. #include <stdio.h>
2.
3. // Function to perform bubble sort
4. void bubbleSort(int arr[], int size) {
5. for (int i = 0; i < size - 1; i++) {
6. for (int j = 0; j < size - 1 - i; j++) {
7. if (arr[j] > arr[j + 1]) {
8. // Swap arr[j] and arr[j + 1]
9. int temp = arr[j];
10. arr[j] = arr[j + 1];
11. arr[j + 1] = temp;
12. }
13. }
14. }
15. }
16.
17. // Function to print the array
18. void printArray(int arr[], int size) {
19. for (int i = 0; i < size; i++) {
20. printf("%d ", arr[i]);
21. }
22. printf("\n");
23. }
24.
25. int main() {
26. int arr[] = {164, 314, 252, 125, 222, 131, 190};
27. int size = sizeof(arr) / sizeof(arr[0]);
28.
29. printf("Original array: \n");
30. printArray(arr, size);
31.
32. bubbleSort(arr, size);
33.
34. printf("Sorted array: \n");
35. printArray(arr, size);
36. return 0;
37. }
P a g e | 24
Output:
P a g e | 25
Experiment:11
WAP to sort a list of numbers using insertion sort.
1. #include <stdio.h>
2.
3. // Function to perform insertion sort
4. void insertionSort(int arr[], int size) {
5. for (int i = 1; i < size; i++) {
6. int key = arr[i];
7. int j = i - 1;
8.
9. // Move elements of arr[0..i-1], that are greater than key,
10. // to one position ahead of their current position
11. while (j >= 0 && arr[j] > key) {
12. arr[j + 1] = arr[j];
13. j--;
14. }
15. arr[j + 1] = key;
16. }
17. }
18. // Function to print the array
19. void printArray(int arr[], int size) {
20. for (int i = 0; i < size; i++) {
21. printf("%d ", arr[i]);
22. }
23. printf("\n");
24. }
25.
26. int main() {
27. int arr[] = {12, 11, 13, 5, 6};
28. int size = sizeof(arr) / sizeof(arr[0]);
29.
30. printf("Original array: \n");
31. printArray(arr, size);
32. insertionSort(arr, size);
33. printf("Sorted array: \n");
34. printArray(arr, size);
35.
36. return 0;
37. }
P a g e | 26
Output:
P a g e | 27
Experiment:12
WAP to sort a list of numbers using Quick sort.
1. #include <stdio.h>
2.
3. // Function to swap two elements
4. void swap(int* a, int* b) {
5. int temp = *a;
6. *a = *b;
7. *b = temp;
8. }
9.
10. // Function to partition the array
11. int partition(int arr[], int low, int high) {
12. int pivot = arr[high];
13. int i = (low - 1);
14.
15. for (int j = low; j <= high - 1; j++) {
16. if (arr[j] < pivot) {
17. i++;
18. swap(&arr[i], &arr[j]);
19. }
20. }
21. swap(&arr[i + 1], &arr[high]);
22. return (i + 1);
23. }
24.
25. // Function to perform quicksort
26. void quickSort(int arr[], int low, int high) {
27. if (low < high) {
28. int pi = partition(arr, low, high);
29.
30. quickSort(arr, low, pi - 1);
31. quickSort(arr, pi + 1, high);
32. }
33. }
34.
35. // Function to print the array
36. void printArray(int arr[], int size) {
37. for (int i = 0; i < size; i++) {
38. printf("%d ", arr[i]);
P a g e | 28
39. }
40. printf("\n");
41. }
42.
43. int main() {
44. int arr[] = {10, 17, 82, 59, 41, 35};
45. int size = sizeof(arr) / sizeof(arr[0]);
46.
47. printf("Original array: \n");
48. printArray(arr, size);
49.
50. quickSort(arr, 0, size - 1);
51.
52. printf("Sorted array: \n");
53. printArray(arr, size);
54.
55. return 0;
56. }
Output:
P a g e | 29
Experiment: 13
WAP for implementation of addition of two polynomials
Using Linked List.
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. // Define a node structure
5. struct Node {
6. int coeff; // Coefficient of the polynomial term
7. int exp; // Exponent of the polynomial term
8. struct Node* next;
9. };
10.
11. // Function to create a new node
12. struct Node* createNode(int coeff, int exp) {
13. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
14. if (!newNode) {
15. printf("Memory allocation failed\n");
16. exit(1);
17. }
18. newNode->coeff = coeff;
19. newNode->exp = exp;
20. newNode->next = NULL;
21. return newNode;
22. }
23.
24. // Function to insert a node in the linked list
25. void insertNode(struct Node** head, int coeff, int exp) {
26. struct Node* newNode = createNode(coeff, exp);
27. newNode->next = *head;
28. *head = newNode;
29. }
30.
31. // Function to add two polynomials and return the result as a linked list
32. struct Node* addPolynomials(struct Node* poly1, struct Node* poly2) {
33. struct Node* result = NULL;
34. struct Node* temp1 = poly1;
35. struct Node* temp2 = poly2;
36.
P a g e | 30
Output:
P a g e | 33
Experiment: 14
WAP to sort a list of numbers using recursive function.
1. #include <stdio.h>
2.
3. // Function to merge two subarrays
4. void merge(int arr[], int left, int mid, int right) {
5. int n1 = mid - left + 1;
6. int n2 = right - mid;
7.
8. int leftArr[n1], rightArr[n2];
9.
10. for (int i = 0; i < n1; i++)
11. leftArr[i] = arr[left + i];
12. for (int i = 0; i < n2; i++)
13. rightArr[i] = arr[mid + 1 + i];
14.
15. int i = 0, j = 0, k = left;
16.
17. while (i < n1 && j < n2) {
18. if (leftArr[i] <= rightArr[j]) {
19. arr[k] = leftArr[i];
20. i++;
21. } else {
22. arr[k] = rightArr[j];
23. j++;
24. }
25. k++;
26. }
27.
28. while (i < n1) {
29. arr[k] = leftArr[i];
30. i++;
31. k++;
32. }
33.
34. while (j < n2) {
35. arr[k] = rightArr[j];
36. j++;
37. k++;
38. }
P a g e | 34
39. }
40.
41. // Function to perform merge sort recursively
42. void mergeSort(int arr[], int left, int right) {
43. if (left < right) {
44. int mid = left + (right - left) / 2;
45.
46. mergeSort(arr, left, mid);
47. mergeSort(arr, mid + 1, right);
48.
49. merge(arr, left, mid, right);
50. }
51. }
52.
53. // Function to print the array
54. void printArray(int arr[], int size) {
55. for (int i = 0; i < size; i++) {
56. printf("%d ", arr[i]);
57. }
58. printf("\n");
59. }
60.
61. int main() {
62. int arr[] = {121, 131, 153, 175,216, 317};
63. int size = sizeof(arr) / sizeof(arr[0]);
64.
65. printf("Original array: \n");
66. printArray(arr, size);
67.
68. mergeSort(arr, 0, size - 1);
69.
70. printf("Sorted array: \n");
71. printArray(arr, size);
72.
73. return 0;
74. }
P a g e | 35
Output:
P a g e | 36
Experiment:15
WAP to sort a list of numbers using non recursive function.
1. #include <stdio.h>
2.
3. // Function to perform selection sort
4. void selectionSort(int arr[], int size) {
5. for (int i = 0; i < size - 1; i++) {
6. int minIndex = i;
7. for (int j = i + 1; j < size; j++) {
8. if (arr[j] < arr[minIndex]) {
9. minIndex = j;
10. }
11. }
12. // Swap the found minimum element with the first element
13. int temp = arr[minIndex];
14. arr[minIndex] = arr[i];
15. arr[i] = temp;
16. }
17. }
18.
19. // Function to print the array
20. void printArray(int arr[], int size) {
21. for (int i = 0; i < size; i++) {
22. printf("%d ", arr[i]);
23. }
24. printf("\n");
25. }
26. int main() {
27. int arr[] = {64, 25, 12, 22, 11};
28. int size = sizeof(arr) / sizeof(arr[0]);
29.
30. printf("Original array: \n");
31. printArray(arr, size);
32.
33. selectionSort(arr, size);
34. printf("Sorted array: \n");
35. printArray(arr, size);
36.
37. return 0;
38. }
P a g e | 37
Output:
.
P a g e | 38
Experiment: 16
WAP in C to create a 2D array and display it.
1. #include <stdio.h>
2.
3. int main() {
4. int rows, cols;
5.
6. // Get the number of rows and columns from the user
7. printf("Enter number of rows: ");
8. scanf("%d", &rows);
9. printf("Enter number of columns: ");
10. scanf("%d", &cols);
11.
12. // Create a 2D array
13. int array[rows][cols];
14.
15. // Input elements in the 2D array
16. printf("Enter elements of the array:\n");
17. for(int i = 0; i < rows; i++) {
18. for(int j = 0; j < cols; j++) {
19. printf("Element [%d][%d]: ", i, j);
20. scanf("%d", &array[i][j]);
21. }
22. }
23.
24. // Display elements of the 2D array
25. printf("The 2D array is:\n");
26. for(int i = 0; i < rows; i++) {
27. for(int j = 0; j < cols; j++) {
28. printf("%d ", array[i][j]);
29. }
30. printf("\n");
31. }
32.
33. return 0;
34. }
P a g e | 39
Output:
P a g e | 40
Experiment:17
WAP in C to implement the push and pop operation using
array in stack.
1. #include<stdio.h>
2. #define n 5
3. int stack[n];
4. int top = -1;
5.
6. void push(){
7. int x;
8. printf("Enter data\n");
9. scanf("%d", &x);
10. if(top == n-1){
11. printf("Overflow\n");
12. }
13. else{
14. top++;
15. stack[top] = x;
16. }
17. }
18.
19. void pop(){
20. int item;
21. if(top == -1){
22. printf("Underflow\n");
23. }
24. else{
25. item = stack[top];
26. top--;
27. printf("%d", item);
28. }
29. }
30.
31. void peek(){
32. if(top == -1){
33. printf("Stacks is impty\n");
34. }
35. else{
36. printf("%d", stack[top]);
37. }
P a g e | 41
38. }
39.
40. void display(){
41. for(int i=top; i>=0; i--){
42. printf("%d\t", stack[i]);
43. }
44. }
45. int main(){
46. int ch;
47. //clrscr();
48. printf("Enter chose: 1 for Push, 2 for pop, 3 for peek, 4 for display\n");
49. do{
50. scanf("%d", &ch);
51. switch(ch){
52. case 1:
53. push();
54. break;
55. case 2:
56. pop();
57. break;
58. case 3:
59. peek();
60. break;
61. case 4:
62. display();
63. break;
64. default:
65. printf("Invelid choice");
66. }
67. }
68.
69. while(ch != 0);
70. return 0 ;
71. }
P a g e | 42
Output:
P a g e | 43
Experiment: 18
WAP to convert infix into postfix expression using stack.
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <ctype.h>
4. #include <string.h>
5.
6. // Function to return precedence of operators
7. int prec(char c) {
8.
9. if (c == '^')
10. return 3;
11. else if (c == '/' || c == '*')
12. return 2;
13. else if (c == '+' || c == '-')
14. return 1;
15. else
16. return -1;
17. }
18.
19. // Function to perform infix to postfix conversion
20. void infixToPostfix(char* exp) {
21. int len = strlen(exp);
22. char result[len + 1];
23. char stack[len];
24. int j = 0;
25. int top = -1;
26.
27. for (int i = 0; i < len; i++) {
28. char c = exp[i];
29.
30. // If the scanned character is
31. // an operand, add it to the output string.
32. if (isalnum(c))
33. result[j++] = c;
34.
35. // If the scanned character is
36. // an ‘(‘, push it to the stack.
37. else if (c == '(')
38. stack[++top] = '(';
39.
40. // If the scanned character is an ‘)’,
41. // pop and add to the output string from the stack
42. // until an ‘(‘ is encountered.
P a g e | 44
Output:
P a g e | 45
Experiment: 19
WAP for insertion, deletion and traversing in double ended Queue.
1. #include <stdio.h>
2. #include <stdlib.h>
3. #define MAX 5 // Define maximum size of the deque
4.
5. int deque[MAX];
6. int front = -1;
7. int rear = -1;
8.
9. // Function to check if the deque is full
10. int isFull() {
11. return ((front == 0 && rear == MAX - 1) || (front == rear + 1));
12. }
13. // Function to check if the deque is empty
14. int isEmpty() {
15. return (front == -1);
16. }
17. // Function to insert an element at the front of the deque
18. void insertFront(int key) {
19. if (isFull()) {
20. printf("Overflow: Unable to insert element at the front. Deque is full.\n");
21. return;
22. }
23. if (front == -1) { // If deque is initially empty
24. front = 0;
25. rear = 0;
26. } else if (front == 0) {
27. front = MAX - 1; // wrap around
28. } else {
29. front = front - 1;
30. }
31. deque[front] = key;
32. printf("Inserted %d at the front.\n", key);
33. }
34. // Function to insert an element at the rear of the deque
35. void insertRear(int key) {
36. if (isFull()) {
37. printf("Overflow: Unable to insert element at the rear. Deque is full.\n");
38. return;
P a g e | 46
39. }
40. if (rear == -1) { // If deque is initially empty
41. front = 0;
42. rear = 0;
43. } else if (rear == MAX - 1) {
44. rear = 0; // wrap around
45. } else {
46. rear = rear + 1;
47. }
48. deque[rear] = key;
49. printf("Inserted %d at the rear.\n", key);
50. }
51. // Function to delete an element from the front of the deque
52. void deleteFront() {
53. if (isEmpty()) {
54. printf("Underflow: Unable to delete element from the front. Deque is empty.\n");
55. return;
56. }
57. int removed = deque[front];
58.
59. if (front == rear) { // Deque has only one element
60. front = -1;
61. rear = -1;
62. } else if (front == MAX - 1) {
63. front = 0; // wrap around
64. } else {
65. front = front + 1;
66. }
67. printf("Deleted %d from the front.\n", removed);
68. }
69. // Function to delete an element from the rear of the deque
70. void deleteRear() {
71. if (isEmpty()) {
72. printf("Underflow: Unable to delete element from the rear. Deque is empty.\n");
73. return;
74. }
75. int removed = deque[rear];
76.
77. if (front == rear) { // Deque has only one element
78. front = -1;
79. rear = -1;
80. } else if (rear == 0) {
81. rear = MAX - 1; // wrap around
82. } else {
P a g e | 47
Output:
P a g e | 49
Experiment: 20
WAP for insertion an element using circular Queue.
1. // C Program to implement the circular queue in c using arrays
2. #include <stdio.h>
3. // Define the maximum size of the queue
4. #define MAX_SIZE 5
5. // Declare the queue array and front, rear variables
6. int queue[MAX_SIZE];
7. int front = -1, rear = -1;
8. // Function to check if the queue is full
9. int isFull()
10. {
11. // If the next position is the front, the queue is full
12. return (rear + 1) % MAX_SIZE == front;
13. }
14. // Function to check if the queue is empty
15. int isEmpty()
16. {
17. // If the front hasn't been set, the queue is empty
18. return front == -1;
19. }
20. // Function to enqueue (insert) an element
21. void enqueue(int data)
22. {
23. // If the queue is full, print an error message and
24. // return
25. if (isFull()) {
26. printf("Queue overflow\n");
27. return;
28. }
29. // If the queue is empty, set the front to the first
30. // position
31. if (front == -1) {
32. front = 0;
33. }
34. // Add the data to the queue and move the rear pointer
35. rear = (rear + 1) % MAX_SIZE;
36. queue[rear] = data;
37. printf("Element %d inserted\n", data);
38. }
P a g e | 50
83. {
84. // Enqueue some elements
85. enqueue(10);
86. enqueue(20);
87. enqueue(30);
88. // Display the queue
89. display();
90.
91. // Dequeue an element and print it
92. printf("Dequeued element: %d\n", dequeue());
93. // Display the queue again
94. display();
95.
96. // End of main function
97. return 0;
98. }
Output: