[算法学习笔记]数据结构之栈和队列
来源:互联网 发布:淘宝修改宝贝详情影响 编辑:程序博客网 时间:2024/05/01 08:55
栈
栈是一种后进先出的数据结构
C++ 实现代码
#include <iostream>using namespace std;class Stack{private: int *A; int maxSize; int curSize;public: Stack(); Stack(int maxSize); ~Stack(); bool isEmpty() const; bool isFull() const; int length() const; void push(int n); int pop(); void display();};int main(){ Stack stack(5); int choose, n; cout << "menu: 1> push 2> pop 3> display 0> eixt\nInput: "; while(scanf("%d", &choose) && choose){ switch (choose) { case 1: cout << "Input: "; cin >> n; try{ stack.push(n); cout << "Output: success!" << endl; }catch(const char *s){ cout << "Output: fail! " << s << endl; } break; case 2: try{ n = stack.pop(); cout << "Output: " << n << " is pop!" << endl; }catch(const char *s){ cout << "Output: fail! " << s << endl; } break; case 3: cout << "Output: "; stack.display(); cout << endl; break; default: printf("Output: error choose\n"); break; } cout << "Input: "; } cout <<"Output: Done!\n"; return 0;}Stack::Stack(){ maxSize = 10; curSize = 0; A = new int[maxSize];}Stack::Stack(int maxSize){ this->maxSize = maxSize; curSize = 0; A = new int[this->maxSize];}Stack::~Stack(){ delete [] A;}bool Stack::isEmpty() const { return curSize == 0;}bool Stack::isFull() const { return curSize == maxSize ;}int Stack::length() const { return curSize;}void Stack::push(int n){ if(isFull()) throw "Stack is full!"; A[curSize] = n; curSize++;}int Stack::pop(){ if(isEmpty()) throw "Stack is empty!"; curSize--; return A[curSize];}void Stack::display(){ for(int i = 0; i < curSize; i++) cout << A[i] << " ";}
队列
队列是一种先进先出的数据结构
C++实现
#include <iostream>using namespace std;class Queue{private: int *A; int maxSize; int curSize;public: Queue(); Queue(int maxSize); ~Queue(); bool isEmpty() const; bool isFull() const; int length() const; void push(int n); int pop(); void display();};int main(){ Queue queue(5); int choose, n; cout << "menu: 1> push 2> pop 3> display 0> eixt\nInput: "; while(scanf("%d", &choose) && choose){ switch (choose) { case 1: cout << "Input: "; cin >> n; try{ queue.push(n); cout << "Output: success!" << endl; }catch(const char *s){ cout << "Output: fail! " << s << endl; } break; case 2: try{ n = queue.pop(); cout << "Output: " << n << " is pop!" << endl; }catch(const char *s){ cout << "Output: fail! " << s << endl; } break; case 3: cout << "Output: "; queue.display(); cout << endl; break; default: printf("Output: error choose\n"); break; } cout << "Input: "; } cout <<"Output: Done!\n"; return 0;}Queue::Queue(){ maxSize = 10; curSize = 0; A = new int[maxSize];}Queue::Queue(int maxSize){ this->maxSize = maxSize; curSize = 0; A = new int[this->maxSize];}Queue::~Queue(){ delete [] A;}bool Queue::isEmpty() const { return curSize == 0;}bool Queue::isFull() const { return curSize == maxSize ;}int Queue::length() const { return curSize;}void Queue::push(int n){ if(isFull()) throw "Queue is full!"; A[curSize] = n; curSize++;}int Queue::pop(){ if(isEmpty()) throw "Queue is empty!"; int front = A[0]; curSize--; for(int i = 0; i < curSize; i++){ A[i] = A[i+1]; } return front;}void Queue::display(){ for(int i = 0; i < curSize; i++) cout << A[i] << " ";}
这两种数据结构比较简单就不特别说明了
1 0
- [算法学习笔记]数据结构之栈和队列
- 数据结构之队列和栈学习笔记
- 《算法学习与数据结构》part1 算法分析与数据结构之表、栈和队列
- 数据结构学习笔记之表、栈、队列
- 数据结构与算法之栈和队列
- 数据结构与算法之栈和队列
- 数据结构学习笔记之队列
- 数据结构学习笔记之队列
- 《Java数据结构和算法》学习笔记(3)——栈和队列
- 【算法和数据结构】1.4--数据结构之队列
- 《数据结构与算法分析》学习笔记一:表、栈和队列
- 【Java数据结构学习笔记之三】Java数据结构与算法之队列(Queue)实现
- 数据结构与算法学习笔记:栈与队列
- Java之数据结构基础、线性表、栈和队列、数组和字符串,树—学习笔记
- Java之数据结构基础、线性表、栈和队列、数组和字符串,树—学习笔记
- 数据结构之栈、队列算法
- 数据结构和算法学习(4)-栈和队列
- 数据结构笔记--栈和队列
- 第一个博文
- UE4 UnrealEngine4 中 实现多通道技术
- 【9912】采药
- jquery实现按回车触发事件
- hdu 1302
- [算法学习笔记]数据结构之栈和队列
- 传入字典的模型项的类型为
- 映美精(IMAGINGSOURCE)相机与OPENCV库
- 每日一linux命令(37)-------vmstat命令
- Eclipse4.X版本安装fatjar插件(luna mars 版本均可用)
- 棋盘格的研究
- 使用表单标签 与用户交互
- Handler消息传递机制(一)理解到底为什么?
- 为测试MM写个拖拽安装apk批处理