## 页首HTML代码 ```html

两个栈实现队

数据结构

线性表练习题

image

一些总结整理

/****************************************************************************
 *
 * file name: 2024-04-26_SeqQueuedemo.c
 * author   : tongyaqi1110@163.com
 * date     : 2024-04-28
 * function : 实现利用栈s1和s2实现队列
 * note     : None
 * CopyRight (c)   2024  tongyaqi1110@163.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;


/****************************************************************************
 *
 * function name     :EnQueue
 * function          : 入队
 * parameter         : 
 *                      @s1
 *                      @s2
 *                      @x
 *
 * Return results    : none
 * note              : None
 * author            : tongyaqi1110@163.com
 * date              : 2024-04-26
 * version           : V1.0
 * revision history  : None
 *
 ****************************************************************************/
//入队
int EnQueue(SeqStack &s1,SeqStack &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;
	}
}

/****************************************************************************
 *
 * function name     : isQueueEmpty
 * function          : 判空
 * parameter         : @s1
 *                     @s2
 *
 * Return results    : 返回头结点地址
 * note              : None
 * author            : tongyaqi1110@163.com
 * date              : 2024-04-28
 * version           : V1.0
 * revision history  : None
 *
 ****************************************************************************/
//判断队列为空
int isQueueEmpty(s1,s2)
{
	if (isEmpty(s1) && isEmpty(s2))
	{
		return 1;
	}
	else
		return 0;
}

/****************************************************************************
 *
 * function name     :DeQueue
 * function          : 出队
 * parameter         : 
 *                      @s1
 *                      @s2
 *                      @x
 *
 * Return results    : none
 * note              : None
 * author            : tongyaqi1110@163.com
 * date              : 2024-04-28
 * version           : V1.0
 * revision history  : None
 *
 ****************************************************************************/
//出队

int DeQueue(s1,s2,&x)
{
	int temp; //为了存储出栈的元素

	//1.判断队列是否为空
	if (isQueueEmpty(s1,s2))
	{
		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-04-28 23:56  一面小镜子  阅读(21)  评论(0)    收藏  举报