【算法和数据结构】1.5--数据结构之栈
来源:互联网 发布:莎莎源码 解压密码 编辑:程序博客网 时间:2024/05/22 00:38
在前面和大家分享了两种线性表(List)结构:链表和队列。今天和大家分享List的最后一种结构—栈(stack)。
首先,给出栈的一个通常定义:一个运算受限的线性表(A restricted form of list): Insert and remove only at front of list. 其具有重要特征:LIFO,Last in,first out.
我们在栈上定义两种操作:
- 插入:PUSH,向栈插入元素
- 删除:POP,删除栈中指定的元素
我们将栈中当前可操作元素称之为Top元素。
栈由于其LIFO性质有很多实际应用(或者我们可以说栈是根据实际应用而设计具有LIFO性质的线性表),这里不再一一列举,有兴趣的读者可以查找相关资料。
下面给出栈的C++代码实现:
//Array based stack: First in, last outclass AStack{private: int* stackArray; int maxSize; int top; //the position to insert a new elementpublic: AStack(int size) { maxSize = size; top = 0; stackArray = new int[maxSize]; } ~AStack() { clear(); delete[] stackArray; } void clear() { top = 0; } int getElementNum() { return top; } bool push(const int& valuePush) { if (top == maxSize) return false; //a full stack else { stackArray[top++] = valuePush; return true; } } //Push an element on the top of the stack bool pop(int& valuePop) { if (top == 0) return false; //an empty stack else { valuePop = stackArray[--top]; return true; } } //Remove the top element of the stack and put it in "valuePop" bool topValue(int& valueTop) { if (top == 0) return false; else { valueTop = stackArray[top - 1]; return true; } } void print() { if (top == 0) cout << "This is an empty stack" << endl; else { for (int i = 0;i < top;i++) cout << stackArray[i] << " "; cout << endl; } }};//Linked stackclass StackNode{public: int element; //Value of this node StackNode* next; //Pointer to next node StackNode(const int & elementValue, StackNode* nextNode) { element = elementValue; next = nextNode; }};class LStack{private: StackNode* top; int size; //Count of elementspublic: LStack() { top = NULL; size = 0; } ~LStack() { clear(); } void clear() { while (top != NULL) { StackNode* temp = top; top = top->next; delete temp; } size = 0; } int getSize() { return size; } bool push(const int& valuePush) { top = new StackNode(valuePush, top); size++; return true; } bool pop(int& valuePop) { if (size == 0) return false; valuePop = top->element; StackNode* temp = top->next; delete top; top = temp; size--; return true; } bool topValue(int& valueTop) const { if (size == 0) return false; valueTop = top->element; return true; } void print() { if (size == 0) cout << "This is an empty stack" << endl; else { StackNode* temp = top; while (temp != NULL) { cout << temp->element << " "; temp = temp->next; } cout << endl; } }};
以上为本人关于栈的一些学习心得,欢迎广大读者朋友批评指正。
1 0
- 【算法和数据结构】1.5--数据结构之栈
- 数据结构复习之【数据结构和算法概念】
- 数据结构之【数据结构和算法概念】--复习
- 【算法和数据结构】1.4--数据结构之队列
- 数据结构算法之栈
- 数据结构和算法系列之 栈
- 数据结构与算法之栈和队列
- 数据结构与算法之栈和队列
- 【算法和数据结构】栈
- 数据结构和算法之C#
- 《数据结构和算法》之汉诺塔
- 《数据结构和算法》之字符串BF算法
- 《数据结构和算法》之KMP算法
- 《算法学习与数据结构》part1 算法分析与数据结构之表、栈和队列
- 数据结构之栈、队列算法
- 数据结构与算法之栈
- 【算法和数据结构】1.1--数据结构之线性表
- 【算法和数据结构】1.8--数据结构之图(C++实现)
- 蓝桥算法训练 表达式计算
- 【CUDA开发】论CUDA和LAV解码器是否真的实用
- 请求服务器时的状态码详解
- IBInspectable / IBDesignable
- 小金掰呀掰不够玉米 (sdut oj)
- 【算法和数据结构】1.5--数据结构之栈
- MarkDown学习笔记
- hdu 1800 Flying to the Mars(多解)
- iOS-IBDesignable/IBInspectable动态改变控件属性
- spring 构造函数注入
- KiCad设计PCB-33-串口下载、AT24C02电路、CAN、蜂鸣器
- C/C++练习7---求某个范围内的所有素数 (sdut oj)
- 机器学习解决问题的步骤
- NYOJ39-水仙花数