-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathstack_and_queue_4.cpp
84 lines (72 loc) · 1.67 KB
/
stack_and_queue_4.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
#include <iostream>
using namespace std;
//implement queue using array
class queueUsingArray
{
int* data;
int nextIndex;
int firstIndex;
int Size;
int capacity;
public:
queueUsingArray(int s)
{
data = new int[s];
nextIndex=0;
firstIndex=-1;
Size=0;
capacity=s;
}
int getSize(){
return Size;
}
bool isEmpty(){
return Size==0?true:false;
}
void enqueue(int element){
if(Size==capacity)
cout<<"Queue Full"<<endl;
data[nextIndex] = element;
nextIndex = (nextIndex+1)%capacity; //isse queue circular behave krta hai
if(firstIndex==-1)
firstIndex=0;
Size++;
}
int front(){
if(isEmpty()){
cout<<"Queue is empty"<<endl;
return 0;
}
return data[firstIndex];
}
int deque()
{
if(isEmpty()){
cout<<"Queue is empty"<<endl;
return 0;
}
int ans= data[firstIndex];
firstIndex = (firstIndex+1)%capacity;
Size--;
if(Size==0){
firstIndex=-1;
nextIndex=0;
}
return ans;
}
};
int main()
{
cout<<"Program started.."<<endl;
queueUsingArray q(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
cout<<"First element is: "<<q.front()<<endl;
cout<<"after dequeue element removed: "<<q.deque()<<endl;
cout<<"size of Queue is: "<<q.getSize()<<endl;
cout<<"Now first element is: "<<q.front()<<endl;
return 0;
}