链栈的实现

 #include<stdio.h>
  2 #include<stdlib.h>
  3 #define TRUE 1
  4 #define FALSE 0
  5 //typedef struct node *pnode;
  6 typedef struct node
  7 {
  8         int data;
  9         struct node *next;
 10 }Stack,*LinkStack;
 11 
 12 int InitStack(Stack * top)
 13 {
 14         top->next = NULL;
 15         return TRUE;
 16 }
 17 
 18 int IsEmpty(Stack * top)
 19 {
 20         if((top->next) == NULL)
 21                 return TRUE;
 22         else
 23                 return FALSE;
 24 }
 25 
 26 int Push(Stack * top, int x)
 27 {
 28         Stack * p;
 29         p = (Stack *)malloc(sizeof(Stack));
 30         if(p == NULL)
 31                 return FALSE;
 32         p->data = x;
 33         p->next = top->next;
 34         top->next = p;
 35         return TRUE;
 36 }
 37 
 38 int Pop(Stack * top, int *x)
 39 {
 40         if(IsEmpty(top))
 41                 return FALSE;
 42         Stack * p;
 43         p = top->next;
 44         *x = p->data;
 45         top->next = p->next;
 46         free(p);
 47 }
 48 
 49 void main()
 50 {
 51         int i;
 52         Stack * sp;
 53         sp = (Stack *)malloc(sizeof(Stack));
 54         InitStack(sp);
 55         for(i = 0; i <100; i++)
 56                 Push(sp, i);
 57         int item;
 58         while(!IsEmpty(sp))
 59         {
 60                 Pop(sp, &item);
 61                 printf("%d  ",item);
 62         }
 63         printf("\n");
 64 }              
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值