-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10_stack.cpp
More file actions
95 lines (80 loc) · 1.33 KB
/
10_stack.cpp
File metadata and controls
95 lines (80 loc) · 1.33 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
/*
WAP to insert N Elements into the stack. Find the Middle element , then delete one element from the stack and again find the new middle element
Example
let 6 elements are there in stack
5
8
6
4
7
middle element is 6
deleted element is 7
new middle element is 8
Input Format
First input : N ,the number of elements in the stack
Second input : accect N elements from the user into the stack
Constraints
N must be nonzero and >=3
second input must be positive intergers
Output Format
Dispaly Middle Element
Display The New Midlle Element After Deleting One element From The Stack.
Sample Input 0
5
2 6 8 7 4
Sample Output 0
8
6
Sample Input 1
3
5 8 6
Sample Output 1
8
5
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int *stack =NULL;
int n;
int top =-1;
void push(int x)
{
stack[++top]=x;
}
void pop()
{
if(top>-1)
{
top--;
}
}
void mid()
{
int mid = top/2;
if(mid!=-1)
{
cout<<stack[mid]<<endl;
}
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
cin>>n;
if(n>=3)
{
stack = new int[n];
for(int i=0;i<n;i++)
{
int x;
cin>>x;
push(x);
}
mid();
pop();
mid();
}
return 0;
}