利用链表实现栈

//利用链表构建栈。//输入1 2 3 4 5 0时输出 5 4 3 2 1#include<stdio.h>#include<stdlib.h>#define true 1#define false 0typedef struct node{ //建立结构体int num; struct node *next;}Node;int push(Node **head, int num) //将一个数字压入以head为头的堆栈{Node *nextNode;if(NULL==(nextNode=(struct node *)malloc(sizeof(struct node )))){return false;}nextNode->num = num;if(NULL == *head){nextNode->next = NULL;*head = nextNode;return true;} nextNode->next = *head;//尾插法*head = nextNode;return true;}int pop(Node **head,int *num) //出栈{Node *temp;if(NULL == *head)return false;*num = (*head)->num;temp = (*head)->next;free(*head);*head = temp;return true;}int main(int argc, char* argv[]) //主函数 {int n=0,num;Node *head;head=NULL;printf("Please input numbers end with 0 :\n");while(1){ //如果不是0,压栈scanf("%d",&num);if(0 == num)break;push(&head,num);n++;}while(pop(&head,&num)){ //出栈输出printf("%d ", num);}printf("\nTotal : %d\n",n);}