zoukankan      html  css  js  c++  java
  • 堆栈之链表实现

    #include<iostream>
    using namespace std;
    struct LinktackNode
    {
         LinktackNode* lastIn;
         int value;
    };
    struct LinkStack
    {
         LinktackNode* top;
         LinktackNode* bottom;
         bool isEmpty;
         int cnt;
    };
    LinkStack* createLinkStack()
    {
         LinkStack* st = (LinkStack*)malloc(sizeof(LinkStack));
         LinktackNode* temp = (LinktackNode*)malloc(sizeof(LinktackNode));
         temp->lastIn=NULL;
         st->top=temp;
         st->bottom=temp;
         st->cnt=0;
         st->isEmpty=true;
         return  st;
    }
    
    bool isEmpty(LinkStack* st)
    {
        return st->isEmpty;
    }
     
    void push(LinkStack* st,int value)
    {
        LinktackNode* inNode = (LinktackNode*)malloc(sizeof(LinktackNode));
        inNode->lastIn=st->top;
        inNode->value=value;
        st->top=inNode;
        st->isEmpty =((++st->cnt)==0);
    }
    bool pop(LinkStack* st,int* ans)
    {
        if(!st->isEmpty)
        {
            *ans=st->top->value;
            st->top=st->top->lastIn;
            st->isEmpty =((--st->cnt)==0);
            return 1;
        }
        return 0;
    }
    bool top(LinkStack* st,int* ans)
    {
        if(!st->isEmpty)
        {
            *ans=st->top->value;
            return 1;
        }
        return 0;
    }
    void clearStack(LinkStack* st)
    {
        int x;
        while(!st->isEmpty)
        {
            pop(st,&x);
            cout<<x<<" ";
        }
        cout<<endl;
    }
    
    void outPut(LinkStack* st)
    {
        LinktackNode* p = st->top;
        while(p!=st->bottom)
        {
            cout<<p->value<<" ";
            p=p->lastIn;
        }
        cout<<endl;
    }
    
     
    void main()
    {
        int len=10;
        LinkStack* st = createLinkStack();
        int v;
        for(int i=0;i<len;i++)
        {
             v = rand() % 100;
             cout<<v<<" ";
             push(st,v);
    
        }
        cout<<endl;
        outPut(st);
        clearStack(st);
    
        for(int i=0;i<len;i++)
        {
             v = rand() % 100;
             cout<<v<<" ";
             push(st,v);
    
        }
        cout<<endl;
        outPut(st);
        
        pop(st,&v);
        cout<<v<<endl;
        outPut(st);
    
        top(st,&v);
        cout<<v<<endl;
        outPut(st);
    
        pop(st,&v);
        cout<<v<<endl;
        outPut(st);
    
        pop(st,&v);
        cout<<v<<endl;
        outPut(st);
    
        for(int i=0;i<5;i++)
        {
             v = rand() % 100;
             cout<<v<<" ";
             push(st,v);
    
        }
        cout<<endl;
        outPut(st);
        if(!top(st,&v))
            cout<<"fail"<<endl;
        else outPut(st);
    
        clearStack(st);
    
        if(!top(st,&v))
            cout<<"fail"<<endl;
        else outPut(st);
    
        if(!top(st,&v))
            cout<<"fail"<<endl;
        else outPut(st);
    
        cin>>len;
    }
  • 相关阅读:
    EL&Filter&Listener:EL表达式和JSTL,Servlet规范中的过滤器,Servlet规范中的监听器,观察着设计模式,监听器的使用,综合案例学生管理系统
    Node搭建api接口
    菜鸟程序员的react TodoList练习之旅
    js 手机靓号正则
    js 去除省市区
    select 下拉框在手机上第一次点击获取不到值
    ios下输入框聚焦文字显示不出来
    浅谈js中的深浅拷贝
    原型和原型链
    将多个对象合并成一个数组的方法
  • 原文地址:https://www.cnblogs.com/kbyd/p/3991741.html
Copyright © 2011-2022 走看看