-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueueUsingLinkedlist.cpp
91 lines (89 loc) · 2.13 KB
/
queueUsingLinkedlist.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
#include<iostream>
using namespace std;
class queueUsingLinkedList{
public:
int length=0;
struct Node{
int data;
struct Node* next;
Node(int data){
this->data= data;
this->next= NULL;
}
};
struct Node* first = NULL;
struct Node* last = NULL;
void enqueue(int data){
struct Node* temp = NULL;
temp = new Node(data);
if(first==NULL){
first = temp;
last = temp;
}
else{
last->next = temp;
last = temp;
}
length++;
}
bool isEmpty(){
if(first==NULL){
return true;
}
else{
return false;
}
}
void dequeue(){
if(isEmpty()){
cout<<"Queue is Empty"<<endl;
}
else{
first= first->next;
length--;
}
}
int peek(){
return first->data;
}
int tail(){
return last->data;
}
int size(){
return length;
}
};
int main(){
queueUsingLinkedList myQueue;
myQueue.enqueue(1);
myQueue.enqueue(2);
myQueue.enqueue(3);
myQueue.enqueue(4);
myQueue.enqueue(5);
cout<<myQueue.peek()<<endl;
cout<<myQueue.size()<<endl;
cout<<myQueue.isEmpty()<<endl;
myQueue.dequeue();
myQueue.dequeue();
myQueue.dequeue();
cout<<myQueue.peek()<<endl;
cout<<myQueue.size()<<endl;
cout<<myQueue.isEmpty()<<endl;
// second Queue Check
cout<< "Second Queue Check" <<endl;
queueUsingLinkedList secondQueue;
secondQueue.enqueue(10);
secondQueue.enqueue(22);
secondQueue.enqueue(32);
secondQueue.enqueue(44);
secondQueue.enqueue(5);
cout<<secondQueue.peek()<<endl;
cout<<secondQueue.size()<<endl;
cout<<secondQueue.isEmpty()<<endl;
secondQueue.dequeue();
secondQueue.dequeue();
secondQueue.dequeue();
cout<<secondQueue.peek()<<endl;
cout<<secondQueue.size()<<endl;
cout<<secondQueue.isEmpty()<<endl;
}