0% found this document useful (0 votes)
5 views

5.2 Queue Prog by Array - Link List

Uploaded by

Anshul Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

5.2 Queue Prog by Array - Link List

Uploaded by

Anshul Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

/* C Program to Implement a Queue using an Array


2. */
3. #include <stdio.h>
4.
5. #define MAX 50
6.
7. void insert();
8. void delete();
9. void display();
10. int queue_array[MAX];
11. int rear = - 1;
12. int front = - 1;
13. main()
14. {
15. int choice;
16. while (1)
17. {
18. printf("1.Insert element to queue \n");
19. printf("2.Delete element from queue \n");
20. printf("3.Display all elements of queue \n");
21. printf("4.Quit \n");
22. printf("Enter your choice : ");
23. scanf("%d", &choice);
24. switch (choice)
25. {
26. case 1:
27. insert();
28. break;
29. case 2:
30. delete();
31. break;
32. case 3:
33. display();
34. break;
35. case 4:
36. exit(1);
37. default:
38. printf("Wrong choice \n");
39. } /* End of switch */
40. } /* End of while */
41. } /* End of main() */
42.
43. void insert()
44. {
45. int add_item;
46. if (rear == MAX - 1)
47. printf("Queue Overflow \n");
48. else
49. {
50. if (front == - 1)
51. /*If queue is initially empty */
52. front = 0;
53. printf("Inset the element in queue : ");
54. scanf("%d", &add_item);
55. rear = rear + 1;
56. queue_array[rear] = add_item;
57. }
58. } /* End of insert() */
59.
60. void delete()
61. {
62. if (front == - 1 || front > rear)
63. {
64. printf("Queue Underflow \n");
65. return ;
66. }
67. else
68. {
69. printf("Element deleted from queue is : %d\n",
queue_array[front]);
70. front = front + 1;
71. }
72. } /* End of delete() */
73.
74. void display()
75. {
76. int i;
77. if (front == - 1)
78. printf("Queue is empty \n");
79. else
80. {
81. printf("Queue is : \n");
82. for (i = front; i <= rear; i++)
83. printf("%d ", queue_array[i]);
84. printf("\n");
85. }
86. } /* End of display() */

Program Explanation
1. Ask the user for the operation like insert, delete, display and exit.
2. According to the option entered, access its respective function using switch statement.
Use the variables front and rear to represent the first and last element of the queue.
3. In the function insert(), firstly check if the queue is full. If it is, then print the output as
“Queue Overflow”. Otherwise take the number to be inserted as input and store it in the
variable add_item. Copy the variable add_item to the array queue_array[] and increment
the variable rear by 1.
4. In the function delete(), firstly check if the queue is empty. If it is, then print the output
as “Queue Underflow”. Otherwise print the first element of the array queue_array[] and
decrement the variable front by 1.
5. In the function display(), using for loop print all the elements of the array starting from
front to rear.
6. Exit.

Runtime Test Cases


1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 15
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 20
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is :
15 20 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 4

Queue By LINK List


#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node* next;

};

struct node* front;

struct node* rear;

void insert(struct node *ptr, int item) {

ptr = (struct node *) malloc (sizeof(struct node));

if (ptr == NULL) {

printf("\nOVERFLOW\n");

return;
} else {

ptr -> data = item;

if (front == NULL) {

front = ptr;

rear = ptr;

front -> next = NULL;

rear -> next = NULL;

} else {

rear -> next = ptr;

rear = ptr;

rear->next = NULL;

int main() {

struct node* head = NULL;

insert(head, 10);

insert(head, 20);

printf("front element: %d", front->data);

return 0;

You might also like