zoukankan      html  css  js  c++  java
  • 共享空间顺序栈

    #include <iostream>
    using namespace std;
    #include <cassert>
    #define MAX 5
    typedef struct sharelist
    {
        int arr[MAX];
        int top;
        int tail;
    }SHARE_LIST;
    
    
    SHARE_LIST* create_list()
    {
        SHARE_LIST* list = new(SHARE_LIST);
        //list->arr[MAX] = {0};
        list->top=0;
        list->tail = MAX-1;
    }
    
    
    bool list_push(SHARE_LIST* list, int data, bool type)
    {
        if(list->top + MAX-1-list->tail ==MAX)
        {
            cout<<"ARRAY has been full !"<<endl;
            assert(false);
        }
        if(true==type)
        {
            list->arr[list->top++]=data;
            return true;
        }
        else
        {
            list->arr[list->tail--]=data;
            return true;
        }
    }
    
    void print(SHARE_LIST* list, bool type)
    {
        if(type)
        {
            if( (list->top==0))
            {
                cout<<"the arr1 empty !";
            }
            else
            {
                while( 0!= list->top)
                {
                 cout<<list->arr[--list->top]<<" ";
                }
            }
        }
        else
        {
            if((list->tail==MAX-1))
            {
                cout<<"the arr2 empty !";
            }
            else
            {
                while((MAX-1)!=list->tail)
                cout<<list->arr[++ list->tail]<<" ";
            }
        }
        cout<<endl;
    }
    
    
    int list_size(SHARE_LIST* list,bool type)
    {
        if(type)
        {
            return list->top;
        }
        else
        {
            return MAX-1-list->tail;
        }
    }
    
    
    int main()
    {
        SHARE_LIST* list = create_list();
        list_push(list, 1, true);
        list_push(list, 2, true);
        list_push(list, 3, true);
    
        list_push(list, 5, false);
        list_push(list, 4, false);
    
        // list_push(list, 4, true);
    
        cout<<"the first arrary size is: ";
        cout<<list_size(list,true)<<endl;
        cout<<"the first arrary element are:";
        print(list,true);
        cout<<endl;
    
        list_size(list,false);
        cout<<"the second arrary size is: ";
        cout<<list_size(list,false)<<endl;
        cout<<"the second arrary element are:";
        print(list,false);
        return 0;
    }

    关注公众号 海量干货等你
  • 相关阅读:
    urllib3使用池管理发送请求和requests常用方法的基本使用+session使用
    Ajax爬取动态数据和HTTPS自动默认证书
    urllib库中的URL编码解码和GETPOST请求
    urllib的使用和进阶——urllib.request
    1.8学习进度总结
    1.7学习进度总结
    1.5学习进度总结
    1.4学习进度总结
    第十二周周进度总结
    第十一周周进度总结
  • 原文地址:https://www.cnblogs.com/sowhat1412/p/12734511.html
Copyright © 2011-2022 走看看