-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathqueueUsingArrays.cpp
More file actions
107 lines (88 loc) · 1.62 KB
/
queueUsingArrays.cpp
File metadata and controls
107 lines (88 loc) · 1.62 KB
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
#include <iostream>
using namespace std;
template <typename T>
class queueusingarray
{
private:
T * data;
int totsize;
int frontindex;
int nextindex;
int capacity;
public:
queueusingarray(int s)
{
data=new T[s];
totsize=0;
frontindex=-1;
capacity=s;
nextindex=0;
}
int getsize()
{
return totsize;
}
bool isEmpty()
{
return (totsize==0);
}
T front()
{
if (totsize==0)
{
cout<<"Queue is Empty";
return 0;
}
return data[frontindex];
}
void enqueue(T d)
{
if (totsize==capacity)
{
cout<<"Queue is full"<<endl;
return;
}
data[nextindex]=d;
nextindex=((nextindex+1)%capacity);
totsize++;
if (frontindex==-1)
{
frontindex=0;
}
}
T dequeue()
{
if (totsize==0)
{
cout<<"Queue is empty!"<<endl;
return 0;
}
T d=data[frontindex];
frontindex=(frontindex+1)%capacity;
totsize--;
return d;
if (totsize==0)
{
frontindex=-1;
nextindex=0;
}
}
};
int main()
{
queueusingarray <int> q(7);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.enqueue(60);
q.enqueue(70);
cout<<q.front()<<endl;
cout<<q.dequeue()<<endl;
cout<<q.dequeue()<<endl;
cout<<q.dequeue()<<endl;
cout<<q.getsize()<<endl;
cout<<q.isEmpty()<<endl;
return 0;
}