【数据结构】栈-共享存储空间的顺序栈

共享存储空间的顺序栈的基本类型定义:

typedef struct{
    SElemType data[StackSize];
    int top1,top2;
}DuSqStack;

共享存储空间的顺序栈的基本操作:

1、初始化操作InitStack

void InitStack(DuSqStack &S){
    S.top1=0,S.top2=StackSize-1;
}

2、判断一个共享存储空间的顺序栈是否为空栈StackEmpty

bool StackEmpty(DuSqStack S){
    return S.top1==0&&S.top2==StackSize-1?true:false;
}

3、求共享存储空间的顺序栈的长度StackLength

int StackLength(DuSqStack S,int i){
    int flag=i%2;
    return flag?S.top1:StackSize-S.top2-1;
}

4、入栈操作Push()

void Push(DuSqStack &S,int i,SElemType e){
    if(S.top1-S.top2==1){
        printf("error !!! Stack is full\n");
        return ;
    }
    S.data[(i&1?S.top1++:S.top2--)]=e;
}

5、出栈操作Pop

void Pop(DuSqStack &S,int i,SElemType &e){
    if(i==1){
        if(S.top1==0){
            printf("error !!! Stack1 is empty\n");
            return ;
        }
        e=S.data[--S.top1];
    }else{
        if(S.top2==StackSize-1){
            printf("error !!! Stack2 is empty\n");
        }
        e=S.data[++S.top2];
    }
}

6、取栈顶元素操作GetTop()

void GetTop(DuSqStack S,int i,SElemType &e){
    if(i==1){
        if(S.top1==0){
            printf("error !!! Stack1 is empty\n");
            return ;
        }
        e=S.data[S.top1-1];
    }else{
        if(S.top2==StackSize-1){
            printf("error !!! Stack2 is empty\n");
        }
        e=S.data[S.top2+1];
    }
}

完整的调试代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define StackSize 100
#define SElemType int
using namespace std;
typedef struct{
    SElemType data[StackSize];
    int top1,top2;
}DuSqStack;
void InitStack(DuSqStack &S){
    S.top1=0,S.top2=StackSize-1;
}
bool StackEmpty(DuSqStack S){
    return S.top1==0&&S.top2==StackSize-1?true:false;
}
int StackLength(DuSqStack S,int i){
    int flag=i%2;
    return flag?S.top1:StackSize-S.top2-1;
}
void Push(DuSqStack &S,int i,SElemType e){
    if(S.top1-S.top2==1){
        printf("error !!! Stack is full\n");
        return ;
    }
    S.data[(i&1?S.top1++:S.top2--)]=e;
}
void Pop(DuSqStack &S,int i,SElemType &e){
    if(i==1){
        if(S.top1==0){
            printf("error !!! Stack1 is empty\n");
            return ;
        }
        e=S.data[--S.top1];
    }else{
        if(S.top2==StackSize-1){
            printf("error !!! Stack2 is empty\n");
        }
        e=S.data[++S.top2];
    }
}
void GetTop(DuSqStack S,int i,SElemType &e){
    if(i==1){
        if(S.top1==0){
            printf("error !!! Stack1 is empty\n");
            return ;
        }
        e=S.data[S.top1-1];
    }else{
        if(S.top2==StackSize-1){
            printf("error !!! Stack2 is empty\n");
        }
        e=S.data[S.top2+1];
    }
}
void Array_DuSqStack(DuSqStack &St, SElemType a[],SElemType b[],int La=0,int Lb=0){
    InitStack(St);
    for(int i=0;i<La;i++){
        St.data[St.top1++]=a[i];
    }
    for(int i=0;i<Lb;i++){
        St.data[St.top2--]=b[i];
    }
}
void OutputDuSqStack(DuSqStack St){
    if(St.top1==0){
        printf("Stack 1 is empty !!!\n");
    }else{
        printf("Stack 1 :\n");
        for(int i=St.top1-1;i>=0;i--){
            printf("%d%c",St.data[i],i?' ':'\n');
        }
    }
    if(St.top2==StackSize-1){
        printf("Stack 2 is empty !!!\n");
    }else{
        printf("Stack 2 :\n");
        for(int i=St.top2+1;i<StackSize;i++){
            printf("%d%c",St.data[i],i==StackSize-1?'\n':' ');
        }
    }
}
int main()
{
    DuSqStack St;
    //int a[]={1,2,3,4,5},b[]={12,24,26};
    //Array_DuSqStack(St,a,b,5,3);
    //OutputDuSqStack(St);
    int a[]={1,3,5,6,7};
    InitStack(St);
    if(StackEmpty(St)){
        int e;
        for(int i=0;i<5;i++){
            Push(St,i%2,a[i]);
            GetTop(St,i%2,e);
            printf("%d%c",e,i==4?'\n':' ');
        }
        printf("%d %d\n",StackLength(St,1),StackLength(St,2));
        for(int i=0;i<5;i++){
            Pop(St,i%2,e);
            printf("%d%c",e,i==4?'\n':' ');
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Z_sea/article/details/85121092