forked from iam-abbas/cs-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue with Doubly LL.cpp
107 lines (107 loc) · 2.02 KB
/
Queue with Doubly LL.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
//Queue with Doubly Linked list
#include<bits/stdc++.h>
using namespace std;
class Node
{
public:
int data ;
Node *next = NULL;
Node *prev = NULL;
Node(int data)
{
this->data = data ;
}
};
class Queue
{
Node* front = NULL;
Node* rear = NULL;
int size = 0 ;
public :
void enqueue(int data)
{
Node *new_node = new Node(data) ;
if(front == NULL)
{
front = new_node;
rear = new_node ;
}
else
{
rear->next = new_node ;
new_node->prev = rear ;
rear = new_node ;
}
size++;
}
void dequeue()
{
if(front == NULL)
{
cout<<"Queue is Already Empty !"<<endl;
}
else
{
Node *x = front ;
front = front->next ;
x->next = NULL ;
x->prev = NULL ;
if(front != NULL)
front->prev = NULL ;
delete x ;
size-- ;
}
}
bool isEmpty()
{
if(size == 0)
return true ;
else
return false ;
}
int Size()
{
return size ;
}
int Front()
{
if(front == NULL || size == 0)
{
cout<<"Queue is Empty"<<endl ;
return -1 ;
}
else
{
return front->data ;
}
}
void print()
{
Node *temp = front ;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next ;
}
cout<<endl ;
}
};
int main()
{
Queue q ;
q.enqueue(1) ;
q.enqueue(2) ;
q.print() ;
cout<<"Front Element is : "<<q.Front()<<endl ;
cout<<"Size of Queue : "<<q.Size()<<endl ;
q.dequeue() ;
q.print() ;
cout<<"Front Element is : "<<q.Front()<<endl ;
cout<<"Size of Queue : "<<q.Size()<<endl ;
q.dequeue() ;
q.print() ;
cout<<"Front Element is : "<<q.Front()<<endl ;
cout<<"Size of Queue : "<<q.Size()<<endl ;
q.dequeue() ;
return 0 ;
}