-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwostacks.cpp
More file actions
48 lines (45 loc) · 920 Bytes
/
twostacks.cpp
File metadata and controls
48 lines (45 loc) · 920 Bytes
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
#include <iostream>
enum status
{
ERROR,
OK
};
const int MAXSIZE = 100;
template<typename T>
struct Sqdolstack
{
T data[MAXSIZE];
int top1;
int top2;
};
template<typename T>
status push(Sqdolstack<T>& S, T& e, int stacknum)
{
if (S.top1 + 1 == S.top2) return ERROR;
switch (stacknum)
{
case 1:
S.data[++S.top1] = e;
return OK;
case 2:
S.data[--S.top2] = e;
return OK;
default:
return ERROR;
}
}
template<typename T>
status pop(Sqdolstack<T>& S, T& e, int stacknum)
{
switch (stacknum)
{
case 1:
if (S.top1 == -1) return ERROR;
e = S.data[S.top1--];
return OK;
case 2:
if (S.top2 == MAXSIZE) return ERROR;
e = S.data[S.top2++];
return OK;
}
}