-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep49_queue_2.cpp
More file actions
90 lines (81 loc) · 1.97 KB
/
practicep49_queue_2.cpp
File metadata and controls
90 lines (81 loc) · 1.97 KB
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
#include <iostream>
using namespace std;
//Queue using linked list
class Node{
public:
int data;
Node *next;
Node(int data){
this->data = data;
this->next = NULL;
}
};
void enqueue(Node* &head, Node* &tail, int data){
cout << "Customer ID " << data << " is enqueued" << endl;
Node* newNode = new Node(data);
if(head == NULL){
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
void dequeue(Node* &head, Node* &tail){
if(head == NULL || tail == NULL){
cout << "Queue is empty" << endl;
return;
}
cout << "Dequeued customer ID: " << head->data << endl;
Node* temp = head;
head = head->next;
delete temp;
if(head==NULL){ //if no elements in queue after dequeueing all.
tail = NULL;
cout<<"No more elements"<<endl;
}
}
void display(Node* head,Node* &tail){
if(head == NULL || tail==NULL){
cout << "Queue is empty" << endl;
return;
}
cout << "Customer IDs in the queue are: ";
Node* temp = head;
while(temp != NULL){
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main(){
int choice, data;
Node* head = NULL; //front
Node* tail = NULL; //rear
cout<<"Enter 1 for insertion"<< endl;
cout<<"Enter 2 for deletion"<< endl;
cout<<"Enter 3 for display"<< endl;
cout<<"Enter 4 to exit"<< endl;
while(true){
cout << "Enter your choice: ";
cin >> choice;
if(choice == 1){
cout<<"Enter value to insert: ";
cin >> data;
enqueue(head, tail, data);
}
else if(choice == 2){
dequeue(head, tail);
}
else if(choice == 3){
display(head,tail);
}
else if(choice == 4){
cout<<"Exiting...";
return 0;
}
else{
cout << "Invalid option" << endl;
}
}
return 0;
}