用栈实现队列

用栈实现队列:题目链接
栈:链接
队列:链接
在这里插入图片描述

思路

用两个栈实现队列:
一个栈负责入队列,一个栈负责实现出队列。出队列时,如果负责出队列的栈为空,则将另一个栈的数据倒入其中;如若有数据则直接出即可

代码实现

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;//top所标识的是最后一个数据的下一个位置
	int capacity;
}Stack;

void StackInit(Stack* ps);
void StackDestroy(Stack* ps);
void StackPush(Stack* ps, STDataType x);
void StackPop(Stack* ps);
void Stackp(Stack* ps);
STDataType StackTop(Stack* ps);
bool StackEmpty(Stack* ps);
int StackSize(Stack* ps);

void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int  newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//如果栈中没有数据,那么给它四个数据的空间;如果有,那么在原有的基础上扩大二倍
		STDataType* temp = realloc(ps->a, sizeof(STDataType) * newCapacity);//用temp指向扩容后的空间,回头再把它赋给ps->a
		if (temp == NULL)//realloc可能会失败,失败会返回空指针
		{
			printf("realloc fail\n");
			exit(-1); 
		}
		ps->a = temp;
		ps->capacity = newCapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

bool StackEmpty(Stack* ps)
{
	assert(ps);
	//if (ps->top > 0)
	//{
	//	return false;
	//}
	//else
	//{
	//	return true;
	//}
	return ps->top == 0;
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}

typedef struct {
    Stack pushst;
    Stack popst;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue *obj = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&obj->pushst);
    StackInit(&obj->popst);
    return obj;
}

void myQueuePush(MyQueue* obj, int x) {
    StackPush(&obj->pushst, x);
}

int myQueuePop(MyQueue* obj) {
    if (StackEmpty(&obj->popst))//如果pop栈为空,则把push栈的数据倒过去
    {
        while (!StackEmpty(&obj->pushst))
        {
            StackPush(&obj->popst, StackTop(&obj->pushst));
            StackPop(&obj->pushst);
        }
    }
    int front = StackTop(&obj->popst);
    StackPop(&obj->popst);
    return front;
}

int myQueuePeek(MyQueue* obj) {
    if (StackEmpty(&obj->popst))//如果pop栈为空,则把push栈的数据倒过去
    {
        while (!StackEmpty(&obj->pushst))
        {
            StackPush(&obj->popst, StackTop(&obj->pushst));
            StackPop(&obj->pushst);
        }
    }    
    return StackTop(&obj->popst);
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->pushst) && StackEmpty(&obj->popst);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->pushst);
    StackDestroy(&obj->popst);
    free(obj);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用栈实现队列的C语言代码如下: ```c #include <stdio.h> #include <stdlib.h> // 定义栈结构 typedef struct { int* data; // 存储数据的数组 int top; // 栈顶指针 int maxSize; // 栈的最大容量 } Stack; // 初始化栈 void initStack(Stack* stack, int maxSize) { stack->data = (int*)malloc(sizeof(int) * maxSize); stack->top = -1; stack->maxSize = maxSize; } // 判断栈是否为空 int isEmpty(Stack* stack) { return stack->top == -1; } // 判断栈是否已满 int isFull(Stack* stack) { return stack->top == stack->maxSize - 1; } // 入栈操作 void push(Stack* stack, int value) { if (isFull(stack)) { printf("Stack is full!\n"); return; } stack->data[++stack->top] = value; } // 出栈操作 int pop(Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty!\n"); return -1; } return stack->data[stack->top--]; } // 使用栈实现队列 typedef struct { Stack* inStack; // 入队栈 Stack* outStack; // 出队栈 } MyQueue; // 初始化队列 void initQueue(MyQueue* queue, int maxSize) { queue->inStack = (Stack*)malloc(sizeof(Stack)); queue->outStack = (Stack*)malloc(sizeof(Stack)); initStack(queue->inStack, maxSize); initStack(queue->outStack, maxSize); } // 入队操作 void enqueue(MyQueue* queue, int value) { if (isFull(queue->inStack)) { printf("Queue is full!\n"); return; } // 将元素压入入队栈 push(queue->inStack, value); } // 出队操作 int dequeue(MyQueue* queue) { if (isEmpty(queue->outStack)) { // 如果出队栈为空,则将入队栈的元素依次弹出并压入出队栈 while (!isEmpty(queue->inStack)) { push(queue->outStack, pop(queue->inStack)); } } return pop(queue->outStack); } int main() { MyQueue queue; initQueue(&queue, 5); enqueue(&queue, 1); enqueue(&queue, 2); enqueue(&queue, 3); printf("%d\n", dequeue(&queue)); printf("%d\n", dequeue(&queue)); enqueue(&queue, 4); enqueue(&queue, 5); printf("%d\n", dequeue(&queue)); printf("%d\n", dequeue(&queue)); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值