-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_stack.cpp
90 lines (84 loc) · 1.45 KB
/
reverse_stack.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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#include <stack>
class Stack
{
private:
int *arr;
int top;
int size;
public:
Stack(int size)
{
this->size = size;
arr = new int[size];
top = -1;
}
void push(int element)
{
if (top < size - 1)
{
top += 1;
arr[top] = element;
}
else
{
cout << "Stack overflow\n";
}
}
void pop(int element)
{
if (top >= 0)
{
top--;
}
else
{
cout << "Stack overflow\n";
}
}
int peek(int element)
{
if (top >= 0 && top < size)
{
return arr[top];
}
else
{
cout << "Stack is empty\n";
}
}
bool isEmpty()
{
if (top < 0)
{
return true;
}
return false;
}
};
void reverse(vector<int> &v, int i, int j)
{
if (i >= j)
{
return;
}
swap(v[i], v[j]);
reverse(v, i + 1, j - 1);
}
int main()
{
vector<int> v = {2, 3, 4, 5, 6, 7, 8};
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << "\n";
reverse(v, 0, v.size() - 1);
for (int j = 0; j < v.size(); j++)
{
cout << v[j] << " ";
}
return 0;
}