《C++实现数据结构》:堆栈
来源:互联网 发布:导航源码 编辑:程序博客网 时间:2024/05/20 08:22
堆栈是限定插入和删除操作都在表的同一端进行的线性表,是后进先出(LIFO)的线性数据结构。
#include <iostream>using namespace std;//堆栈基类template <typename T>class Stack{public: virtual bool IsEmpty() const = 0; //若栈空,则返回true virtual bool IsFull() const = 0; //若栈满,则返回true virtual bool Top(T &x) const = 0; //在x中返回栈顶元,若操作成功,则返回true virtual bool Push(T x) = 0; //在栈顶插入元素x(入栈),若操作成功,则返回true virtual bool Pop() = 0; //从栈中删除栈顶元素(出栈),若操作成功,则返回true virtual void Clear() = 0; //清除栈中全部元素};
一、堆栈的顺序表示
和线性表的顺序表示一样,是用数组来构造的。
//堆栈的顺序表示template <typename T>class SeqStack:public Stack<T>{private: int top; //栈顶指针 int maxTop; //最大栈顶指针 T* s;public: SeqStack(int mSize); ~SeqStack(); bool IsEmpty() const; //若栈空,则返回true bool IsFull() const; //若栈满,则返回true bool Top(T &x) const; //在x中返回栈顶元,若操作成功,则返回true bool Push(T x); //在栈顶插入元素x(入栈),若操作成功,则返回true bool Pop(); //从栈中删除栈顶元素(出栈),若操作成功,则返回true void Clear(); //清除栈中全部元素};template <typename T>SeqStack<T>::SeqStack(int mSize) { maxTop = mSize - 1; s = new T[mSize]; top = -1;}template <typename T>SeqStack<T>::~SeqStack() { delete[] s;}template <typename T>bool SeqStack<T>::IsEmpty() const { return -1 == top;}template <typename T>bool SeqStack<T>::IsFull() const { return top == maxTop;}template <typename T>bool SeqStack<T>::Top(T &x) const { if (IsEmpty()) { cout<<"Empty"<<endl; return false; } x= s[top]; return true;}template <typename T>bool SeqStack<T>::Push(T x) { if (IsFull()) { cout << "Full" << endl; return false; } s[++top] = x; return true;}template <typename T>bool SeqStack<T>::Pop() { if (IsEmpty()) { cout<<"Empty"<<endl; return false; } top--; return true;}template <typename T>void SeqStack<T>::Clear() { top = -1;}
二、堆栈的链接表示
表头指针的next永远指向栈顶。
//堆栈的链接表示template <typename T>class SingleStack;template <typename T>class Node{private: T element; Node<T>* next; friend class SingleStack<T>;};template <typename T>class SingleStack:public Stack<T>{private: Node<T>* head; bool IsFull() const{};public: SingleStack(); ~SingleStack(); bool IsEmpty() const; //若栈空,则返回true bool Top(T &x) const; //在x中返回栈顶元,若操作成功,则返回true bool Push(T x); //在栈顶插入元素x(入栈),若操作成功,则返回true bool Pop(); //从栈中删除栈顶元素(出栈),若操作成功,则返回true void Clear(); //清除栈中全部元素};template <typename T>SingleStack<T>::SingleStack() { head = new Node<T>; head->next = NULL;}template <typename T>SingleStack<T>::~SingleStack() { Node<T> *p; while (head != NULL) { p = head; head = head->next; delete (p); }}template <typename T>bool SingleStack<T>::IsEmpty() const { if (head->next == NULL) { return true; } else { return false; }}template <typename T>bool SingleStack<T>::Top(T &x) const { if (IsEmpty()) { cout<<"Empty"<<endl; return false; } x = head->next->element; return true;}template <typename T>bool SingleStack<T>::Push(T x) { Node<T>* newNode = new Node<T>; newNode->element = x; newNode->next = head->next; head->next = newNode;}template <typename T>bool SingleStack<T>::Pop() { if (IsEmpty()) { cout<<"Empty"<<endl; return false; } Node<T>* p = head->next; head->next = p->next; delete (p);}template <typename T>void SingleStack<T>::Clear() { Node<T> *p = head->next; head->next = NULL; Node<T> *q; while (p != NULL) { q = p; p = p->next; delete (q); }}
最后的测试用例:
int main() { int n = 10; Stack<int> *stack = new SeqStack<int>(10); //Stack<int> *stack = new SingleStack<int>(); for (int i = 0; i < 5; ++i) { stack->Push(i); } int x; if (stack->Top(x)) { cout<<"top:"<<x<<endl; } stack->Pop(); if (stack->Top(x)) { cout<<"now top:"<<x<<endl; } return 0;}
0 0
- C语言实现堆栈(栈)的数据结构
- 数据结构堆栈实现(C版本)
- 堆栈数据结构的C语言实现
- 堆栈数据结构的C语言实现
- 数据结构---堆栈(C#)
- 堆栈数据结构的实现~
- 用链表实现堆栈数据结构
- 数据结构--堆栈的实现
- 《C++实现数据结构》:堆栈
- 数据结构-堆栈-数组实现
- 数据结构C语言实现系列——链式堆栈
- 数据结构:泛型堆栈的实现(C++)
- C语言数据结构之堆栈
- 数据结构-堆栈-链表实现
- 堆栈的C实现
- C语言实现堆栈
- 堆栈c实现
- 纯c实现堆栈
- Add Two Numbers
- C语言好难——“_at()” 特殊地址定位指令
- AndroidStuido敏捷开发
- malloc & calloc 的区别
- 关于转义字符 \r,\n,\r\n 的区别
- 《C++实现数据结构》:堆栈
- 乘法口诀表
- PTA 一 天梯地图
- QGis二次开发 -- 源码编译终极篇
- 对UGUI的GridLayoutGroup组件进行拓展
- Python核心编程---读书笔记:第7章~第13章
- 索引设计的一些原则
- MFC modBus 读写串口实例
- 字符串转为整数或浮点数