SDUT 2556 - 传说中的数据结构

来源:互联网 发布:php抓取微信文章 编辑:程序博客网 时间:2024/05/06 23:58

传送门SDUT 2556 - 传说中的数据结构

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

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

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

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


#include <cstdio>#include <stack>#include <cstring>using namespace std;#define MAXN 2000int 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;}


0 0
原创粉丝点击