利用两个栈实现队列结构——“先进先出”

请利用两个找sl 和s2 来模拟一个队列, 假设栈中元素为int 型, 栈中元素最多为maxSize 。已知栈的3 个运算定义如下。

push(ST,x) : 元素x 入ST 栈。
pop(ST,&x): ST 栈顶元素出栈, 赋给变量x 。
isEmpty(ST) : 判断ST 栈是否为空。
如何利用栈的运算来实现该队列的3 个运算: enQueue (元素入队列)、deQu eue (元素出队列)、
isQueueEmpty (判断队列是否为空, 空返回1' 不空返回0 ) 。要求:
( 1 ) 给出基本设计思想。
( 2 ) 根据设计思想, 采用C 或C++语言描述算法, 关键之处给出注释。

/**
 * file name:StackQueue.c
 * author   : liaojx2016@126.com
 * date     : 2024-04-28
 * function : Implementing queues using two stacks s1 and s2
 * note     : None
 * CopyRight (c)   2024  liaojx2016@126.com  Right Reseverd
 *
**/
//栈的思想是“后进先出”,队列的思想是“先进先出”.
//把栈s1作为入队缓存,把栈s2作为出队缓存

#include <stdio.h>
#include <stdlib.h>
// #include <cstddef>
#include <stdbool.h>

#define MAXSIZE 20
typedef struct 
{
  int data [MAXSIZE];//存放栈中元素
  int top;//栈顶指针
}SeqStack;


/**
 *
 * func name      : EnQueue
 * function       : Enqueue
 * parameter      : 
 *                      @s1:stack pointer
 *                      @s2:stack pointer
 *                      @x :enqueue value
 *
 * Return val      : none
 * note            : None
 * author          : liaojx2016@126.com
 * date            : 2024-04-26
 * version         : V1.0
 * modify history  : None
 *
**/
//入队
int EnQueue(s1, s2, int x)
{
	int temp; //用于存储出栈的元素的值

	//1.判断栈s1是否已满
	if (s1->top + 1 >= MAXSIZE)
	{
		//说明栈s1已满,再s2满或者空
		if ( isEmpty(s2) )
		{
      return 0;//如果是s1满而s2不空就无法入栈
    }
			//此时栈s2为空,所以需要把栈s1的元素依次出栈到s2中
      else if (isEmpty(s2)){
			while( ! isEmpty(s1) )
			{
				pop(s1,&temp); //把出栈元素暂时存储在temp中
				push(s2,temp); //把变量temp中的元素入栈到s2
			}

			push(s1,x); //将s1中所有元素都压入s2中,又可以把新元素x入栈到s1
			return 1;
		}

	else
	{
		//此时栈s1未满,所以可以把元素x入栈到s1中
		push(s1,x); 
    return 1;
	}
}

/**
 *
 * func name      : isQueueEmpty
 * function       : Judge whether queue is empty
 * parameter      : 
 *                      @s1:stack pointer
 *                      @s2:stack pointer
 * Return val      : Judge result (1 means the queue is empty,0 means the queue is not empty)
 * note            : None
 * author          : liaojx2016@126.com
 * date            : 2024-04-26
 * version         : V1.0
 * modify history  : None
 *
**/
//判断队列为空
int isQueueEmpty(s1,s2)
{
	if (isEmpty(s1) && isEmpty(s2))
	{
		return 1;
	}
	else
		return 0;
}

/**
 *
 * func name      : DeQueue
 * function       : Dequeue
 * parameter      : 
 *                      @s1:stack pointer
 *                      @s2:stack pointer
 * 						@x :dequeue value
 * Return val      : Judge result (1 means the dequeue success, 0 means the dequeue fail)
 * note            : None
 * author          : liaojx2016@126.com
 * date            : 2024-04-26
 * version         : V1.0
 * modify history  : None
 *
**/
//出队
int DeQueue(s1,s2,&x)
{
	int temp; //为了存储出栈的元素

	//1.判断队列是否为空
	if (isQueueEmpty(s1,s2))
	{
		printf("the queue is empty,dequeue failed\n");
		return 0;
	}
	else
	{
		//说明队列不空,判断栈s2空或栈s2不空
		if ( !isEmpty(s2) )
		{
			//说明栈s2不空,则直接把元素出栈
			pop(s2,&x);
		}
		else
		{
			//说明栈s2为空,此时需要把栈s1的元素依次出栈到s2中
			while( ! isEmpty(s1) )
			{
				pop(s1,&temp); //把出栈元素暂时存储在temp中
				push(s2,temp); //把变量temp中的元素入栈到s2
			}

			pop(s2,&x);
		}
	}

	return 1;
}
posted @ 2024-05-04 22:22  沉舟道人  阅读(17)  评论(0编辑  收藏  举报