C源码@数据结构与算法->栈Stack
2015-08-11 16:14
661 查看
/*
* fatal.h
* Header file for print error message.
*/
#ifndef _FATAL_H
#define _FATAL_H
#include
#include
#define Error(str) FatalError(str)
#define FatalError(str) fprintf(stderr, "%s\n", str), exit(-1)
#endif /* _FATAL_H */
* fatal.h
* Header file for print error message.
*/
#ifndef _FATAL_H
#define _FATAL_H
#include
#include
#define Error(str) FatalError(str)
#define FatalError(str) fprintf(stderr, "%s\n", str), exit(-1)
#endif /* _FATAL_H */
/* * stack.h */ #ifndef _STACK_H #define _STACK_H #define _STACK_ARRAY_ #ifndef NULL #define NULL (0) #endif typedef int ElementType; #ifndef _STACK_ARRAY_ struct Node; typedef struct Node *PtrToNode; typedef PtrToNode Stack; #else /* ifndef _STACK_ARRAY_ */ struct StackRecord; typedef struct StackRecord *Stack; #endif /* ifndef _STACK_ARRAY_ */ #ifdef __cplusplus extern "C" { #endif int IsEmpty(Stack S); #ifndef _STACK_ARRAY_ Stack CreateStack(); #else /* _STACK_ARRAY_ */ Stack CreateStack(int MaxElements); int IsFull(Stack S); ElementType TopAndPop(Stack S); #endif /* _STACK_ARRAY_ */ void MakeEmpty(Stack S); void DisposeStack(Stack S); void Push(ElementType X, Stack S); ElementType Top(Stack S); void Pop(Stack S); #ifdef __cplusplus } #endif #endif /* _STACK_H */
/* * stack.cpp */ #include #include "fatal.h" #include "stack.h" #ifndef _STACK_ARRAY_ /* --------------------Linked List Implementation of Stacks------------------- */ struct Node { ElementType Element; PtrToNode Next; }; /* * Test whether a stack is empty. */ int IsEmpty(Stack S) { return S->Next == NULL; } /* * Create a header node. * Then, set the next pointer to NULL. */ Stack CreateStack() { Stack S; S = (Stack)malloc(sizeof(struct Node)); if (S == NULL) { FatalError("Out of memory!"); } S->Next = NULL; MakeEmpty(S); return S; } void MakeEmpty(Stack S) { if (S == NULL) { FatalError("Must use CreateStack first"); } else { while (!IsEmpty(S)) { Pop(S); } } } void DisposeStack(Stack S) { MakeEmpty(S); free(S); } /* * The Push is implemented as an insertion into the front of a linked list, * where the front of the list serves as the top of the stack. */ void Push(ElementType X, Stack S) { PtrToNode TmpCell; TmpCell = (PtrToNode)malloc(sizeof(struct Node)); if (TmpCell == NULL) { FatalError("Out of memory!"); } else { TmpCell->Element = X; TmpCell->Next = S->Next; S->Next = TmpCell; } } /* * Retrieve the element in the first position of the list. */ ElementType Top(Stack S) { if (!IsEmpty(S)) { return S->Next->Element; } else { Error("Empty stack"); return -1; /* Return value used to avoid warning */ } } /* * Delete a cell from the front of the list. */ void Pop(Stack S) { PtrToNode FirstCell; if (IsEmpty(S)) { FatalError("Empty stack"); } else { FirstCell = S->Next; S->Next = S->Next->Next; free(FirstCell); } } #else /* #ifndef _STACK_ARRAY_ */ /* --------------------Array Implementation of Stacks------------------- */ #define EmptyTOS (-1) #define MinStackSize (5) struct StackRecord { int Capacity; int TopOfStack; ElementType *Array; }; /* * Test whether stack is empty. */ int IsEmpty(Stack S) { return S->TopOfStack == EmptyTOS; } /* * Test whether stack is full. */ int IsFull(Stack S) { return S->TopOfStack == S->Capacity - 1; } /* * Create stack. */ Stack CreateStack(int MaxElements) { Stack S; if (MaxElements < MinStackSize) { FatalError("Stack size is too small!"); } S = (Stack)malloc(sizeof(struct StackRecord)); if (S == NULL) { FatalError("Out of memory!"); } S->Array = (ElementType *)malloc(sizeof(ElementType) * MaxElements); if (S->Array == NULL) { FatalError("Out of memory!"); } S->Capacity = MaxElements; MakeEmpty(S); return S; } /* * Make stack empty. */ void MakeEmpty(Stack S) { S->TopOfStack = EmptyTOS; } /* * Free the stack structure. */ void DisposeStack(Stack S) { if (S != NULL) { free(S->Array); free(S); } } /* * A Push on a full stack will overflow the array bounds and cause a crash. */ void Push(ElementType X, Stack S) { if (IsFull(S)) { FatalError("Full stack!"); } else { S->Array[++S->TopOfStack] = X; } } /* * Return top of stack. */ ElementType Top(Stack S) { if (IsEmpty(S)) { FatalError("Empty stack!"); return 0; /* Return value used to avoid warning */ } else { return S->Array[S->TopOfStack]; } } /* * A Pop on an empty stack will underflow the array bounds and cause a crash. */ void Pop(Stack S) { if (IsEmpty(S)) { FatalError("Empty stack!"); } else { --S->TopOfStack; } } ElementType TopAndPop(Stack S) { if (IsEmpty(S)) { FatalError("Empty stack!"); return 0; /* Return value used to avoid warning */ } else { return S->Array[S->TopOfStack--]; } } #endif /* #ifndef _STACK_ARRAY_ */
/* * teststack.cpp */ #include #include "stack.h" int main() { Stack S = NULL; int i; #ifndef _STACK_ARRAY_ S = CreateStack(); #else S = CreateStack(12); #endif for (i = 0; i < 10; ++i) { Push(i, S); } while (!IsEmpty(S)) { printf("%d\n", Top(S)); Pop(S); } DisposeStack(S); return 0; }
相关文章推荐
- cocos2d-x 中的数据结构集合(容器)(CCArray,Map,Vector)使用笔记
- 初学数据结构之排序
- 数据结构之堆栈计算器
- C++类实现hash表的抽象数据结构(链式解决冲突)
- 数据结构(29)图的遍历
- 数据结构(28)图的实现
- 数据结构(27)二叉树的操作
- 数据结构(26)二叉树
- 数据结构(25)树
- 数据结构(24)队列--两个栈实现
- 数据结构(23)队列--单链表实现
- 数据结构(22)循环队列--线性表实现
- 数据结构(21)队列--线性表实现
- 数据结构(20)栈--单链表实现
- 数据结构(19)栈的线性实现
- 数据结构(18)双向循环链表
- 数据结构(17)双向链表
- 数据结构(16)循环链表
- 数据结构实验之栈一:进制转换 SDUT 2131
- 数据结构07:查找