DS Exp 5
DS Exp 5
2 EXPERIMENT NO. 5
3 TITLE :- Array Implementation of Linear Queue.
4 AIM :- To Implement Linear Queue ADT using Array.
5 NAME:- HARSH CHANDANSHIVE
6 UIN :- 241P122
7 ROLL NO:- 52
8 DIV :- D
9 */
10 #include <stdio.h>
11
12 #define SIZE 3
13 int front = -1, rear = -1, queue[SIZE];
14
15 int isFull() { return rear == SIZE - 1; }
16 int isEmpty() { return front == -1 || front > rear; }
17
18 void enqueue() {
19 if (isFull()) {
20 printf("Queue is at full capacity! Cannot add more elements.\n");
21 return;
22 }
23
24 int value;
25 printf("Enter a number to add to the queue: ");
26 scanf("%d", &value);
27
28 if (isEmpty()) front = 0; // Initialize front on first insertion
29
30 rear++;
31 queue[rear] = value;
32 printf("%d has been added successfully!\n", value);
33 }
34
35 void dequeue() {
36 if (isEmpty()) {
37 printf("Queue is empty! No elements to remove.\n");
38 return;
39 }
40
41 printf("Removed element: %d\n", queue[front]);
42 front++;
43
44 if (front > rear) {
45 front = -1;
46 rear = -1;
47 printf("Queue is now completely empty.\n");
48 }
49 }
50
51 void display() {
52 if (isEmpty()) {
53 printf("Queue is empty. Please insert elements first.\n");
54 return;
55 }
56
57 printf("Current Queue: [");
58 for (int i = front; i <= rear; i++) {
59 printf("%d%s", queue[i], (i == rear) ? "" : ", ");
60 }
61 printf("]\n");
62 }
63
64 int main() {
65 printf("\n=== Queue Management System ===\n");
66
67 int choice;
68 while (1) {
69 printf("\nOptions:\n");
70 printf("1. Enqueue (Insert an element)\n");
71 printf("2. Dequeue (Remove an element)\n");
72 printf("3. Display Queue\n");
73 printf("4. Exit\n");
74 printf("Enter your choice (1-4): ");
75 scanf("%d", &choice);
76
77 switch (choice) {
78 case 1: enqueue(); break;
79 case 2: dequeue(); break;
80 case 3: display(); break;
81 case 4:
82 printf("\nExiting program... Thank you!\n\n");
83 return 0;
84 default:
85 printf("Invalid input! Please select a number between 1 and 4.\n");
86 }
87 }
88 }
89 /*
90 OUTPUT
91
92 === Queue Management System ===
93
94 Options:
95 1. Enqueue (Insert an element)
96 2. Dequeue (Remove an element)
97 3. Display Queue
98 4. Exit
99 Enter your choice (1-4): 1
100 Enter a number to add to the queue: 10
101 10 has been added successfully!
102
103 Options:
104 1. Enqueue (Insert an element)
105 2. Dequeue (Remove an element)
106 3. Display Queue
107 4. Exit
108 Enter your choice (1-4): 1
109 Enter a number to add to the queue: 25
110 25 has been added successfully!
111
112 Options:
113 1. Enqueue (Insert an element)
114 2. Dequeue (Remove an element)
115 3. Display Queue
116 4. Exit
117 Enter your choice (1-4): 1
118 Enter a number to add to the queue: 50
119 50 has been added successfully!
120
121 Options:
122 1. Enqueue (Insert an element)
123 2. Dequeue (Remove an element)
124 3. Display Queue
125 4. Exit
126 Enter your choice (1-4): 1
127 Queue is at full capacity! Cannot add more elements.
128
129 Options:
130 1. Enqueue (Insert an element)
131 2. Dequeue (Remove an element)
132 3. Display Queue
133 4. Exit
134 Enter your choice (1-4): 3
135 Current Queue: [10, 25, 50]
136
137 Options:
138 1. Enqueue (Insert an element)
139 2. Dequeue (Remove an element)
140 3. Display Queue
141 4. Exit
142 Enter your choice (1-4): 2
143 Removed element: 10
144
145 Options:
146 1. Enqueue (Insert an element)
147 2. Dequeue (Remove an element)
148 3. Display Queue
149 4. Exit
150 Enter your choice (1-4): 3
151 Current Queue: [25, 50]
152
153 Options:
154 1. Enqueue (Insert an element)
155 2. Dequeue (Remove an element)
156 3. Display Queue
157 4. Exit
158 Enter your choice (1-4): 2
159 Removed element: 25
160
161 Options:
162 1. Enqueue (Insert an element)
163 2. Dequeue (Remove an element)
164 3. Display Queue
165 4. Exit
166 Enter your choice (1-4): 2
167 Removed element: 50
168 Queue is now completely empty.
169
170 Options:
171 1. Enqueue (Insert an element)
172 2. Dequeue (Remove an element)
173 3. Display Queue
174 4. Exit
175 Enter your choice (1-4): 4
176
177 Exiting program... Thank you!
178 */
179