-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise10_1_2.cpp
75 lines (72 loc) · 1.23 KB
/
Exercise10_1_2.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
/*************************************************************************
> File Name: Exercise10_1_2.cpp
> Author: yinahe
> Mail: yina.he@nokia.com
> Created Time: Sat 24 Oct 2015 03:48:01 PM CST
************************************************************************/
#include<iostream>
using namespace std;
#include "Exercise10_1_2.h"
bool arrayWithTwoStack::isEmpty(int index)
{
if(index == 0 && top1 < 1)
return true;
if(index == 1 && top2 > len)
return true;
return false;
}
void arrayWithTwoStack::push(int index, int data)
{
//已满
if(top1 > top2)// cout<<"error:overflow"<<endl;
return;
//对栈1操作
if(index == 0)
{
//top
top1++;
s[top1] = data;
}
//对栈2操作
else
{
top2--;
s[top2] = data;
}
}
//出栈,index为0表示对第1个栈进行操作,index为1表示对第2个栈进行操作
int arrayWithTwoStack::pop(int index)
{
int ret;
//对栈1操作
if(index == 0)
{
//栈1为空
if(top1 < 0)
{
//cout<<"error:underflow"<<endl;
return -1;
}
else
{
ret = s[top1];
top1--;
}
}
//对栈2操作
else
{
//栈2为空
if(top2 > len)
{
//cout<<"error:underflow"<<endl;
return -1;
}
else
{
ret = s[top2];
top2++;
}
}
return ret;
}