数据结构与算法分析——栈
2015-08-20 16:55
447 查看
栈
代码实现如下
Stack.h
Stack.c
代码实现如下
Stack.h
#ifndef _Stack_H struct Node; typedef struct Node *PtrToNode; typedef PtrToNode Stack; typedef int ElementType; int IsEmpty(Stack S); Stack CreateStack(void); void Push(ElementType X, Stack S); void Pop(Stack S); ElementType Top(Stack S); #endif
Stack.c
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include "Stack.h" struct Node { ElementType Element; PtrToNode Next; }; void FatalError(const char *Str) { printf("%s\n",(*Str)); exit(-1); } int IsEmpty(Stack S) { return S->Next == NULL; } /* Create a empty Stack */ Stack CreateStack(void) { Stack S; S = malloc(sizeof(struct Node)); if(S == NULL) FatalError("Out of space!!!\n"); S->Next = NULL; return S; } /* Push X into Stack S*/ void Push(ElementType X, Stack S) { PtrToNode TmpCell; TmpCell = malloc(sizeof(struct Node)); if(TmpCell == NULL) FatalError("Out of space!!!\n"); else { TmpCell->Element = X; TmpCell->Next = S->Next; S->Next = TmpCell; } } ElementType Top(Stack S) { if(!IsEmpty(S)) return S->Next->Element; printf("IsEmpty Stack"); return 0; } void Pop(Stack S) { PtrToNode FirstCell; if(IsEmpty(S)) printf("Empty Stack\n"); else { FirstCell = S->Next; S->Next = FirstCell->Next; free(FirstCell); } }
相关文章推荐
- 数据结构中的英文汇总
- 数据结构是一种用于实现集合的基本编程结构
- 数据结构学习笔记之线性结构--离散存储[链表]
- 数据结构学习笔记之线性结构--连续存储[数组]
- 数据结构学习笔记---基础篇
- 数据结构-算法
- 最小生成树之prime算法
- 数据结构六:二叉树的先序建树与中序的非递归遍历算法
- 数据结构学习——Huffman树及其应用
- PAT《数据结构学习与实验指导》实验项目集 2-06. 数列求和(20) C语言
- PAT《数据结构学习与实验指导》实验项目集 2-05. 求集合数据的均方差(15) C语言
- 数据结构与算法分析——最大公约数
- 数据结构与算法分析——二分查找
- 数据结构-----哈希表
- 数据结构学习笔记6-寻找和为定值的两个数(二分查找)
- 汉诺塔实现
- 数据结构与算法分析——最大的子序列和问题
- fibonacci数列的递归与非递归实现
- KMP算法
- BF算法