数据结构基础6_链栈的实现

typedef int ElemType;
typedef int Status;
#include<stdio.h>
#define true 1
#define false 0
typedef struct Snode
{
        ElemType data;
        struct Snode *next;
        }Snode,*pstack;
        typedef struct {
                pstack top;
                
                }linkstack;
 Status Initstack(linkstack &S)
 {
        
         
        S.top=NULL;
       
        return true;
        }
 Status Gettop(linkstack S,ElemType &e)
 {
      if(S.top==NULL)
      {
                       return false;}
                       Snode *p=S.top;
                       e=p->data;
                       return true;
                       }
   Status Destroystack(linkstack &S)
   {
         Snode *p=S.top;
         p->next=NULL;
         free(p);
               return true;
               }
    Status Clearstack(linkstack &S)
    {
           Snode *p=S.top;
    p->next=NULL;
    return true;
}
    Status stackempty(linkstack S)
    {
           if(S.top==NULL)
           {
                return true;
                }
                else
                return false;
                }
     Status stacklength(linkstack S)
     {
            int j=0;
            Snode *p=S.top;
            while(p->next!=NULL)
            {
                 j++;
                 p=p->next;
                 }
                 return j;
                 }
      Status push(linkstack &S,ElemType e)
      {
          Snode *q=(pstack)malloc(sizeof(Snode));
          if(!q)
          {
                return false;
                }
          q->data=e;
          q->next=S.top;
          S.top=q;
          return true;
          }
     Status pop(linkstack &S,ElemType &e)
     {
         Snode *p=S.top;
          if(S.top==NULL)
          {
              return false;
              }
         
         e=p->data;
         S.top=p->next;
        free(p);
         return true;
         }
     Status stacktraverse(linkstack S)
     {
            Snode *p=S.top;
            while(p!=NULL)
            {
                printf("%d",p->data);
                p=p->next;
                }
                return true;
                }                             
                                                               
           
                                          
                         
          
          
                              
                         
        
                        
#include<stdio.h>
#include<stdlib.h>
#include<linkstack1.h>
int main()
{
    linkstack s;
    Initstack(s);
    push(s,1);
    push(s,2);
    push(s,3);
    push(s,4);
    push(s,5);
    int n;
    pop(s,n); 
    stacktraverse(s);
    system("pause");
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值