共享空间顺序栈

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

posted @ 2017-03-02 09:36  sowhat1412  阅读(68)  评论(0编辑  收藏  举报