链栈的实现

来源:互联网 发布:数据透视表常用快捷键 编辑:程序博客网 时间:2024/05/29 02:34

【版权声明:转载请保留出处:blog.csdn.net/algorithm_only。邮箱:liuy0711@foxmail.com

链栈的操作就是链式线性表的尾部进行添加元素和删除元素,由于只在尾部进行操作,不需要专门的头节点,栈顶元素即是该链栈的头节点。(注意:栈顶元素对应头插法建表的刚进入链表的元素,栈底元素对应头插法建表最早进入链表的元素)

  • 链栈的存储结构
#defineOK1#define ERROR-1typedef struct linkstack{elemtypedata;structlinkstack*next;}*linkstack;
  • 链栈元素进栈
int push_linkstack(linkstack *top, elemtype e){linkstacktmp;if ((tmp = (linkstack) malloc (sizeof(struct linkstack))) == NULL) return ERROR;tmp->data = e;tmp->next =*top;*top = tmp;return OK;}
链栈的进栈类似于头插法添加元素。
  • 链栈元素出栈
int pop_linkstack(linkstack *top, elemtype *e){linkstacktmp;if (NULL == *top)return ERROR;tmp = *top;*e = tmp->data;*top = (*top)->next;free(tmp);return OK;}
链栈的元素出栈即是释放第一个节点的空间,类似于释放链表节点的操作。
  • 源码实现
源码免费下载http://download.csdn.net/detail/algorithm_only/3787667

原创粉丝点击