lstack.h

来自「数据结构实验与学习指导」· C头文件 代码 · 共 47 行

H
47
字号
/*lstack.h*/
#include"sj.h"                    /*调用自定义库函数*/
typedef struct node{
   ElemType data;
   struct node *next;
}StackNode;
typedef struct
 {
    struct node *top;
 }LinkStack;
void Init_LS(LinkStack *ls)        /*置空链栈 */
 {  ls->top=NULL;
 }
Status  Empty_LS(LinkStack *ls)              /*判栈空 */
 {  return ls->top==NULL;
 }
void Push_LS(LinkStack *ls, ElemType x)     /*入栈 */
  { StackNode  *p=(StackNode *)malloc(sizeof(StackNode));
    p->data=x;
    p->next=ls->top;                             /* 将元素x插入链栈顶 */
    ls->top=p ;
}
Status  Pop_LS(LinkStack *ls, ElemType *y)    /* 出栈*/
  { StackNode  *p;
    if (Empty_LS(ls))
    { printf ("下溢!") ;
      return  OVERFLOW;
    }
     else { *y=ls->top->data ;
       p=ls->top ;
       ls->top=p->next;
       free (p) ;
       return  OK ;
    }
}
Status  GetTop(LinkStack *ls, ElemType  *y)    /*取栈顶元素 */
 {  if( Empty_LS(ls) )
     { printf ("下溢!") ;
       return  OVERFLOW;
     }
   else {
     *y=ls->top->data;
     return OK ;
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?