TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
queue_using_linked_list.cpp
1#include <iostream>
2using namespace std;
3
4struct node {
5 int val;
6 node *next;
7};
8
9node *front, *rear;
10
11void Enque(int x) {
12 if (rear == NULL) {
13 node *n = new node;
14 n->val = x;
15 n->next = NULL;
16 rear = n;
17 front = n;
18 }
19
20 else {
21 node *n = new node;
22 n->val = x;
23 n->next = NULL;
24 rear->next = n;
25 rear = n;
26 }
27}
28
29void Deque() {
30 if (rear == NULL && front == NULL) {
31 cout << "\nUnderflow";
32 } else {
33 node *t = front;
34 cout << "\n" << t->val << " deleted";
35 front = front->next;
36 delete t;
37 if (front == NULL)
38 rear = NULL;
39 }
40}
41
42void show() {
43 node *t = front;
44 while (t != NULL) {
45 cout << t->val << "\t";
46 t = t->next;
47 }
48}
49
50int main() {
51 int ch, x;
52 do {
53 cout << "\n1. Enque";
54 cout << "\n2. Deque";
55 cout << "\n3. Print";
56 cout << "\nEnter Your Choice : ";
57 cin >> ch;
58 if (ch == 1) {
59 cout << "\nInsert : ";
60 cin >> x;
61 Enque(x);
62 } else if (ch == 2) {
63 Deque();
64 } else if (ch == 3) {
65 show();
66 }
67 } while (ch != 0);
68
69 return 0;
70}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
int main()
Main function.