forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midele.c
51 lines (43 loc) · 996 Bytes
/
midele.c
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
#include<bits/stdc++.h>
using namespace std;
// Deletes middle of stack of size
// n. Curr is current item number
void deleteMid(stack<char> &st, int n,
int curr=0)
{
// If stack is empty or all items
// are traversed
if (st.empty() || curr == n)
return;
// Remove current item
int x = st.top();
st.pop();
// Remove other items
deleteMid(st, n, curr+1);
// Put all items back except middle
if (curr != n/2)
st.push(x);
}
//Driver function to test above functions
int main()
{
stack<char> st;
//push elements into the stack
st.push('1');
st.push('2');
st.push('3');
st.push('4');
st.push('5');
st.push('6');
st.push('7');
deleteMid(st, st.size());
// Printing stack after deletion
// of middle.
while (!st.empty())
{
char p=st.top();
st.pop();
cout << p << " ";
}
return 0;
}