点击打开链接
#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");
}
}