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;
}
本来是直接去做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;
}
相关文章推荐
- SDUT 2556 传说中的数据结构
- 传说中的数据结构 SDUT 2556
- SDUT-2556 传说中的数据结构
- SDUT 2556 传说中的数据结构
- 【栈与队列】SDUT练习1—传说中的数据结构
- 山理工2556 传说中的数据结构
- SDUT 1309 不老的传说问题 (区间DP)
- 传说中的数据结构
- 传说中的数据结构
- 传说中的数据结构
- sdut数据结构上机实验之二分查找
- [SDUT](1130)数据结构上机测试1:顺序表的应用
- 栈学习——传说中的数据结构
- 【sdut】1309 不老的传说问题(简单环形区间DP)
- SDUT1130数据结构上机测试1:顺序表的应用
- sdut 1309 不老的传说问题(区间DP,难,值得好好看)
- [SDUT](1130)数据结构上机测试1:顺序表的应用 ---顺序存储(线性表)
- 传说中的数据结构
- sdut 1309 —— 不老的传说问题
- 传说中的数据结构 (sdut oj)