链栈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//链栈 
#include<stdio.h>
typedef int datatype;
typedef struct node
{
    datatype data;
    struct node *next;
 } linkstack;
 linkstack *top;//栈顶指针top指向链栈的栈顶结点
  //链栈的进栈运算
  linkstack *PUSH_LSTACK(top,x)
  linkstack *top;datatype x;
  {
      linkstack *p;
      p=(struct node*)malloc(sizeof(linkstack));
      p->data=x;
      p->next=top;
      top=p;
      return (top);
   } 
   
   //链栈的出栈运算
   linkstack *POP_LSTACK(top.datap)
   linkstack *top;datatype *datap;
   {
       linkstack *p;
       if(top<0)
       {
           printf("栈空!");
        return 0;
        }
        else 
        {
            *datap=top->data;
            p=top;
            top=top->next;
            free(p);
            return(top);
        }
    }