simple version of stack and queue 's ADT

 
  1. //Stack
  2. template<class datatype>
  3. class stack{
  4. public:
  5.     static const int MAXSIZE = 100; 
  6.     stack() : top(0) {
  7.     }
  8.     bool empty() const {
  9.         return top == 0;
  10.     }
  11.     void push(const datatype & x) {
  12.         if(top == MAXSIZE)
  13.             throw "StackOverflow";
  14.         data[top++] = x;
  15.     }
  16.     void pop() {
  17.         if(empty())
  18.             throw "StackEmpty";
  19.         top--;
  20.     }
  21.     void get_top() const{
  22.         if(empty())
  23.             throw "StackEmpty";
  24.         return data[top-1];
  25.     }
  26. private:
  27.     datatype data[MAXSIZE];
  28.     int top;
  29. };
  30. ///
  31. //****************************************************************
  32. /
  33. //queue
  34. template<class datatype>
  35. class queue {
  36. public:
  37.     static const int MAXSIZE = 100;
  38.     queue() {
  39.         front = rear = 0;
  40.     }
  41.     bool is_empty() const{
  42.         return front == rear;
  43.     }
  44.     bool is_full() const {
  45.         return rear == MAXSIZE;
  46.     }
  47.     void insert(datatype x) {
  48.         if(is_full())
  49.             throw "Queue Overflow";
  50.         data[rear++] = x;
  51.     }
  52.     void delete() {
  53.         if(is_empty())
  54.             throw "Queue Empty";
  55.         front++;
  56.     }
  57.     datatype get_front() const{
  58.         if(is_empty())
  59.             throw "Queue Empty";
  60.         return data[front];
  61.     }
  62. private:
  63.     datatype data[MAXSIZE];
  64.     int front;
  65.     int rear;
  66. };
  67. //^_^
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值