Skip to content

Commit 95dc37b

Browse files
authored
Stack, Queue using arrays div-bargali#223
Stack, Queue using arrays
2 parents 2aa2287 + bf626e0 commit 95dc37b

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
template <typename T>
4+
5+
class QueueUsingArray {
6+
T* data;
7+
int firstIndex;
8+
int nextIndex;
9+
int size;
10+
int capacity;
11+
public:
12+
QueueUsingArray() {
13+
data = new T[5];
14+
firstIndex = -1;
15+
nextIndex = 0;
16+
size = 0;
17+
capacity = 5;
18+
}
19+
int getsize() {
20+
return size;
21+
}
22+
bool isEmpty() {
23+
return size == 0;
24+
}
25+
void enqueue(T element) {
26+
if (size == capacity)
27+
{
28+
int j = 0;
29+
T* newData = new T[capacity * 2];
30+
for (int i = firstIndex; i < capacity; i++) {
31+
newData[j] = data[i];
32+
j++;
33+
}
34+
for (int i = 0; i < firstIndex; i++) {
35+
newData[j] = data[i];
36+
j++;
37+
}
38+
delete [] data;
39+
data = newData;
40+
firstIndex = 0;
41+
nextIndex = capacity;
42+
capacity *= 2;
43+
}
44+
data[nextIndex] = element;
45+
nextIndex = (nextIndex + 1) % capacity;
46+
if (firstIndex == -1) {
47+
firstIndex = 0;
48+
}
49+
size++;
50+
}
51+
T front() {
52+
if (isEmpty()) {
53+
cout << "Queue is empty!" << endl;
54+
return 0;
55+
}
56+
return data[firstIndex];
57+
}
58+
59+
T dequeue() {
60+
if (isEmpty()) {
61+
cout << "Queue is empty!" << endl;
62+
return 0;
63+
}
64+
T ans = data[firstIndex];
65+
firstIndex = (firstIndex + 1) % capacity;
66+
size--;
67+
if (size == 0) {
68+
firstIndex = -1;
69+
nextIndex = 0;
70+
}
71+
return ans;
72+
}
73+
74+
};
75+
76+
77+
78+
int main() {
79+
#ifndef ONLINE_JUDGE
80+
freopen("input.txt", "r", stdin);
81+
freopen("output.txt", "w", stdout);
82+
#endif
83+
QueueUsingArray<int> q;
84+
q.enqueue(10);
85+
q.enqueue(20);
86+
q.enqueue(30);
87+
q.enqueue(40);
88+
q.enqueue(50);
89+
q.enqueue(60);
90+
q.enqueue(70);
91+
q.enqueue(80);
92+
q.enqueue(90);
93+
cout << q.front() << endl;
94+
cout << q.dequeue() << endl;
95+
cout << q.dequeue() << endl;
96+
cout << q.getsize() << endl;
97+
cout << q.dequeue() << endl;
98+
cout << q.dequeue() << endl;
99+
cout << q.dequeue() << endl;
100+
cout << q.dequeue() << endl;
101+
cout << q.dequeue() << endl;
102+
cout << q.dequeue() << endl;
103+
cout << q.front() << endl;
104+
cout << q.isEmpty() << endl;
105+
106+
107+
return 0;
108+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
template<typename T>
5+
class StackUsingArray {
6+
T *data;
7+
int nextIndex;
8+
int capacity;
9+
public:
10+
StackUsingArray() {
11+
data = new T[capacity];
12+
capacity = 4;
13+
nextIndex = 0;
14+
}
15+
int size() {
16+
return nextIndex;
17+
}
18+
bool isEmpty() {
19+
return nextIndex == 0;
20+
}
21+
void push(T n) {
22+
if (nextIndex == capacity) {
23+
T *newData = new T[2 * capacity];
24+
for (int i = 0; i < capacity; i++) {
25+
newData[i] = data[i];
26+
}
27+
capacity = capacity * 2;
28+
delete[] data;
29+
data = newData;
30+
31+
}
32+
data[nextIndex] = n;
33+
nextIndex++;
34+
}
35+
T pop() {
36+
if (isEmpty() == true) {
37+
cout << "Stack is empty" << endl;
38+
return 0;
39+
}
40+
nextIndex--;
41+
return data[nextIndex];
42+
}
43+
T top() {
44+
return data[nextIndex - 1];
45+
}
46+
47+
};
48+
int main() {
49+
#ifndef ONLINE_JUDGE
50+
freopen("input.txt", "r", stdin);
51+
freopen("output.txt", "w", stdout);
52+
#endif
53+
54+
StackUsingArray<int> s;
55+
s.push(101);
56+
s.push(102);
57+
s.push(103);
58+
s.push(104);
59+
s.push(105);
60+
61+
62+
cout << s.size() << endl;
63+
cout << s.top() << endl;
64+
cout << s.pop() << endl;
65+
cout << s.pop() << endl;
66+
cout << s.pop() << endl;
67+
cout << s.pop() << endl;
68+
cout << s.pop() << endl;
69+
return 0;
70+
}

0 commit comments

Comments
 (0)