DS (RM) Program - Stack And Queue: Queue – Link List Implementation
Output:
Enter the number of values to be pushed into queue
3
Enter the value to be entered into queue
12
Enter the value to be entered into queue
43
Enter the value to be entered into queue
61
Removed Values
12
43
61
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | #include<iostream.h> #include<conio.h> #include<process.h> struct node { int info; node *next; node *prev; }*head, *tail; class dqueue { public: int top1, top2; void insert() { struct node *temp; int ch, value; if (top1 + top2 >= 50) { cout<<"Dequeue Overflow"<<endl; return; } if (top1 + top2 == 0) { cout<<"Enter the value to be inserted: "; cin>>value; head = new (struct node); head->info = value; head->next = NULL; head->prev = NULL; tail = head; top1++; cout<<"Element Inserted into empty deque"<<endl; } else { while (1) { cout<<endl; cout<<"1.Insert Element at first"<<endl; cout<<"2.Insert Element at last"<<endl; cout<<"3.Exit"<<endl; cout<<endl; cout<<"Enter Your Choice: "; cin>>ch; cout<<endl; switch(ch) { case 1: cout<<"Enter the value to be inserted: "; cin>>value; temp = new (struct node); temp->info = value; temp->next = head; temp->prev = NULL; head->prev = temp; head = temp; top1++; break; case 2: cout<<"Enter the value to be inserted: "; cin>>value; temp = new (struct node); temp->info = value; temp->next = NULL; temp->prev = tail; tail->next = temp; tail = temp; top2++; break; case 3: return; break; default: cout<<"Wrong Choice"<<endl; } } } } void del() { if (top1 + top2 <= 0) { cout<<"Deque Underflow"<<endl; return; } int ch; while (1) { cout<<endl; cout<<"1.Delete Element at first"<<endl; cout<<"2.Delete Element at last"<<endl; cout<<"3.Exit"<<endl; cout<<endl; cout<<"Enter Your Choice: "; cin>>ch; cout<<endl; switch(ch) { case 1: head = head->next; head->prev = NULL; top1--; break; case 2: tail = tail->prev; tail->next = NULL; top2--; break; case 3: return; break; default: cout<<"Wrong Choice"<<endl; } } } void display() { struct node *temp; int ch; if (top1 + top2 <= 0) { cout<<"Deque Underflow"<<endl; return; } while (1) { cout<<endl; cout<<"1.Display Deque from Beginning"<<endl; cout<<"2.Display Deque from End"<<endl; cout<<"3.Exit"<<endl; cout<<endl; cout<<"Enter Your Choice: "; cin>>ch; cout<<endl; switch (ch) { case 1: temp = head; cout<<"Deque from Beginning:"<<endl; while (temp != NULL) { cout<<temp->info<<" "; temp = temp->next; } cout<<endl; break; case 2: cout<<"Deque from End:"<<endl; temp = tail; while (temp != NULL) { cout<<temp->info<<" "; temp = temp->prev; } temp = tail; cout<<endl; break; case 3: return; break; default: cout<<"Wrong Choice"<<endl; } } } dqueue() { top1 = 0; top2 = 0; head = NULL; tail = NULL; } }; int main() { int choice; dqueue dl; while (1) { cout<<"\n-------------"<<endl; cout<<"Operations on Deque"<<endl; cout<<"\n-------------"<<endl; cout<<"1.Insert Element into the Deque"<<endl; cout<<"2.Delete Element from the Deque"<<endl; cout<<"3.Traverse the Deque"<<endl; cout<<"4.Quit"<<endl; cout<<"Enter your Choice: "; cin>>choice; cout<<endl; switch(choice) { case 1: dl.insert(); break; case 2: dl.del(); break; case 3: dl.display(); break; case 4: exit(1); break; default: cout<<"Wrong Choice"<<endl; } } return 0; } |
Output:
Enter the number of values to be pushed into queue
3
Enter the value to be entered into queue
12
Enter the value to be entered into queue
43
Enter the value to be entered into queue
61
Removed Values
12
43
61
