-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_02_queue_using_array.cpp
133 lines (121 loc) · 3.51 KB
/
_02_queue_using_array.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// اذكر الله
// صلِ على خير خلق الله محمد
// Include necessary header file for input and output operations
#include <iostream>
using namespace std;
// create struct to define queue function
struct Queue
{
int front = -1; // the first element in my queue
int rear = -1; // the last element in my queue
int *queue; // pointer to intiallize queue as array
int SIZE; // size of my queue
// n here refer to user input of queue size
Queue(int n)
{
SIZE = n;
// create queue with size which user entered before
queue = new int[SIZE];
}
// function to check if my queue is full
bool is_Full()
{
return rear == SIZE - 1;
}
// function to check if my queue is empty
bool is_Empty()
{
return rear == -1;
}
// function to add element
void inQueue(int element)
{
if (is_Full())
{
cout << "Queue Overflow\n";
return;
}
// first element in my queue
if (rear == -1)
front++;
// to add element in my queue
rear++;
queue[rear] = element;
}
// function to remove element from my queue
void deQueue()
{
if (is_Empty())
{
cout << "Queue Underflow\n";
return;
}
// check if my queue has only one element and I want to delete it
if (rear == front)
{
rear = -1, front = -1;
return;
}
// delete element from the front of my queue
front++;
}
void Display()
{
cout << "____________________________________________________________________\n";
// check if my queue is empty
if (is_Empty())
cout << "Your queue is empty\n";
else
{
// going from the first element to the last element in my queue
for (int i = front; i <= rear; i++)
cout << queue[i] << '\n';
}
cout << "____________________________________________________________________\n";
}
};
// main function
int main()
{
// take size of queu from user
int SIZE;
cout << "Please enter the size of your queue\n";
cin >> SIZE;
// create element from Queue struct with size which I have
Queue queue(SIZE);
// start using my queue
while (true)
{
// take user choise
cout << "_________________Please enter what you need to do_________________ \n";
cout << " 1 >> InQueue element to my Queue\n";
cout << " 2 >> DeQueue element from my Queue\n";
cout << " 3 >> Print my Queue\n";
cout << " 0 >> Exit\n";
cout << "____________________________________________________________________\n";
int CHOICE;
cin >> CHOICE;
// user decied to add element
if (CHOICE == 1)
{
int element;
cout << "Please enter the element\n";
cin >> element;
queue.inQueue(element);
}
// user decied to delete element
else if (CHOICE == 2)
queue.deQueue();
// user decied to pronit queue
else if (CHOICE == 3)
queue.Display();
// user decide to exit the program
else if (CHOICE == 0)
break;
// user entered wrong choise
else
cout << "You have entered a wrong number, please try again\n";
}
// end of the program
return 0;
}