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;
    }

    关注公众号 海量干货等你
  • 相关阅读:
    [UVA10859 放置街灯 Placing Lampposts]
    洛谷7月月赛题解(2020)
    [学习笔记]马拉车-Manacher
    [SP1026] FAVDICE
    [NOIP2013]货车运输
    [洛谷P1801]黑匣子
    [HAOI2015]树上染色
    python-第二块:time模块和datatime模块
    python-作业:员工信息表
    python-第二块,笔记整理和学习内容复习(day7)
  • 原文地址:https://www.cnblogs.com/sowhat1412/p/12734511.html
Copyright © 2011-2022 走看看