This repository was archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 740
/
Copy pathQueueUsingLL.cpp
107 lines (99 loc) · 2.14 KB
/
QueueUsingLL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// FIFO Mechanism
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
// Function to insert values in the end in the Queue
node *add(node *rear, int val)
{
node *x;
x = new node;
x->data = val;
x->next = NULL;
if (rear != NULL)
rear->next = x;
rear = x;
return rear;
}
// Function to delete values from the front in the Queue
node *del(node *front, int &val)
{
if (front == NULL)
{
cout << "Queue is Empty..!" << endl; // Prints if the queue is empty
val = -9999;
}
else
{
node *x;
x = front;
front = front->next;
val = x->data; // Storing the deleted value in 'val'
delete x;
}
return front;
}
// Function to print values in the Queue
void showqueue(node *front)
{
node *ptr;
ptr = front;
if (ptr == NULL)
{
cout << "Queue is empty..!" << endl; // Prints if the queue is empty
}
else
{
cout << "Queue is " << endl;
while (ptr != NULL)
{
cout << ptr->data << "\t";
ptr = ptr->next;
}
cout << endl;
}
}
int main()
{
int choice, val;
node *front, *rear;
front = rear = NULL;
// Menu Driven Program
do
{
cout << "\nMain Menu\n"
<< "1. Insert\n"
<< "2. Delete\n"
<< "3. Show\n"
<< "4. Exit\n"
<< "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value to be added: ";
cin >> val;
rear = add(rear, val);
if (front == NULL)
front = rear;
break;
case 2:
front = del(front, val);
if (val != -9999)
cout << "The deleted value is: " << val << endl;
if (front == NULL)
rear = front;
break;
case 3:
showqueue(front);
break;
default:
break;
}
} while (choice != 4); // Program exits whenever the user enters no. 4
return 0;
}
// Author - Ankit Goel