-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuesLinkedList.cpp
More file actions
75 lines (66 loc) · 1.63 KB
/
queuesLinkedList.cpp
File metadata and controls
75 lines (66 loc) · 1.63 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
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
void Enqueue(Node* head, int num){
Node* link = head;
while(link->next != NULL){
link = link->next;
}
link->next = new Node;
link = link->next;
link->data = num;
}
Node* Dequeue(Node* head){
if(head == NULL){
return 0;
} else {
cout<<"Dequeued element is "<<head->data<<endl;
head = head->next;
//cout<<"*"<<head->data<<endl;
return head;
}
}
void printLinkedList(Node* head){
Node* link = head;
while(link != NULL){
cout<<link->data<<" ";
link = link->next;
}
}
int main() {
Node* head = new Node();
int array[] = {7,5,2,1,9,7};
int n = (sizeof(array))/sizeof(array[0]);
Node* link = head;
for(int i = 0; i < n; i++){
link->data = array[i];
if(i < (n-1)){
link->next = new Node();
link = link->next;
}
}
int choice = 0;
while(choice != 3){
cout<<"Enter 1 to enqueue a number , 2 to dequeue and 3 to exit ";
cin>>choice;
if(choice == 1){
int numToEnqueue;
cout<<"Enter a number to enqueue ";
cin>>numToEnqueue;
Enqueue(head, numToEnqueue);
printLinkedList(head);
} else if(choice == 2){
head = Dequeue(head);
if(!head){
cout<<"Nothing to Dequeue, linked list is empty "<<endl;
}
printLinkedList(head);
} else {
cout<<endl<<"Exiting "<<endl;
break;
}
}
}