【c语言】两个栈实现一个队列

博客介绍了用两个栈实现队列的方法。核心思想是模拟队列先进先出结构,设input和output两个栈,input负责插入数据,出队列时input元素按顺序进入output,只要output非空,出队操作通过其出栈实现,为空则从input导入,两栈皆空则队列空,还提及代码模拟文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两个栈实现一个队列

在这里插入图片描述
核心思想:模拟出队列先进先出的数据结构
假设有两个栈input和output,input模拟栈的数据插入,当需要模拟出队列操作时,input栈中的A,B,C,D会按照D,C,B,A的顺序进入栈output。 只要output栈不为空,出队列操作就可以通过output的出栈操作来实现。
若output栈为空,则继续从input栈导入数据。
若两个栈都为空,即整个队列为空。

代码模拟:
Queueby_two_stack.h

#pragma once

typedef int DataType;

typedef struct Stack
{
	DataType* Data;
	//有效元素个数
	int size;
	//栈的容量个数
	int capacity;
}Stack;

typedef struct Queue
{
	Stack input;
	Stack output;
	//有效元素个数
	int size;
}Queue;

//初始化函数
void QueueInit(Queue *queue);
//销毁函数
void QueueDestory(Queue *queue);
//入栈函数
void QueuePush(Queue *queue, DataType value);
//出栈函数
void QueuePop(Queue *queue);
//取栈顶元素函数
DataType QueueFront(Queue *queue);

Queueby_two_stack.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Queueby_two_stack.h"

void StackInit(Stack* stack,DataType capacity)
{
	if (stack == NULL)
	{
		assert(0);
		return -1;
	}
	stack->capacity = capacity;
	stack->size = 0;
	stack->Data = (DataType*)malloc(sizeof(DataType)* stack->capacity);
}

//栈销毁函数
void StackDestory(Stack* stack)
{
	if(stack == NULL)
	{
		assert(0);
		return -1;
	}
	free(stack->Data);
	stack->Data = NULL;
	stack->capacity = 0;
	stack->size = 0;
}

//入栈操作
void StackPush(Stack* stack,DataType value)
{
	if (stack == NULL)
	{
		assert(0);
		return -1;
	}
	if (StackFull(stack))
	{
		int new_capacity = stack->capacity * 2;
		DataType *newDate = (DataType *)malloc(sizeof(DataType) * new_capacity);
		if (newDate == NULL)
		{
			assert(0);
		}
		for (int i = 0; i < stack->size; ++i)
		{
			newDate[i] = stack->Data[i];
		}
		stack->Data = newDate;
		stack->capacity = new_capacity;
	}
	else
	{
		stack->Data[stack->size + 1] = value;
		stack->size++;
	}
}

int StackFull(Stack* stack)
{
	if(stack == NULL)
	{
		assert(0);
		return -1;
	}
	if (stack->size == stack->capacity)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int StackEmpty(Stack* stack)
{
	if (stack == NULL)
	{
		assert(0);
		return -1;
	}
	if (stack->size == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//出栈操作
int StackPop(Stack* stack)
{
	if (stack == NULL)
	{
		assert(0);
		return -1;
	}
	if(stack->size == 0)
	{
		return ;
	}
	stack->size--;
}

//取栈顶元素
DataType StackGetTop(Stack* stack)
{
	if (stack == NULL)
	{
		assert(0);
		return -1;
	}
	if (stack->size == 0)
	{
		return ; 
	}
	else
	{
		return stack->Data[stack->size-1];
	}
}

//队列初始化函数
void QueueInit(Queue* queue,DataType capacity)
{
	if (queue == NULL)
	{
		assert(0);
	}
	StackInit(&queue->input,capacity);
	StackInit(&queue->output, capacity);
	queue->size = 0;
}

//队列销毁函数
void QueueDestory(Queue *queue)
{
	if (queue == NULL)
	{
		assert(0);
	}
	StackDestory(&queue->input);
	StackDestory(&queue->output);
	queue->size = 0;
}

//入队列函数
void QueuePush(Queue *queue, DataType value)
{
	if (queue == NULL)
	{
		assert(0);
		return -1;
	}
	StackPush(&queue->input, value);
	queue->size++;
}

//出队列函数
void QueuePop(Queue* queue)
{
	if (queue == NULL)
	{
		assert(0);
		return -1;
	}
	if (StackEmpty(&queue->output))
	{
		if (StackEmpty(&queue->input))
		{
			return ;
		}
		else
		{
			int newsize = queue->input.size;
			for(int i = 0;i< newsize;++i)
			{
				DataType tmp = StackGetTop(&queue->input);
				StackPop(&queue->input);
				StackPush(&queue->output,tmp);
			}
		}
	}
	StackPop(&queue->output);
	queue->size--;

}

//取队首元素函数
DataType QueueFront(Queue* queue)
{
	if (queue == NULL)
	{
		assert(0);
		return -1;
	}
	if (StackEmpty(&queue->output))
	{
		if (StackEmpty(&queue->input))
		{
			return ;
		}
		else
		{
			int newsize = queue->input.size;
			for (int i = 0; i <newsize ; ++i)
			{
				DataType tmp = StackGetTop(&queue->input);
				StackPop(&queue->input);
				StackPush(&queue->output, tmp);
			}
		}
	}
	return StackGetTop(&queue->output);
}
int main()
{
	Queue queue;
	QueueInit(&queue,10);
	//入队列函数测试
	QueuePush(&queue, 1);
	QueuePush(&queue, 2);
	QueuePush(&queue, 3);
	QueuePush(&queue, 4);
	QueuePop(&queue);
	DataType tmp = QueueFront(&queue);
	printf("%d \n", tmp);
	system("pause");
	return 0;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值