-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackRev.cpp
More file actions
75 lines (75 loc) · 1.02 KB
/
stackRev.cpp
File metadata and controls
75 lines (75 loc) · 1.02 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
#include<iostream>
using namespace std;
#define max 5
struct stack
{
int data[max];
int top;
};
stack *p,s1;
void init()
{
p=&s1;
p->top=-1;
}
int empty()
{
if(p->top==-1)
return 1;
else
return 0;
}
int full()
{
if(p->top==max-1)
return 1;
else
return 0;
}
void push(int x)
{
if(full())return;
p->top++;
p->data[p->top]=x;
}
int pop()
{
int y;
if(empty())return 0;
y=p->data[p->top];
p->top--;
return y;
}
void dispR() // Display in reverse (top to bottom)
{
int temp = p->top;
while (temp != -1)
{
cout << p->data[temp] << " ";
temp--;
}
cout << endl;
}
void dispO() // Display in original order (bottom to top)
{
int temp = 0;
while (temp <= p->top)
{
cout << p->data[temp] << " ";
temp++;
}
cout << endl;
}
int main()
{
init();
push(10);
push(20);
push(30);
push(40);
push(50);
cout<<"original array: ";
dispO();
cout<<"reversed array: ";
dispR();
}