《数据结构与算法分析》—栈的链表和数组实现(C语言)
来源:互联网 发布:接收chinanet的软件 编辑:程序博客网 时间:2024/04/30 07:46
/**栈的链表实现*表头作为栈顶*/#include <stdio.h>#include <stdlib.h>struct Node{ ElementType Element; PtrToNode Next;};int IsEmpty(Stack S){ return S->Next==NULL;}void Push(ElementType X,Stack S)//插入在表头{ Stack p; p=(struct Node*)malloc(sizeof(struct Node)); p->Element=X; p->Next=S->Next; S->Next=p;}ElementType Top(Stack S)//表头为栈顶{ if(!IsEmpty(S)) return S->Next->Element; return 0;}void Pop(Stack S){ PtrToNode p=S->Next; if(IsEmpty(S)) printf("Empty stack"); else { S->Next=p->Next; free(p); }}void MakeEmpty(Stack S){ if(S==NULL) printf("Must use CreateStack first"); else while(!IsEmpty(S)) Pop(S);}void StackPrint(Stack S){ struct Node *P=S->Next; while(P!=NULL) { printf("%3d",P->Element); P=P->Next; }}int main(){ Stack S; S=(struct Node*)malloc(sizeof(struct Node)); S->Next=NULL; int i=0; for(i=0;i<5;++i) { Push(i,S); } StackPrint(S); printf("\nTop=%3d\n",Top(S));//表头为栈顶 Pop(S); StackPrint(S); return 0;}/**栈的数组实现**/#include <stdio.h>#include <stdlib.h>struct StackArr{ int Capacity;//容量 int TopOfStack;//数组下标 ElementType *Array;};int IsEmpty(Stack S)//检查一个栈是否为空栈{ return S->TopOfStack==EmptyTOS;}int IsFull(Stack S){ return S->TopOfStack==S->Capacity-1;}void MakeEmpty(Stack S)//创建一个空栈{ S->TopOfStack=EmptyTOS;}void Push(ElementType X,Stack S){ if(IsFull(S)) printf("Full stack"); else S->Array[++S->TopOfStack]=X;}ElementType Top(Stack S){ if(!IsEmpty(S)) return S->Array[S->TopOfStack]; printf("Empty stack"); return 0;}void Pop(Stack S){ if(IsEmpty(S)) printf("Empty stack"); else S->TopOfStack--;}ElementType TopAndPop(Stack S){ if(!IsEmpty(S)) return S->Array[S->TopOfStack--]; printf("Empty stack"); return 0;}Stack CreateStack(int MaxElements){ Stack S; if(MaxElements<MinStackSize) printf("Stack size if too small"); S=malloc(sizeof(struct StackArr)); if(S==NULL) printf("Out of space!!!"); S->Array=malloc(sizeof(ElementType)*MaxElements); if(S->Array==NULL) printf("Out of space!!!"); S->Capacity=MaxElements; MakeEmpty(S); return S;}void StackArrPrint(Stack S){ int i; for(i=0;i<S->TopOfStack+1;++i) { printf("%3d\n",S->Array[i]); }}int main(){ Stack S=CreateStack(7); printf("%d\n",S->Capacity); printf("%d\n",S->TopOfStack); int i=0; for(i=0;i<S->Capacity;++i) { Push(i,S); } StackArrPrint(S); return 0;}
0 0
- 《数据结构与算法分析》—栈的链表和数组实现(C语言)
- 数据结构与算法分析—栈的数组实现(C语言)
- (数据结构与算法分析 三)------栈的实现(包括链栈和数组实现栈 Java语言描述)
- 数据结构与算法分析—循环队列的数组实现(C语言)
- 数据结构与算法分析-用C语言实现栈(数组方式)
- 数据结构与算法分析-用C语言实现栈(链表方式)
- 队列——链表与数组实现(数据结构与算法分析C语言版)
- 数据结构与算法分析-用C语言实现队列(数组方式)
- 数据结构与算法分析 C语言描述 单链表的实现
- 《数据结构与算法——C语言描述》答案 3.15 自调整表的实现(数组法)
- 栈及其应用 - C语言实现(摘自数据结构与算法分析 C语言描述)
- 栈及其应用 - C语言实现(摘自数据结构与算法分析 C语言描述)
- (c#)数据结构与算法分析 --数组、向量和表
- (数据结构与算法分析 四)------数组循环队列的实现( Java语言描述)
- 算法与数据结构 其二 数组和链表 的实现
- AVL(Adelson-Velskii和Landis)树 - C语言实现(摘自数据结构与算法分析 C语言描述)
- AVL(Adelson-Velskii和Landis)树 - C语言实现(摘自数据结构与算法分析 C语言描述)
- 数据结构与算法分析——带有头结点的单链表的实现(C语言)
- 【翻译】Leapmotion-python开发官方文档(8)
- 关于nginx配置
- 自动化测试工具QTP的学习
- 领导者追随者模式
- CentOS-6.5-x86_64-minimal 搭建mysql-5.6.29
- 《数据结构与算法分析》—栈的链表和数组实现(C语言)
- leetcode 4. Median of Two Sorted Arrays
- 自动化运维工具SaltStack详细部署
- Objective-C之Category
- Git学习(四)
- Storm使用到的相关技术总结
- 面试
- java 读取属性文件 properties
- LInux vim 显示行号