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

传说中的数据结构

2014-01-16 19:45 218 查看
点击打开链接
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    int data ;
    struct node * next;
};
struct node *head = NULL,*tail,*tmp;
int main ()
{
    int m;
    char pp[10];

    while (scanf ("%d",&m) != EOF)
    {
        head = (struct node *)malloc (sizeof (struct node));
        head->next = NULL;
        while (m--)
        {
            tmp = (struct node *)malloc (sizeof (struct node));
            scanf ("%s",pp);

            if (strcmp(pp,"push")==0)
            {
                scanf ("%d",&tmp -> data);
                tmp ->next = NULL;
                tmp ->next = head;
                head=tmp;
            }
            else if (strcmp(pp,"top")==0)
            {
                if (head->next == NULL)
                    printf("empty\n");
                else
                    printf ("%d\n",head -> data);
            }
            else if (strcmp(pp,"pop")==0)
            {
                if (head->next == NULL)
                    printf("error\n");
                else
                {
                    tmp = head;
                    head = head->next;
                    free (tmp);
                }
            }
        }
        printf ("\n");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: