-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
117 lines (103 loc) · 1.41 KB
/
queue.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
117
#include <iostream>
#define queue_size 10
class queue
{
int *array;
int capacity;
int front;
int rear;
int count;
public:
queue(int size = queue_size);
~queue();
void enqueue(int);
void dequeue();
int peek();
int size();
bool is_empty();
bool is_full();
};
//Constructor
queue::queue(int size)
{
array = new int[size];
capacity = size;
front = 0;
rear = -1;
count = 0;
}
//Destructor
queue::~queue()
{
delete[] array;
}
//Enqueue
void queue::enqueue(int item)
{
if (is_full())
{
std::cout << "Queue overflow!" << std::endl;
exit(EXIT_FAILURE);
}
else
{
std::cout << "Inserting=> " << item << std::endl;
rear = (rear + 1) % capacity;
array[rear] = item;
count++;
}
}
//Dequeue
void queue::dequeue()
{
if (is_empty())
{
std::cout << "Queue underflow!" << std::endl;
exit(EXIT_FAILURE);
}
else
{
std::cout << "Removing=> " << array[front] << std::endl;
front = (front + 1) % capacity;
count--;
}
}
//Peek
int queue::peek()
{
if (is_empty())
{
std::cout << "Queue underflow!" << std::endl;
exit(EXIT_FAILURE);
}
return array[front];
}
//Size
int queue::size()
{
return count;
}
//is_full()
bool queue::is_full()
{
if (size() == capacity)
return true;
return false;
}
//is_empty()
bool queue::is_empty()
{
if (size() == 0)
return true;
return false;
}
int main()
{
int x;
queue q;
q.enqueue(5);
x = q.peek();
std::cout << x << std::endl;
q.dequeue();
return 0;
}