• c++链栈


    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    namespace mystack2
    {
        typedef int type;
        struct node
        {
            type data;
            node*next;
        };
        class stack
        {
            enum{maxsize=1000};
            node*head;
            int items;
        public:
            stack();
            ~stack();
            bool push(type x);
            bool pop(type &x);
            bool pop();
            type top();
            void fill();
            void show();
        };
        stack::stack(){
            head=new node;
            head->next=NULL;
            items=0;
        }
        stack::~stack(){
            node*p;
            while(head){
                p=head;
                head=head->next;
                delete p;
            }
        }
        bool stack::push(type x){
            if(items==maxsize)
                return false;
            node*t=new node;
            t->data=x;
            t->next=head->next;
            head->next=t;
            items++;
            return true;
        }
        bool stack::pop(type &x){
            if(items==0)
                return false;
            node*p=head->next;
            x=p->data;
            head->next=p->next;
            items--;
            return true;
        }
        bool stack::pop(){
            if(items==0)
                return false;
            node*p=head->next;
            head->next=p->next;
            items--;
            return true;
        }
        type stack::top(){
            if(items==0)
                return -1;
            else return head->next->data;
        }
        void stack::fill(){
            for(int i=0;i<10;i++){
                this->push(i);
            }
        }
        void stack::show(){
            node*p=head->next;
            while(p){
                cout<<p->data<<" ";
                p=p->next;
            }
            cout<<endl;
        }
    }
    int main()
    {
        using namespace mystack2;
        stack one;
        one.fill();
        one.show();
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    (转)投票系统,更改ip刷票
    图像判断(转)
    第06组 Alpha事后诸葛亮
    第06组 Alpha冲刺(6/6)
    2019 SDN上机第4次作业
    第06组 Alpha冲刺(5/6)
    第06组 Alpha冲刺(4/6)
    第06组 Alpha冲刺(3/6)
    第06组 Alpha冲刺(2/6)
    2019 SDN上机第3次作业
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768500.html
Copyright © 2020-2023  润新知