C++ 写栈 和 队列
来源:互联网 发布:mac禁止开机启动项 编辑:程序博客网 时间:2024/06/08 18:24
栈的代码, 比较容易实现:
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cstring>using namespace std;class Point{public:int x, y;Point(int x, int y):x(x), y(y){}Point(){}};template<class T>class Stack{public:Stack(){cur = 0;}private: T s[1000];int cur;public:void push(T t){s[cur++] = t;}T top(){return s[cur - 1];}void pop(){cur--;}};int main(){Stack<Point>s;s.push(Point(1, 2));s.push(Point(4, 5));//s.pop();cout << s.top().x << endl;return 0;}
队列的代码, 有链表实现:
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cstring>using namespace std;class Node{public:Node *next;Node *pre;int t;Node(){next = NULL; //这两个都得是NULLpre = NULL;}Node(int t):t(t){}};template<class T>class Link{public:T *head; //指向队列头T *tell; //指向队列尾int len;Link(){head = new T(); //一开始为脑袋实例化, 注意head里面不存数据, 只是标识队列tell = NULL; //一开始尾巴为空len = 0;}void insert(T t){T *newT = new T();*newT = t; //为声明的空间赋值, 这里需要重载T的赋值符号=//让老二和newT关联T *tmp = head->next;newT->next = tmp;if(NULL != tmp) //如果没有老二tmp->pre = newT;//让新的老二和脑袋关联head->next = newT;newT->pre = head;//当长度为0的时候就更新tell的指向if(0 == len){ tell = newT;}len++;}T front(){if(0 == len)return NULL;return *tell;}void pop(){if(0 == len){tell = NULL;return;}else{ //删除尾指针所指向的空间后更新尾指针的指向T *tmp = tell->pre;delete tell;tell = tmp;len--;}}bool empty(){return 0 == len;}};int main(){Link<Node>l;l.insert(Node(12));l.insert(Node(23));l.insert(Node(27));l.pop();l.pop();l.pop();if(!l.empty())cout << l.front().t << endl;else{cout << "l is empty" << endl;}return 0;}
0 0
- C++ 写栈 和 队列
- C++ 写栈和队列
- C++:栈和队列
- 数据结构-------C栈和队列
- C实现栈和队列
- 【C语言/C++】 栈和队列
- C语言实现,顺序队列,循环队列,和栈!
- C/C++中的堆、栈和队列
- 栈和队列 C语言实现
- 数据结构与算法(C#)--栈和队列
- 数据结构伪C代码:栈和队列
- 【C++】STL队列和栈的使用
- 栈和队列的操作(c)
- 栈和队列(C语言版)
- 括号分配(栈和队列)C
- c/c++数据结构 栈和队列
- 【数据结构】【C++STL】栈和FIFO队列
- (C++)栈和队列的模拟实现
- 【10.2移动新特性】GraphicsLayer新增动态模式,性能极大提升!
- 内存泄露
- 写代码文章最好这样
- 人生哲理
- [Java] Learning_Notes for J2SE
- C++ 写栈 和 队列
- cetos 上 开启 crontab的日志 和调试,并运行 和调试 java 脚本
- 【转载】水晶报表VS2010版《无比感谢楼主分享!!!》
- MTK Android Driver :lcm
- python enumerate函数
- surface surfaceTexture
- 一个月断网了
- openldap 、delta_crl生成及ssl证书知识链接
- hdu 1573 X问题