• 链栈(c++实现)


    代码如下:

      1 #include <iostream>
      2 
      3 using namespace std;
      4 
      5 struct stackNode   //栈节点
      6 {
      7     int value;
      8     stackNode* next;
      9 
     10     stackNode()  //无参构造
     11     {
     12         this->value = 0;
     13         this->next = NULL;
     14     }
     15 
     16     stackNode(int data) //有参构造
     17     {
     18         this->value = data;
     19         this->next = NULL;
     20     }
     21 
     22     
     23
     24     void changeValue(int data)  //改数值
     25     {
     26         this->value = data;
     27         this->next = NULL;
     28     }
     29 };
     30 
     31 
     32 class NewStack
     33 {
     34 public:
     35     NewStack();
     36     ~NewStack() {}
     37     bool isEmpty();  //判断是否为空
     38     void push(int data);  //压栈
     39     void pop();  //弹栈
     40     int getTop();  //返回栈顶元素
     41     int getNum();  //栈元素数量
     42     void clean();  //清空栈
     43 private:
     44     stackNode* top = new stackNode();
     45     stackNode* current;
     46     int nodeNum;
     47     void clean(stackNode *p);
     48 };
     49 
     50 NewStack::NewStack()
     51 {
     52     top->next = NULL;
     53     current = top->next;
     54     nodeNum = 0;
     55 }
     56 
     57 bool NewStack::isEmpty()
     58 {
     59     if(top->next == NULL)
     60         return true;
     61     return false;
     62 }
     63 
     64 int NewStack::getNum()
     65 {
     66     return nodeNum;
     67 }
     68 
     69 int NewStack::getTop()
     70 {
     71     return current->value;
     72 }
     73 
     74 void NewStack::push(int data)
     75 {
     76     stackNode *newNode = new stackNode(data);
     77     top->next = newNode;
     78     newNode->next = current;
     79     current = newNode;
     80     ++ nodeNum;
     81 }
     82 
     83 void NewStack::pop()
     84 {
     85     if(top->next == NULL)
     86         return ;
     87     stackNode *p = current;
     88     current = current->next;
     89     top->next = current;
     90     delete[] p;
     91     -- nodeNum;
     92 }
     93 
     94 void NewStack::clean(stackNode *p) //递归实现
     95 {
     96     if(p->next != NULL)
     97         clean(p->next);
     98     delete[] p;
     99 }
    100 
    101 void NewStack::clean()
    102 {
    103     if(top->next == NULL)
    104         return ;
    105     clean(current);
    106     top->next = NULL;
    107     nodeNum = 0108 }
    109     
    110 int main()
    111 {
    112     //test
    113 
    114     NewStack s;
    115     for(int i = 0; i < 10; ++i)
    116     {
    117         s.push(i);
    118     }
    119 
    120     cout << "The num of the stack is" << s.getNum() << endl;
    121     cout << "The top of the stack is" << s.getTop() << endl;
    122     cout << endl;
    123 
    124     for(int i = 0;i < 5; ++i)
    125     {
    126         s.pop();
    127         cout << "The num of the stack is" << s.getNum() << endl;
    128         cout << "The top of the stack is" << s.getTop() << endl;
    129         cout << endl;
    130     }
    131 
    132     s.clean();
    133 
    134     if(s.isEmpty() == true)
    135         cout << "The stack is empty" << endl;
    136     else
    137         cout << "The stack is not empty" << endl;
    138     
    139 
    140     return 0;
    141 }
  • 相关阅读:
    这些诗词你知道一句,却不知全诗!
    二十二、事件绑定及深入
    二十一、事件对象
    二十、事件入门
    十九、动态加载脚本和样式
    十八、DOM元素尺寸和位置
    十七、DOM操作表格及样式
    十六、DOM进阶
    十五、DOM基础
    正确使用volatile场景--状态标志
  • 原文地址:https://www.cnblogs.com/baiweituyou/p/13434289.html
Copyright © 2020-2023  润新知