C++ 写栈和队列

栈的代码, 比较容易实现:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <algorithm>  
  4. #include <string>  
  5. #include <cstring>  
  6. using namespace std;  
  7. class Point{  
  8. public:  
  9.     int x, y;  
  10.     Point(int x, int y):x(x), y(y){}  
  11.     Point(){}  
  12. };  
  13. template<class T>  
  14. class Stack{  
  15. public:  
  16.     Stack(){  
  17.         cur = 0;  
  18.     }  
  19. private:   
  20.     T s[1000];  
  21.     int cur;  
  22. public:   
  23.     void push(T t){  
  24.         s[cur++] = t;  
  25.     }  
  26.     T top(){  
  27.         return s[cur - 1];  
  28.     }  
  29.     void pop(){  
  30.         cur--;  
  31.     }  
  32. };  
  33.   
  34. int main()  
  35. {  
  36.     Stack<Point>s;  
  37.     s.push(Point(1, 2));  
  38.     s.push(Point(4, 5));  
  39.     //s.pop();  
  40.     cout << s.top().x << endl;  
  41.     return 0;  
  42. }  

队列的代码, 有链表实现:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <algorithm>  
  4. #include <string>  
  5. #include <cstring>  
  6. using namespace std;  
  7.   
  8. class Node{  
  9. public:  
  10.     Node *next;  
  11.     Node *pre;  
  12.     int t;  
  13.     Node(){  
  14.         next = NULL;  //这两个都得是NULL  
  15.         pre = NULL;  
  16.     }  
  17.     Node(int t):t(t){  
  18.     }  
  19. };  
  20. template<class T>  
  21. class Link{  
  22. public:  
  23.     T *head;          //指向队列头  
  24.     T *tell;          //指向队列尾  
  25.     int len;  
  26.     Link(){  
  27.         head = new T(); //一开始为脑袋实例化, 注意head里面不存数据, 只是标识队列  
  28.         tell = NULL;    //一开始尾巴为空  
  29.         len = 0;  
  30.     }  
  31.     void insert(T t){  
  32.         T *newT = new T();  
  33.         *newT = t;              //为声明的空间赋值, 这里需要重载T的赋值符号=  
  34.           
  35.         //让老二和newT关联  
  36.         T *tmp = head->next;  
  37.         newT->next = tmp;  
  38.         if(NULL != tmp)  //如果没有老二  
  39.             tmp->pre = newT;  
  40.   
  41.         //让新的老二和脑袋关联  
  42.         head->next = newT;  
  43.         newT->pre = head;  
  44.         //当长度为0的时候就更新tell的指向  
  45.         if(0 == len){      
  46.             tell = newT;  
  47.         }  
  48.         len++;  
  49.     }  
  50.     T front(){  
  51.         if(0 == len)  
  52.             return NULL;  
  53.         return *tell;  
  54.     }  
  55.     void pop(){  
  56.         if(0 == len){  
  57.             tell = NULL;  
  58.             return;  
  59.         }  
  60.         else{      //删除尾指针所指向的空间后更新尾指针的指向  
  61.             T *tmp = tell->pre;  
  62.             delete tell;  
  63.             tell = tmp;  
  64.             len--;  
  65.         }  
  66.     }  
  67.     bool empty(){  
  68.         return 0 == len;  
  69.     }  
  70.   
  71. };  
  72.   
  73. int main(){  
  74.     Link<Node>l;  
  75.     l.insert(Node(12));  
  76.     l.insert(Node(23));  
  77.     l.insert(Node(27));  
  78.     l.pop();  
  79.     l.pop();  
  80.     l.pop();  
  81.     if(!l.empty())  
  82.         cout << l.front().t << endl;  
  83.     else{  
  84.         cout << "l is empty" << endl;  
  85.     }  
  86.     return 0;  
  87. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值