-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueArray.cpp
77 lines (65 loc) · 1.2 KB
/
QueueArray.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
#include "QueueArray.h"
//C'tor
QueueArray::QueueArray(int lengthArray)
{
sizeQ = lengthArray;
int* QArray = new int[sizeQ];//60
array = QArray;
head = &QArray[0];
tail = &QArray[0];
sizeQarray = 60;
counter = 0;
}
//Enqueue - add member to the queue
void QueueArray::Enqueue(int x)
{
if (counter < sizeQ - 1)
{
*tail = x;
tail++;
counter++;
}
else if ((counter == sizeQ - 1) && (head == array ) || (tail + 1 == head))
{
cout << "overflow!!" << endl;
}
else if ((tail == array + sizeQ - 1 ) && head != array)
{
*tail = x;
tail = array;
counter =0;
}
}
//Dequeue - remove member from the queue
int QueueArray::Dequeue()
{
int temp = -1 ;
bool res = IsEmpty();
if (res)
{
return temp;
}
else
{
temp = *head;
head++;
}
return temp;
}
// IsEmpty - checking if the queue is empty
bool QueueArray::IsEmpty()
{
bool res = false;
if ( tail == head )//לשנות ל head+1
{
res = true;
return res;cout << "The Q is empty" << endl;
}
else
return res;
}
//Top - return the next member which is supposed to come out
int QueueArray::Top()
{
return *head;
}