-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue_main.cpp
116 lines (107 loc) · 2.35 KB
/
Queue_main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
using namespace std;
class Node{
public :
int data;
Node* ptr;
Node(int value){
this->data = value;
this->ptr = nullptr;
}
};
class Queue{
private:
Node* first;
Node* last;
public:
Queue(){
this->first = nullptr;
this->last = nullptr;
}
bool isempty(){
return(first == nullptr);
}
void enqueue(int item){
Node* newnode = new Node(item);
if (isempty()){
first = newnode;
last = newnode;
}
else{
last->ptr = newnode;
last = newnode;
}
}
void dequeue(){
if (isempty()){
cout<< "the queue is empty\n";
}
else if(first == last && first != nullptr){
Node* delptr = first;
first = nullptr;
last = nullptr;
delete delptr;
}
else{
Node* delptr = first;
first = first->ptr;
delete delptr;
}
}
void display(){
if (isempty()){
cout<< " the queue is empty "<<endl;
}
else{
Node* temp = first;
while(temp != nullptr){
cout<< temp->data<<" ";
temp = temp->ptr;
}
cout<< endl;
delete temp;
}
}
int getfirst(){
return first->data;
}
int getlast(){
return last->data;
}
int countnodes(){
Node* temp = first;
int counter = 0;
while(temp!= nullptr){
counter++;
temp =temp->ptr;
}
return counter;
}
void clearnodes(){
while(!isempty()){
dequeue();
}
}
} ;
int main(){
int input;
int noofvalues;
Queue q1;
cout<< q1.isempty()<<endl;
cout<< " the no of values to add"<<endl;
cin>>noofvalues;
for(int i =0 ; i< noofvalues ; i++){
cout<< " the value no " << i+1 <<endl;
cin>>input;
q1.enqueue(input);
}
q1.display();
q1.dequeue();
cout<< "after deletion \n";
q1.display();
cout<<q1.getfirst()<< " is the first value" <<endl;
cout<<q1.getlast()<< " is the last value "<<endl;
q1.clearnodes();
q1.display();
return 0;
}