-
Notifications
You must be signed in to change notification settings - Fork 1
/
Data_queue.h
157 lines (144 loc) · 3.25 KB
/
Data_queue.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Implement Circular Queue for Data buffer
*/
#include <iostream>
#define MAX 100
using namespace std;
/*
* Class Circular Queue
*/
class Data_Queue
{
private:
char *cqueue_arr;
int front, rear;
public:
Data_Queue()
{
cqueue_arr = new char[MAX];
rear = front = -1;
}
/*
* Insert into Circular Queue
*/
void insert(char item)
{
if ((front == 0 && rear == MAX - 1) || (front == rear + 1))
{
cout << "Queue Overflow \n";
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = item;
printf("inserted ");
}
/*
* Delete from Circular Queue
*/
void del()
{
if (front == -1)
{
cout << "Queue Underflow\n";
return;
}
cout << "Element deleted from queue is : " << cqueue_arr[front] << endl;
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}
char *getArray()
{
return cqueue_arr;
}
char getFrontIndex()
{
return front;
}
char getRearIndex()
{
return rear;
}
//return true and store the data to the buf if successfully get completed data
bool getData(char *buffer, int &numOfData)
{
if ((cqueue_arr[front] == 'A') && (cqueue_arr[rear] == '\n'))
{
cout << "OK";
int queue_index = front;
int buffer_index = 0;
do
{
if (queue_index == MAX)
queue_index = 0;
buffer[buffer_index] = cqueue_arr[queue_index];
buffer_index++;
queue_index++;
} while (queue_index != rear);
buffer[buffer_index] = cqueue_arr[queue_index];
//delete the data in queue after storing to the buf
for(int i = 0; i < buffer_index; i ++){
del();
}
numOfData = queue_index;
return true;
}
return false;
}
/*
* Display Circular Queue
*/
void display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
cout << "Queue is empty\n";
return;
}
cout << "Queue elements :\n";
if (front_pos <= rear_pos)
{
while (front_pos <= rear_pos)
{
cout << cqueue_arr[front_pos] << " ";
front_pos++;
}
}
else
{
while (front_pos <= MAX - 1)
{
cout << cqueue_arr[front_pos] << " ";
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
cout << cqueue_arr[front_pos] << " ";
front_pos++;
}
}
cout << endl;
}
};