-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInOutS.cpp
32 lines (29 loc) · 1.15 KB
/
InOutS.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
#include "InOutS.h"
#include "../../../Status/Status.h"
#include <iostream>
// 用数组s模拟栈, 根据读入的数据完成入栈和出栈操作
void InOutS(int s[]) {
int top = 0; // top为栈顶指针, top=0时为栈空
int maxsize = 5; // 设置栈容量是5
int n = 5; // 对5个整数序列进行处理
for (int i = 1; i <= n; i++) // 对n个整数序列进行处理
{
int x;
std::cin >> x; // 从键盘读入整数序列
if (x != -1) // 读入的整数不等于-1时入栈
{
if (top == maxsize - 1) {
std::cout << "栈满" << std::endl;
exit(ERROR);
} else
s[++top] = x; // x入栈
} else // 读入的整数等于-1时退栈
{
if (top == 0) {
std::cout << "栈空" << std::endl;
exit(ERROR);
} else
std::cout << "出栈元素是" << s[top--] << std::endl;
}
}
}