队列的特点是先进先出,也就是说,存进队列的顺序和从队列中取出的顺序是一样的
栈的特点是后进先出,也就是说,存进栈内的顺序和从栈中去出的数据是相反的,入过用两个栈,第一个栈将数据反向,第二个栈又将第一个栈的数据反向,最后得到的序列与从队列中得到的序列一致,示意图如下:


代码如下:
头文件:QueueByTwoStack.h
#pragma once
#include<stdio.h>
#include"SeqStack.h"
typedef struct Queue{
SeqStack stack_input; //负责接收入队列数据
SeqStack stack_output; //负责出队列
}Queue;
void QueueInit(Queue* queue); //队列的初始化
void QueueDestroy(Queue* queue); //队列的销毁
void QueueIn(Queue* queue, StackType value); //入队列
void QueueOut(Queue* queue); //出队列
int QueueFront(Queue* queue, StackType* value); //取队首元素
头文件实现:QueueByTwoStack.c
#include"QueueByTwoStack.h"
void QueueInit(Queue* queue) {
if(queue == NULL) {
return;
}
SeqStackInit(&queue->stack_input);
SeqStackInit(&queue->stack_output);
return;
}
void QueueDestroy(Queue* queue) {
if(queue == NULL) {
return;
}
SeqStackDestroy(&queue->stack_input);
SeqStackDestroy(&queue->stack_output);
return;
}
void QueueIn(Queue* queue, StackType value) {
if(queue == NULL) {
return;
}
StackType box;
while(queue->stack_output.size > 0) {
SeqStackTop(&queue->stack_output, &box);
SeqStackPop(&queue->stack_output);
SeqStackPush(&queue->stack_input,box);
}
SeqStackPush(&queue->stack_input,value);
return;
}
void QueueOut(Queue* queue) {
if(queue == NULL) {
return;
}
while(queue->stack_input.size != 0) {
StackType box;
SeqStackTop(&queue->stack_input,&box);
SeqStackPop(&queue->stack_input);
SeqStackPush(&queue->stack_output, box);
}
SeqStackPop(&queue->stack_output);
return;
}
int QueueFront(Queue* queue, StackType* value) {
if(queue == NULL || value == NULL) {
return 0;
}
StackType box;
while(queue->stack_input.size != 0) {
SeqStackTop(&queue->stack_input,&box);
SeqStackPop(&queue->stack_input);
SeqStackPush(&queue->stack_output, box);
}
if(queue->stack_output.size == 0) {
return 0;
}
SeqStackTop(&queue->stack_output,value);
return 1;
}
//////////////////////////////////////////////////////
//以下为测试代码
//////////////////////////////////////////////////////
void TestQueue() {
Queue queue;
QueueInit(&queue);
QueueIn(&queue, 'a');
QueueIn(&queue, 'b');
QueueIn(&queue, 'c');
QueueIn(&queue, 'd');
int ret;
StackType value;
ret = QueueFront(&queue, &value);
printf("ret expected 1, actual %d\n", ret);
printf("value expected a, actual %c\n", value);
QueueOut(&queue);
ret = QueueFront(&queue, &value);
printf("ret expected 1, actual %d\n", ret);
printf("value expected b, actual %c\n", value);
QueueOut(&queue);
ret = QueueFront(&queue, &value);
printf("ret expected 1, actual %d\n", ret);
printf("value expected c, actual %c\n", value);
QueueOut(&queue);
ret = QueueFront(&queue, &value);
printf("ret expected 1, actual %d\n", ret);
printf("value expected d, actual %c\n", value);
QueueOut(&queue);
ret = QueueFront(&queue, &value);
printf("ret expected 0, actual %d\n", ret);
QueueOut(&queue);
QueueDestroy(&queue);
}
int main()
{
TestQueue();
return 0;
}
注意:
在本次实验中使用以前写过的栈SeqStack.h和SeqStack.c
队列初始化:初始化两个栈即可
队列销毁:销毁两个栈即可
入队列:要保证stack_output中没有元素才能向stack_input中入栈,如果stack_output中有元素,那么全部转移到stack_input中
出队列:要保证stack_input中没有元素才能从stack_output中出栈,如果stack_input中有元素,那么全部转移到stack_output中
去队首元素:要保证stack_input中没有元素才能取stack_output的栈顶元素,如果stack_input中有元素,那么全部转移到stack_output中
上面所说的转移,就是从指定的栈中出栈,然后直接入另一个栈

1万+

被折叠的 条评论
为什么被折叠?



