链栈 - 栈的链式存储

来源:互联网 发布:战舰世界排位赛数据 编辑:程序博客网 时间:2024/05/23 16:53

链栈:解决顺序栈的上溢问题,链栈不需要设置头结点,因为头指针就是栈顶指针,插入删除均在均在表的一端进行。top为链表头指针。


1.结点结构与单链表相同 (top为头指针)

typedef struct node{    int data;    struct node *next;}Stack_node;

2.置栈空

void init(Stack_node ** top){    *top = NULL;}

3.入栈 改变头指针传双重指针

void push(Stack_node **top,int a){    Stack_node *p = (Stack_node*)malloc(sizeof(Stack_node));    p->data = a;    p->next = *top;    *top = p;}

4.出栈与进栈同理
出栈判栈空即:头指针Stack_node *top 是否为NULL

0 0
原创粉丝点击