算法导论 简单顺序栈
2015-09-10 21:00
281 查看
简单顺序栈
1.什么是栈?什么是顺序栈?
栈是一种基本的数据结构,栈这种数据结构实现的是一种对元素进行后进先出(last-in, first-out,LIFO)的策略。栈的顺序存储结构称为顺序栈。顺序栈可以用一个一维数组和一个记录栈顶位置的整形变量来实现,数组用于顺序存储栈中所有的数据元素,栈顶指针用于存储栈顶元素的位置。2.顺序栈的基本操作(伪代码)
STACK-EMPTY(S)if S.top == 0 return TRUE else return FALSE
PUSH(S, x) //这里未考虑上溢
S.top = S.top + 1 S[S.top] = x
POP(S)
if STACK-EMPTY(S) error "underflow" else S.top = S.top - 1 return S[S.top + 1]
3.C++实现顺序栈的基本操作
//SequeStack.h #pragma once #include <assert.h> template<typename ElemType> class SequeStack { public: SequeStack(unsigned int size); bool Push(ElemType elem); bool Pop(ElemType* retElem); bool Empty() const; bool Visit(ElemType* elem, unsigned int pos) const; private: ElemType* m_array; unsigned int m_top; unsigned int m_size; }; template<typename ElemType> bool SequeStack<ElemType>::Visit(ElemType* elem, unsigned int pos) const { if (pos >= m_size || pos < 0) { assert(false && "Error: Visit Pos is out range of array!"); return false; } *elem = m_array[pos]; return true; } template<typename ElemType> bool SequeStack<ElemType>::Empty() const { if (m_top) { return false; } else { return true; } } template<typename ElemType> bool SequeStack<ElemType>::Pop(ElemType* retElem) { if (Empty()) { assert(false && "Error: SequeStack is underflow!"); return false; } else { *retElem = m_array[--m_top]; return true; } } template<typename ElemType> bool SequeStack<ElemType>::Push(ElemType pushElem) { if (m_top == m_size) { assert(false && "Error: SequeStack is overflow!"); return false; } else { m_array[m_top++] = pushElem; return true; } } template<typename ElemType> SequeStack<ElemType>::SequeStack(unsigned int size) : m_array(new ElemType[size]),m_top(0),m_size(size) { memset(m_array,0,sizeof(ElemType)*size); }
//Util.h #pragma once namespace Util { template<typename T> void PrintMemory(T& dateStruct, unsigned int size) { cout << "PrintMemory: "; for (int i = 0; i != size; i++) { ElemType tempElem; dateStruct.Visit(&tempElem,i); printf("%d ",tempElem); } printf("\n"); } }
//main.cpp #include "SequeStack.h" #include "Util.h" #include <iostream> using namespace std; typedef int ElemType; int main() { const int STACK_SIZE = 10; SequeStack<ElemType> testSequeStack(STACK_SIZE); Util::PrintMemory(testSequeStack,STACK_SIZE); cout << (testSequeStack.Empty() ? "Empty SequeStack." : "Not Empty SequeStack.") << endl; for (int i = 1; i != 5; i++) { testSequeStack.Push(i); cout << "\nPush:" << i << endl; Util::PrintMemory(testSequeStack,STACK_SIZE); cout << (testSequeStack.Empty() ? "Empty SequeStack." : "Not Empty SequeStack.") << endl; } for(int i = 1; i!= 5; i++) { int temp; testSequeStack.Pop(&temp); cout << "\nPop:" << temp << endl; Util::PrintMemory(testSequeStack,STACK_SIZE); cout << (testSequeStack.Empty() ? "Empty SequeStack." : "Not Empty SequeStack.") << endl; } return 0; }
4.运行结果
PrintMemory: 0 0 0 0 0 0 0 0 0 0Empty SequeStack.
Push:1
PrintMemory: 1 0 0 0 0 0 0 0 0 0
Not Empty SequeStack.
Push:2
PrintMemory: 1 2 0 0 0 0 0 0 0 0
Not Empty SequeStack.
Push:3
PrintMemory: 1 2 3 0 0 0 0 0 0 0
Not Empty SequeStack.
Push:4
PrintMemory: 1 2 3 4 0 0 0 0 0 0
Not Empty SequeStack.
Pop:4
PrintMemory: 1 2 3 4 0 0 0 0 0 0
Not Empty SequeStack.
Pop:3
PrintMemory: 1 2 3 4 0 0 0 0 0 0
Not Empty SequeStack.
Pop:2
PrintMemory: 1 2 3 4 0 0 0 0 0 0
Not Empty SequeStack.
Pop:1
PrintMemory: 1 2 3 4 0 0 0 0 0 0
Empty SequeStack.
相关文章推荐
- C++Primer第五版 1.4.3节练习
- new int[]和new int()的区别
- toad for oracle中文手册
- Java_jdbc 基础笔记之十二 数据库连接 (beanutils )
- WINDOWS 7 PRO X64 2015年9月增量补丁包微软官方下载地址
- Java编写ArrayBasic制作一个简单的酒店管理系统
- Using git with proxy
- 常用正则表达式
- C++实现单向循环链表
- linker command failed with exit code 1 错误的解决
- 将2N个整数分成两组,每组有N个数,并且满足,这两组的差的绝对值最小。
- Ubuntu 14.04 LTS 输入法无法输入英文半角引号
- linux likely & unlikely 宏
- 【1】基于TQ2440的MP3设计——【5、裁剪和移植Linux 2.6 内核】
- poj 3046
- MySql中时间类型总结
- 数据挖掘和机器学习的区别与联系
- iOS之学习系统提供的框架(库)和自己制作框架
- poj1611 The Suspects
- Android 多渠道打包方案问题记录及解决方案