#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct LLNode
{
char ID[5];
struct LLNode *next;
};
struct LLNode * createNode(char val[5])
{
struct LLNode *temp;
temp=(struct LLNode *)malloc(sizeof(struct LLNode));
temp->next=NULL;
strcpy(temp->ID, val);
return temp;
};
void push(char* val, struct LLNode *head)
{
struct LLNode *temp;
temp = createNode(val);
temp->next = head->next;
head->next = temp;
}
char* pop(struct LLNode *head)
{
struct LLNode *temp;
char *val = (char *)malloc(5*sizeof(char));
strcpy(head->next->ID, val);
temp = head->next;
head->next = head->next->next;
free(temp);
return val;
}
char nothing(struct LLNode *head, struct LLNode *tail)
{
if(head->next == NULL)
return 1;
else
return 0;
}
int main()
{
char* value = (char *)malloc(5*sizeof(char));
struct LLNode *head = NULL;
struct LLNode *tail = NULL;
head = createNode("");
tail = createNode("");
head->next = tail;
printf("head->ID = %s\n", head->ID);
push ("C201", head);
printf("head->next->ID = %s\n",head->next->ID) ;
push ("C202", head);
printf("head->next->ID = %s\n",head->next->ID) ;
push ("C203", head);
printf("head->next->ID = %s\n",head->next->ID) ;
push ("C204", head);
printf("head->next->ID = %s\n",head->next->ID) ;
push ("C205", head);
printf("head->next->ID = %s\n",head->next->ID) ;
for (int i=0; i<3; i++)
{
value = (char *)malloc(5*sizeof(char));
strcpy(value, pop(head));
printf("The value pop = %s\n", value);
printf("head->next->ID= %s\n",head->next->ID);
free(value);
}
if (nothing(head,tail))
printf("The stack is empty");
printf("Remaining IDs:");
struct LLNode *curr = head;
while (curr)
{
printf("%s ",curr->ID);
curr = curr->next;
}
return 0;
}
输出: