您的位置:首页 > 理论基础 > 数据结构算法

SDUT 2556 - 传说中的数据结构

2014-04-03 16:48 197 查看
传送门SDUT 2556 - 传说中的数据结构

本来是直接去做UVA的模拟栈的题的, 没思路不用说, 别人的解题报告也根本看不懂.

后来想想还是我太心急了, 栈是什么都不是很清楚, 没有学会走路就想飞.

所以就先做点题目, 熟悉一下.

争取清明之前把栈, 队列, 二叉树搞清楚. 至少能看懂别人的代码.

#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
#define MAXN 2000
int main()
{
//freopen("input.txt", "r", stdin);
int n;
while (scanf("%d", &n) == 1)
{
getchar();
int i, j;
char temp[1000];
int target[MAXN], top = -1;
for (i = 0; i < n; i++)
{
scanf("%s", temp);
if (strcmp(temp, "push") == 0)
{
scanf("%d", &target[++top]);
}
else if (strcmp(temp, "top") == 0)
{
if (top == -1)
printf("empty\n");
else
printf("%d\n", target[top]);
}
else if (strcmp(temp, "pop") == 0)
{
if (top == -1)
printf("error\n");
else
top--;
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM SDUT