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

问题描述:

考虑用两个栈实现队列这样的特殊结构


问题分析:

我们靠两个栈实现队列,肯定是一个用来存放入队的数据,一个用来出栈,在这里我们主要关注这个样几个问题:

  • 什么时候队列可以出队?
  • 什么时候队列为空?
  • 什么时候队列可以入队?
  • 什么时候队列为满?
    弄明白这几个问题就可以很容易的解决问题了

下面给出答案(这里不是纯代码描述,说明问题即可):
在这里插入图片描述


实现代码:

在这里给出栈的定义与实现,以及用两个栈实现队列的具体代码:

//栈的结构定义与方法声明

#define INITSIZE 5

typedef int ElemType;

typedef struct Stack
{
	ElemType data[INITSIZE];
	int top;

}Stack;

void InitStack(Stack *st,  int init_size);


bool  Empty(Stack *st);

bool  Push(Stack *st, ElemType value);

bool  Pop(Stack *st);

bool Top(Stack *st, ElemType *reval);

void DestroyStack(Stack *st);
//栈的方法实现

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

void InitStack(Stack *st,  int init_size)
{
	if(st==NULL) exit(0);

	st->top =0;
}

bool  Empty(Stack *st)
{
	if(st==NULL) exit(0);

	return st->top ==0;
}

bool Full(Stack *st)
{
	if(st==NULL) exit(0);

	return st->top ==INITSIZE;
}
bool  Push(Stack *st, ElemType value)
{
	if(st==NULL) exit(0);
	if(Full(st)) return false;

	st->data [st->top ++]=value;
	return true;
}

bool  Pop(Stack *st)
{
	if(st==NULL) exit(0);

	if(Empty(st)) return false;

	st->top --;
	return true;
}

bool Top(Stack *st, ElemType *reval)
{
	if(st==NULL) exit(0);

	*reval=st->data[st->top];
	return true;
}

void DestroyStack(Stack *st)
{
	if(st==NULL) exit(0);

	while(!Empty(st))
	{
		Pop(st);
	}
}

//队列的结构定义与方法实现

typedef struct Queue
{
	Stack enter;
	Stack out;

}que;

void Init_que(que *q)
{
	if(q==NULL) exit(0);
	InitStack(&(q->enter),10); //初始化一个栈用来保存入队元素
	InitStack(&(q->out),10);  //初始化一个栈用来队列出队
}

bool Empty_que(que *q)
{
	if(q==NULL) exit(0);

	if(Empty(&q->enter) && Empty(&q->out))
		return true;
	else
		return false;
}

bool Full_que(que *q)
{
	if(q==NULL) exit(0);

	if(!Empty(&q->out) && Full(&q->enter))
		return true;
	else 
		return false;
}

bool Push_que(que *q,ElemType value)
{
	if(q==NULL) exit(0);

	if(Full_que(q)) return false;// 队满

	if(Full(&q->enter))
	{
		ElemType val;
		while(!Empty(&q->enter))
		{
			Top(&q->enter,&val);
			Pop(&q->enter);
			Push(&q->out,val);
		}
	}
	Push(&q->enter,value);
	return true;
}

static bool Pop_que(que *q)
{
	if(q==NULL) exit(0);

	if(Empty_que(q)) return false;//队空

	if(Empty(&q->out))
	{
		ElemType val;
		while(!Empty(&q->enter))
		{
			Top(&q->enter,&val);
			Pop(&q->enter);
			Push(&q->out,val);
		}
	}
	Pop(&q->out);
	return true;
}

static bool Top_que(que *q,ElemType *revalue)
{
	if(q==NULL) exit(0);

	if(Empty_que(q)) return false;

	if(Empty(&q->out))
	{
		ElemType val;
		while(!Empty(&q->enter))
		{
			Top(&q->enter,&val);
			Pop(&q->enter);
			Push(&q->out,val);
		}
	}
	Top(&q->out,revalue);
	return true;
}

static void Destory_que(que *q)
{
	if(q==NULL) exit(0);

	DestroyStack(&q->enter);
	DestroyStack(&q->out);
}
  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言实现两个实现一个队列的程序: ```c #include <stdio.h> #include <stdlib.h> typedef struct stack { int top; int capacity; int* array; } Stack; typedef struct queue { Stack* stack1; Stack* stack2; } Queue; Stack* createStack(int capacity) { Stack* stack = (Stack*)malloc(sizeof(Stack)); stack->capacity = capacity; stack->top = -1; stack->array = (int*)malloc(stack->capacity * sizeof(int)); return stack; } int isStackEmpty(Stack* stack) { return stack->top == -1; } int isStackFull(Stack* stack) { return stack->top == stack->capacity - 1; } void push(Stack* stack, int item) { if (isStackFull(stack)) { printf("Stack is full.\n"); return; } stack->array[++stack->top] = item; } int pop(Stack* stack) { if (isStackEmpty(stack)) { printf("Stack is empty.\n"); return -1; } return stack->array[stack->top--]; } void enqueue(Queue* queue, int item) { push(queue->stack1, item); } int dequeue(Queue* queue) { if (isStackEmpty(queue->stack1) && isStackEmpty(queue->stack2)) { printf("Queue is empty.\n"); return -1; } if (isStackEmpty(queue->stack2)) { while (!isStackEmpty(queue->stack1)) { int item = pop(queue->stack1); push(queue->stack2, item); } } return pop(queue->stack2); } int main() { Queue* queue = (Queue*)malloc(sizeof(Queue)); queue->stack1 = createStack(100); queue->stack2 = createStack(100); enqueue(queue, 1); enqueue(queue, 2); enqueue(queue, 3); printf("%d dequeued from queue.\n", dequeue(queue)); printf("%d dequeued from queue.\n", dequeue(queue)); printf("%d dequeued from queue.\n", dequeue(queue)); free(queue->stack1->array); free(queue->stack1); free(queue->stack2->array); free(queue->stack2); free(queue); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值