Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use two stacks to implement a queue in C++

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

C++ in how to use two stacks to achieve a queue, many novices are not very clear, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

I. Stack

Stack: only insert and delete elements are allowed at the fixed end. One end of the enter and delete operation is called the top of the stack, and the other end is the bottom of the stack.

Stack features: last in, first out

Stack function: change data from one sequence to another

II. Queue

1. Data is only allowed to be inserted at one end and deleted at the other end

two。 The end of the insertion operation is called the end of the queue (queuing)

3. The end of the deletion operation is called the head of the queue (out of queue).

4. The queue has the characteristic of first-in-first-out

Use two stacks to implement a queue

Idea: define a structure with two stacks, one for incoming data and the other for outgoing data. The data is first put into stack 1, and the data is moved to stack 2. When stack 1 is not empty and stack 2 is empty, the process is: when stack 1 is not empty and stack 2 is empty, take out the top data of stack 1 and put it into stack 2, and so on, and then pop the data of stack 2 to achieve a queue.

The specific code is as follows:

# pragma once#include "stack1.h" # include typedef struct SQueue {Stack stack1;// in data Stack stack2;// out data} SQueue; / / initialize void Init (SQueue * pSQ) {Stack * p1, * p2; p1 = & (pSQ- > stack1); p2 = & (pSQ- > stack2); StackInit (p1); StackInit (p2);} / / Stack void Push (SQueue * pSQ, SDataType data) {Stack * p1, * p2; p1 = & (pSQ- > stack1) P2 = & (pSQ- > stack2); StackPush (p1, data);} / / void Pop (SQueue * pSQ) {Stack * p1, * p2; p1 = & (pSQ- > stack1); p2 = & (pSQ- > stack2); SDataType data; if (StackIsEmpty (p2)) {while (! StackIsEmpty (p1)) {data=StackTop (p1); StackPop (p1); StackPush (p1, data);}} StackPop (p2) } SDataType Front (SQueue * pSQ) {Stack * p1, * p2; p1 = & (pSQ- > stack1); p2 = & (pSQ- > stack2); SDataType data; if (StackIsEmpty (p2)) {while (! StackIsEmpty (p1)) {data = StackTop (p1); StackPop (p1); StackPush (p1, data);}} return StackTop (p2);} is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report