-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS06_QueuesUsingLL.cpp
101 lines (98 loc) · 2.73 KB
/
DS06_QueuesUsingLL.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
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int CurrData){
this->data=CurrData;
this->next=NULL;
}
};
class Queues_LinkedList{
private:
Node* head;
Node* tail;
int size;
int Capacity;
public:
Queues_LinkedList(int c){
this->Capacity=c;
this->size=0;
this->head=NULL;
this->tail=NULL;
}
void enqueue(int data){
if (this->size == this->Capacity){
cout<<"QUEUE OVERFLOW ::: QUEUE IS FULL"<<endl;
return;
}
Node* NewNode=new Node(data);
if(this->head==NULL){
this->head=this->tail=NewNode;
size++;
return;
}
this->tail->next=NewNode;
this->tail=NewNode;
size++;
}
int dequeue(){
if (this->head==NULL){
cout<<"QUEUE UNDERFLOW ::: QUEUE IS EMPTY"<<endl;
return (-1);
}
Node* temp=this->head;
int Value=temp->data;
this->head=this->head->next;
temp->next=NULL;
this->size--;
free(temp);
return Value;
}
int PeekFront(){
if (this->head==NULL){
cout<<"QUEUE UNDERFLOW ::: QUEUE IS EMPTY"<<endl;
return (-1);
}
return this->head->data;
}
int PeekLast(){
if (this->head==NULL){
cout<<"QUEUE UNDERFLOW ::: QUEUE IS EMPTY"<<endl;
return (-1);
}
return this->tail->data;
}
void PrintQueue(){
cout<<"THE QUEUE IS ::: FRONT--> ";
Node* temp=head;
while(temp->next!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<" <--REAR"<<endl;
}
int Size(){ return this->size; }
bool isEmpty(){ return this->head==NULL; }
bool isFull(){ return this->size==this->Capacity; }
};
int main(){
Queues_LinkedList qll(10);
qll.enqueue(1);
qll.enqueue(5);
qll.enqueue(3);
qll.enqueue(4);
qll.enqueue(2);
qll.enqueue(6);
qll.PrintQueue();
qll.dequeue();
qll.PrintQueue();
cout<<"THE FRONT OF THE QUEUE IS ::: "<<qll.PeekFront()<<endl;
cout<<"THE LAST OF THE QUEUE IS ::: "<<qll.PeekLast()<<endl;
cout<<"QUEUE IS EMPTY ::: "<<qll.isEmpty()<<endl;
cout<<"QUEUE IS FULL ::: "<<qll.isFull()<<endl;
cout<<"SIZE OF QUEUE IS ::: "<<qll.Size()<<endl;
cout<<endl;
return 0;
}