顺序栈共享存储区与双栈模拟队列代码实现

顺序栈

top0与top1
top0入栈先自增1,然后放入元素
top1 出栈先自减1,然后放入元素

top0出栈先取出元素再自减,
top1 出栈 先取出元素再自加

#include<stdio.h>
typedef struct {
    
    
    int elem[10];
    int top[2];
}SqStack;
int push(SqStack &st,int stNo,int x){
    
    
    if(st.top[0]+1<st.top[1]){
    
    
        if(stNo==1){
    
    
            st.top[0]++;
            st.elem[st.top[0]]=x;
            return 1;
        }else if(stNo==2){
    
    

            st.top[1]--;
            st.elem[st.top[1]] = x;
            return 1;
        }
    }else
        return 0;

}
int pop(SqStack &st,int stNo,int &x){
    
    
    if(stNo==1){
    
    
        if(st.top[0]==0){
    
    
            return 0;
        }else{
    
    
            x= st.elem[st.top[0]];
            st.top[0]--;
            return 1;
        }

    }else if(stNo==2){
    
    
        if(st.top[1]==10-1){
    
    
            return 0;
        }else{
    
    
            x = st.elem[st.top[1]] ;
            st.top[1]++;
            return 1;
        }
    }
    else return 0;
}

int main(){
    
    
    //最左边是第一个栈的栈底,最右边是第二个栈的栈底.
    //左边栈入栈 先自增1,放入元素 出栈:先取出元素然后 -1
    //右边栈入栈 先减1 放入元素 出栈 先取出元素然后+1
    SqStack st;

    st.top[0] = 0;
    st.top[1] = 10-1;
    push(st,1,2);
    int x;
    pop(st,1,x);
    printf("\ntop0:%d",x);
    push(st,2,3);
    pop(st,2,x);
    printf("\ntop1:%d",x);

    return 0;

}

栈模拟队列

//双栈模拟队列 ,入队 相当于s1入栈
//出队时s1,退出压入s2,
//队空才为空


#include<stdio.h>
#define maxsize 10
typedef struct {
    
    
    int elem[maxsize];
    int top;
}SqStack;

int push(SqStack &st,int x){
    
    
    if(st.top == maxsize-1){
    
    
        return 0;
    }
    ++(st.top);
    st.elem[st.top] = x;
    return 1;
}
int pop(SqStack &st,int &x){
    
    
    if(st.top == -1){
    
    
        return 0;
    }
    x = st.elem[st.top] ;
    --(st.top);

    return 1;
}
bool isEmpty(SqStack st){
    
    
    if(st.top == -1){
    
    
        return true;

    }else return false;
}

int enQueue(SqStack &s1,SqStack &s2,int x){
    
    
    int y;
    if(s1.top == maxsize - 1){
    
    
        if(!isEmpty(s2))
            return 0;
        else{
    
    
            while(!isEmpty(s1)){
    
    
                pop(s1,y);
                push(s2,y);
            }
            push(s1,x);
            return 1;
        }
    }else{
    
    
        push(s1,x);
        return 1;
    }
}
int deQueue(SqStack &s1,SqStack &s2,int &x){
    
    
    int y;
    if(!isEmpty(s2)){
    
    
        pop(s2,x);
        return 1;
    }else{
    
    
        if(isEmpty(s1)){
    
    
            return 0;
        }else{
    
    
            while(!isEmpty(s1)){
    
    
                pop(s1,y);
                push(s2,y);
            }
            pop(s2,x);

            return 1;
        }

    }
}

int isQueueEmpty(SqStack s1,SqStack s2){
    
    
    if(isEmpty(s1)&& isEmpty(s2)){
    
    
        return true;
    }else return false;
}
int main(){
    
    
    SqStack st1;
    SqStack st2;
    st1.top = -1;
    st2.top = -1;
    //双栈模拟队列 ,入队 相当于s1入栈
    //出队时s1,退出压入s2,
    //队空才为空
    int x = 3;
    enQueue(st1,st2,x);
    printf("isEmpty:%d\n", isQueueEmpty(st1,st2));
    int y;
    deQueue(st1,st2,y);
    printf("deQueue:%d", y);
    return 0 ;

    return 0;

}

猜你喜欢

转载自blog.csdn.net/m0_37149062/article/details/125659514