链栈

# define null 0
  typedef struct stacknode{int data;
  struct stacknode *next;}stacklink;
  typedef struct {stacklink *top;int stacksize;}stackk;

  initlink(stackk *s)
  {s->top=(stacklink *)malloc(sizeof(stacklink));
   s->top->data=0;
   s->top->next=null;
  }

  int poplink(stackk *s)
  {stackk *p;int v;
   if(s->top->next==null) printf("the stackis empty\n");
   else {v=s->top->next->data;
   p=s->top->next;
   s->top=s->top->next;
   free(p);
   return v;
  }
    }

  int pushlink(stackk *s,int x)
      {stackk *p;
      p=(stacklink *)malloc(sizeof(stacklink));
      p->data=x;
      p->next=s->top->next;
      s->top->next=p;
  }

  int gettop(stackk *s)
    {int e;
     if(s==null) printf("the stack is empty!\n");
     e=s->top->next->data;
     return e;
   }
   display(stackk *s)
   {stackk *p;
    p=s->top->next;
    printf("display the stacklink:\n");
    if (s->top=null) printf("the stacklink is empty!\n");
    else {while(p)
  {printf("->%d",p->data);
   p=p->next;}
   }
   }
   main(stacklink *p)
   {int n,k,i,select,h,x1,x2;
    printf("create a empty stacklink!\n");
    initlink(p);
    printf("input a stacklink length:\n");
    scanf("%d",&n);
    for (i=1;i<=n;i++)
   {printf("input a stacklink value:\n");
    scanf("%d",&k);
    pushlink(p,k);
   }
    printf("select 1:display()\n");
    printf("select 2:pushlink()\n");
    printf("select 3:poplink()\n");
    printf("select 4:gettop()\n");
    printf("input a your select(1-4):\n");
    scanf("%d",&select);
    switch(select)
   {case 1:{display(p);break;}
    case 2:{printf("input a push a value :\n");
     scanf("%d",&h);
     pushlink(p,h);
     display(p);
     break;}
     case 3:{x1=poplink(p);printf("x1->%d\n",x1);
      display(p);
      break;}
     case 4: {x2=gettop(p);printf("x2->%d",x2);
       break;}
     }
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值