-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue_Using_Linked_List.cpp
100 lines (83 loc) Β· 2.97 KB
/
Queue_Using_Linked_List.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
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
};
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Display(Node* n){
if (n == NULL){
cout << "Queue is Empty";
return;
}
while (n != NULL){
cout << n->data << " ";
n = n->next;
}
cout << endl;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void EnQueue(Node** head_ref, int new_data){
// 1. allocate node
Node* new_node = new Node();
Node *last = *head_ref;
// 2. Put in the data
new_node->data = new_data;
// 3. This new node is going to be the last node, so make next of it as NULL
new_node->next = NULL;
// 4. If the Linked List is empty, then make the new node as head
if (*head_ref == NULL){
*head_ref = new_node;
return;
}
// 5. Else traverse till the last node
while (last->next != NULL)
last = last->next;
// 6. Change the next of last node
last->next = new_node;
return;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void DeQueue(Node** head_ref){
// Store head node
Node* temp = *head_ref;
if (temp == NULL){
cout << "Queue is Empty";
return;
}
*head_ref = temp->next; // Changed head
delete temp; // free old head
return;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int main(){
Node *head = NULL;
int ch, a;
cout << "1 βββββ> EnQueue \n";
cout << "2 βββββ> DeQueue \n";
cout << "3 βββββ> Display \n";
cout << "4 βββββ> Exit\n";
do{
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch){
case (1):
cout << "Enter the value to be EnQueue to the Queue : ";
cin >> a;
EnQueue(&head, a);
break;
case (2):
DeQueue(&head);
break;
case (3):
Display(head);
break;
case (4):
break;
default:
cout << "Enter a value between 1 and 4";
}
}while(ch != 4);
return 0;
}