• 共享空间顺序栈


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

    关注公众号 海量干货等你
  • 相关阅读:
    java之md5加密算法
    springboot之快速创建项目
    java之idea打jar包
    java只http改成https访问
    springboot之读取配置文件
    springboot之项目打包
    Git之fatal: remote origin already exists
    WebStorm ES6 语法支持设置和ES6语法的JS文件编译为ES5语法文件
    转 JavaScript里的数组转化新方法Array.From
    网页特殊符号HTML代码大全
  • 原文地址:https://www.cnblogs.com/sowhat1412/p/12734511.html
Copyright © 2020-2023  润新知