【数据结构与算法C】利用两个栈S1S2模拟一个队列,用栈的基本操作实线EnQueue,DeQueue,QueueEmpty

【数据结构与算法C】利用两个栈S1S2模拟一个队列,用栈的基本操作实线EnQueue,DeQueue,QueueEmpty

入队列

Created with Raphaël 2.1.2 开始 S2为空 出S1入S2 入S1 yes no

出队列

Created with Raphaël 2.1.2 开始 S2不为空 出S2 结束 出S1入S2 S2不为空 出S2 yes no yes
/*
 * main.c
 *
 *  Created on: Apr 19, 2015
 *      Author: xubin
 *      利用两个栈S1S2模拟一个队列,用栈的基本操作实线EnQueue,DeQueue,QueueEmpty
 */

#include<stdio.h>
#include<stdlib.h>

#define STACKINITSIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
typedef int bool;
//顺序栈
typedef struct{
    char *base;
    char *top;
    int stacksize;
}SqStack;

//函数声明
int StackLength(SqStack S);
//若栈S为空,则返回TRUE,否则返回FALSE
bool StackEmpty(SqStack S);
// 销毁栈S,S不在存在
bool DestroyStack(SqStack *S);


//构造一个空栈
bool InitStack(SqStack *S){
    S->base = (char *)malloc(STACKINITSIZE * sizeof(SqStack));
    if(S->base == NULL){
        return FALSE;
    }
    S->top = S->base;
    S->stacksize = STACKINITSIZE;
    return TRUE;
}
// 若栈不为空,则用e返回栈顶元素,并返回true,否则返回false
bool GetTop(SqStack S,char *e){
    if(S.top == S.base){
        return FALSE;
    }
    *e = *(S.top - 1);
    return TRUE;
}

//插入元素e为新的栈顶元素
bool Push(SqStack *S,char e){
    if(S->top - S->base >=S->stacksize){
        //栈满,追加存储空间
        S->base = (char *)realloc(S->base,(S->stacksize + STACKINCREMENT) * sizeof(SqStack));
        if(S->base == NULL){
            return FALSE;
        }
        S->top = S->base + S->stacksize;
    }

    *S->top ++ = e;
    return TRUE;
}

// 若栈不为空,则删除S的栈顶元素,用e返回其值,并返回TRUE,否则返回FALSE
bool Pop(SqStack *S,char *e){
    if(S->top == S->base){
        return FALSE;
    }
    *e = * -- S->top;   // 传递指针指向的内容而不是传递指针
    //e= --S->top;  // 错误的传递方法
    return TRUE;
}


//**********************************************************************
//**********************************************************************
// 用栈的基本操作模拟队列
// 利用两个栈S1S2模拟一个队列,用栈的基本操作实线EnQueue,DeQueue,QueueEmpty
//
bool EnQueue(SqStack *S1,SqStack *S2,char e){
    char temp;
    //如果S2为空,出S1入S2
    if(StackEmpty(*S2) == TRUE){
        while(StackEmpty(*S1) == FALSE){
            Pop(S1,&temp);
            Push(S2,temp);
        }
    }
    Push(S1,e);
    return TRUE;
}

bool DeQueue(SqStack *S1,SqStack *S2,char *e){
    char temp;
    // 判断是否为空
    if(StackEmpty(*S2) == TRUE && StackEmpty(*S1) == TRUE){
        return FALSE;
    }
    if(StackEmpty(*S2) == FALSE){
        Pop(S2,e);
    }
    else{
        while(StackEmpty(*S1) == FALSE){
            Pop(S1,&temp);
            Push(S2,temp);
        }
        Pop(S2,e);
    }
    return TRUE;
}

bool QueueEmpty(SqStack S1,SqStack S2){
    if(StackEmpty(S1) == TRUE && StackEmpty(S2) == TRUE){
        return TRUE;
    }else
        return FALSE;
}
//*********************************************************************
//*********************************************************************


int main(){
    SqStack S1,S2;
    char ch;
    char temp;
    printf("顺序栈的基本操作\n");
    if(InitStack(&S1) == 1 && InitStack(&S2) == 1){
        printf("栈初始化成功\n");
    }
    else{
        printf("初始化失败\n");
        return 1;
    }
    printf("现在输入一串字符串,压入队列:");
    ch = getchar();
    while(ch != '\n' && EnQueue(&S1,&S2,ch)){
        ch = getchar();
    }

    while(QueueEmpty(S1,S2) == FALSE){
        DeQueue(&S1,&S2,&temp);
        printf("%c\t",temp);
    }


    return 0;
}

//返回栈S的元素的个数,即栈的长度
int StackLength(SqStack S){
    //int num = 0;
    return (S.top - S.base);
}

// 销毁栈S,S不在存在
bool DestroyStack(SqStack *S){
    if(S->base == NULL)
        return FALSE;
    free(S->base);
    return TRUE;
}

//若栈S为空,则返回TRUE,否则返回FALSE
bool StackEmpty(SqStack S){
    if(S.top == S.base){
        return TRUE;
    }
    else{
        return FALSE;
    }
}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值