forked from SarahN18/Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
101 lines (82 loc) · 1.48 KB
/
queue.cpp
File metadata and controls
101 lines (82 loc) · 1.48 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
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
#include<algorithm>
using namespace std;
struct Node{
int data;
Node *next;
};
class Qlist{
Node *front, *rear;
public:
Qlist(){
front = rear = NULL;
}
bool isEmpty();
void insert(int);
void del();
void display();
};
bool Qlist:: isEmpty(){
if(front==NULL && rear==NULL) return true;
return false;
}
void Qlist:: insert(int item){
Node *curr = new Node;
if(curr==NULL){
cout<<"Heap Full"<<endl;
return;
}
curr->data=item;
curr->next=NULL;
if(rear==NULL){
front=rear=curr;
return;
}
rear->next=curr;
rear=curr;
}
void Qlist::del(){
Node *temp=front;
front=front->next;
delete temp;
temp=NULL;
}
void Qlist:: display(){
if(!isEmpty()){
Node *temp=front;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
return;
}
int main(){
Qlist ql;
int choice,item;
while(true){
cout<<"\n\n";
cout<<"Press 1 to check if Queue is empty"<<endl;
cout<<"Press 2 to push an item to Queue"<<endl;
cout<<"Press 3 to pop an item from Queue"<<endl;
cout<<"Press 4 to display the Queue"<<endl;
cout<<"Enter choice ->";
cin>>choice;
switch(choice){
case 1: if(ql.isEmpty()==0) cout<<"False"<<endl;
else cout<<"True"<<endl;
break;
case 2: cout<<"Enter an item ->";
cin>>item;
ql.insert(item);
break;
case 3: ql.del();
break;
case 4: cout<<"The Queue ->";
ql.display();
break;
default:cout<<"Invalid Choice"<<endl;
exit(0);
}
}
}