-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueCpp.h
More file actions
73 lines (64 loc) · 1.01 KB
/
QueueCpp.h
File metadata and controls
73 lines (64 loc) · 1.01 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
#ifndef QueueCpp_h
#define QueueCpp_h
struct Node
{
Node* lchild;
Node* rchild;
int data;
};
class Queue
{
private:
int size;
int front;
int rear;
Node** Q;
public:
Queue(int size)
{
this->size = size;
front = rear = 0;
Q = new Node*[this->size];
}
void enqueue(Node* value);
Node* dequeue();
bool isEmpty();
void display();
};
void Queue::enqueue(Node* value)
{
if((rear+1)%size == front)
std::cout<<"Queue is full \n";
else
{
rear = (rear+1)%size;
Q[rear] = value;
}
}
Node* Queue::dequeue()
{
Node* x = NULL;
if(rear == front)
std::cout<<"Queue is empty \n";
else
{
front = (front+1)%size;
x = Q[front];
}
return x;
}
bool Queue::isEmpty()
{
return front == rear;
}
void Queue::display()
{
int i = front+1;
do
{
std::cout<<Q[i]<<" ";
i = (i+1)%size;
}while((i!=rear+1)%size);
std::cout<<"\n";
}
#endif /* QueueCpp_h */