《数据结构》 栈代码操作集合
2016-03-30 16:33
447 查看
栈的基本操作代码,来自《数据结构-用C语言描述》(第二版)高教社
栈的数据结构相当于受限制(只能从栈顶取元素,先进后出LIFO)的顺序表或单链表,可以参考之前的博客。
栈的数据结构相当于受限制(只能从栈顶取元素,先进后出LIFO)的顺序表或单链表,可以参考之前的博客。
/*以下为顺序栈*/ #define Stack_Size 50 /*设栈中元素为50*/ typedef struct { StackElemType elem[Stack_Size]; int top; //用来存放栈顶元素的下标 } SeqStack; /*初始化*/ void InitStack(SeqStck) { S->top = -1; } /*进栈:将x置入新栈顶*/ int Push(SeqStack *S, StackElemType x) { if(S->top == Stack_Size -1) { return(FALSE); } S->top++; S->elem[S->top] = x; return(TRUE); } /*出栈*/ int Pop(SeqStack *S, StackElemType *x) { if(S->top = -1) { return(FALSE); } else { *x = S->elem[top]; top--; //修改栈顶指针 *x = S->elem[top--] return(TRUE); } } /*读栈顶*/ int GetTop(SeqStack *S, StackElemType *x) { if(top = -1) { return(FALSE); } else { *x = S->elem[S-top]; return(TRUE); } } /*以下为链栈*/ typedef struct node { StackElemType data; struct node *next; } LinkStackNode, *LinkStack; /*初始化;即单链表的初始化*/ InitLink(LinkStack *top) { *top =(LinkStack)malloc(sizeof(Node)); (*top)->next = NULL; } /*进栈*/ int Push(LinkStack top, StackElemType x) { LinkStackNode *temp; temp = (LinkStackNode *)malloc(sizeof(LinkStackNode)); if(temp == NULL) { return(FALSE); } temp->data = x; temp->next = top->next; top->next = temp; return (TRUE); } int Pop(LinkStack top, StackElemType *x) { LinkStackNode *temp; temp = top->next; if(top == NULL){ return (FALSE); } top->next = temp->next; *x = temp->data; free(temp); return(TRUE); }
相关文章推荐
- 数据结构(java语言描述)串与数组——文件加解密
- 常见的数据结构和算法
- 【数据结构】链表与实现分析
- 数据结构--Dijkstra算法最清楚的讲解
- 数据结构(树链剖分):BZOJ 4034: [HAOI2015]T2
- Java数据结构——链表-单链表
- 数据结构
- Intellij IDEA快捷键整理
- Java数据结构——解析算术表达式
- 统计文章中单词的字数并按照出现的频率排序(treeSet)
- 数据结构之队列的实现
- 数据结构(五)--最小生成树(普利姆算法、克鲁斯卡尔算法)
- 数据结构(四)--B树、B-树、B+树、B*树
- 数据结构线性表的两种存储形式顺序表和单链表的比较
- 数据结构之栈的实现
- 链表的所有操作(总结)
- 数据结构之双端(通用)链表的实现
- 数据结构与算法学习(九)(续二)
- 干货分享-数据结构
- 数据结构之单链表的实现