用栈实现队列

代码编写:用两个栈实现一个队列

栈的特点:
	1.后进先出LIFO(Last In First Out2.出入元素都是在同一端--栈顶 单开口类比水桶
队列的特点:
	1.先进先出FIFO(First In First Out2.出入元素在不同的的两端---队头 & 队尾 双开口类比吸管

要求:能画图说明入栈和出栈
栈:
	入栈:【栈底】口口口口口口口【栈顶】<---7
	出栈:【栈底】口口口口口口口【栈顶】--->7
队列:
	入栈:【队头】口口口口口口口【队尾】<---7
	出栈:7<---【队头】口口口口口口口【队尾】

思考:单个的栈是不可能实现队列的,两个栈就可以!
方式:组装
	一个栈作为队列的入口,负责插入新元素
	一个栈负责队列的出口,负责移除老元素
观察:	
	栈1 【栈底】口口口口口口口【栈顶】<---  5 4 3 2 12 【栈底】口口口口口口口【栈顶】--->  1 2 3 4 5
结论:
	以栈1出栈的顺序压入栈2,栈2再出栈,即栈1和栈2入栈的顺序是相反的
了解java.util.Stack<E>的API方法:
boolean empty()			测试栈是否为空
E peek()				查看栈顶的元素,不移除
E pop()					移除栈顶的元素,并返回
E push(E item)			元素入栈,在栈顶
int search(Object o)	在栈中查找元素,返回栈顶返回的距离,栈顶为1   底层使用equals方法

代码实现:

import java.util.Stack;

public class TestStackImplQueue {
    /**
     * 定义两个栈实现队列
     * 栈1负责插入新元素
     * 栈2负责移除老元素
     */
    private Stack<Integer> stack1 = new Stack<Integer>();
    private Stack<Integer> stack2 = new Stack<Integer>();

    /**
     * 入队  即元素压入栈1
     *
     * @param item
     */
    public void inQueue(int item) {
        stack1.push(item);
    }

    /**
     * 出队 即元素弹出栈2
     *
     * @return
     */
    public Integer outQueue() {
        //最后考虑 栈2为空的情况:栈2为空,栈1还有吗?
        if (stack2.empty()) {
            if (stack1.empty()) {
                return null;
            } else {
                overTurn();
            }
        }
        return stack2.pop();//E / null
    }

    /**
     * 元素从栈1中出栈,然后压入栈2
     * 栈1不为空就一直弹弹弹
     */
    private void overTurn() {
        while (!stack1.empty()) {
            stack2.push(stack1.pop());
        }
    }

    /**
     * 入栈是没有问题的,出栈需要考虑是否能拿到全部元素
     */

    public static void main(String[] args) {
        TestStackImplQueue test = new TestStackImplQueue();
        test.inQueue(5);
        test.inQueue(4);
        test.inQueue(3);

        System.out.println(test.outQueue());
        System.out.println(test.outQueue());
        System.out.println(test.outQueue());

        test.inQueue(8);
        System.out.println(test.outQueue());
    }
}
  • 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、付费专栏及课程。

余额充值