In the previous articleImplementation of optimized queues (implemented in C)In, although we have optimized the time complexity of the queue, but it makes the code more readable, and the code looks a little bloated (of course, you can look at these words, mainly to flatter this blog ).
The main implementation here is: Using stacks to implement queues
Basic Ideas:
1. Create two stacks
2. combine the two stacks and assemble them into a queue named instack and outstack respectively. These stacks are used for incoming and outgoing queues.
3. For example, if 1, 2, 3, 4, and 5 need to enter the queue, first press this string of numbers into the instack stack. Assume that the order of pushing is 1, 2, 3, 4, 5 (1 is the bottom of the stack ), then, the data in instack is moved into the outstack. The output stack sequence is: 5, 4, 3, 2, 1. when an outsatck is input, the stack entry sequence is 5, 4, 3, 2, and 1 (5 is the bottom of the stack). When an outstack is output, the sequence is 1, 2, 3, 4, 5 In this way, the inbound values are 1, 2, 3, 4, and 5, which are also 1, 2, 3, 4, and 5, which is the same as the queue.
Code Implementation ideas:
Implementation
Prepare two stacks for queue implementation: inStack and outStack
When a new element is added to the queue: press it into inStack.
When you need to leave the team:
When outStack is empty:
1. pop up the elements in inStack one by one and press them into the outStack.
2. pop up the top element of the outStack stack.
When outStack is not empty:
-Directly bring out the top element of the outStack stack.
Source code entry:
Stack code is used here. For details, refer:Stack implementation and operations (implemented in C)
Header file:
#ifndef _SPQueue_H_#define _SPQueue_H_typedef void SPQueue;SPQueue* SPQueue_Create();void SPQueue_Destroy(SPQueue* queue);void SPQueue_Clear(SPQueue* queue);int SPQueue_Append(SPQueue* queue, void* item);void* SPQueue_Retrieve(SPQueue* queue);void* SPQueue_Header(SPQueue* queue);int SPQueue_Length(SPQueue* queue);#endif
Source file:
// Stack implementation queue. cpp: defines the entry point of the console application. // # Include "stdafx. h" # include "SPQueue. h" # include "LinkStack. h" # include
# Include
Typedef struct {LinkStack * instack; LinkStack * outstack;} TSPQueue; int _ tmain (int argc, _ TCHAR * argv []) {SPQueue * queue = SPQueue_Create (); int a [10] = {0}; int I = 0; for (I = 0; I <10; I ++) {a [I] = I + 1; SPQueue_Append (queue, a + I);} printf ("first incoming queue:"); printf ("Header: % d \ n", * (int *) SPQueue_Header (queue); printf ("Length: % d \ n", SPQueue_Length (queue); for (I = 0; I <5; I ++) {printf ("% d \ t out queue \ n ",*( Int *) SPQueue_Retrieve (queue);} printf ("\ n Second incoming queue: \ n"); printf ("Header: % d \ n ", * (int *) SPQueue_Header (queue); printf ("Length: % d \ n", SPQueue_Length (queue); for (I = 0; I <10; I ++) // Add 10 nodes to the end {a [I] = I + 1; SPQueue_Append (queue, a + I);} while (SPQueue_Length (queue)> 0) {printf ("% d \ t out queue \ n", * (int *) SPQueue_Retrieve (queue);} SPQueue_Destroy (queue ); system ("pause"); return 0;} // create SPQueue * SPQueue _ Create () {TSPQueue * ret = (TSPQueue *) malloc (sizeof (TSPQueue); if (NULL! = Ret) {ret-> instack = LinkStack_Create (); ret-> outstack = LinkStack_Create (); if (NULL = ret-> instack) & (NULL = ret-> outstack) {LinkStack_Destroy (ret-> instack); LinkStack_Destroy (ret-> outstack); free (ret); ret = NULL ;}} return ret;} // destroy void SPQueue_Destroy (SPQueue * queue) {SPQueue_Clear (queue); free (queue);} // clear void SPQueue_Clear (SPQueue * queue) {TSPQueue * SPQueue = (TSPQueue *) queue; if (NULL! = SPQueue) {LinkStack_Clear (SPQueue-> instack); LinkStack_Clear (SPQueue-> outstack) ;}// add int SPQueue_Append (SPQueue * queue, void * item) {TSPQueue * SPQueue = (TSPQueue *) queue; int ret = 0; if (NULL! = SPQueue) {ret = LinkStack_Push (SPQueue-> instack, item);} return ret;} // Delete the header void * SPQueue_Retrieve (SPQueue * queue) {TSPQueue * SPQueue = (TSPQueue *) queue; void * ret = NULL; if (NULL! = SPQueue) {// when the outstack length is 0, move the data in instack into outstackif (LinkStack_Size (SPQueue-> outstack) = 0) {while (LinkStack_Size (SPQueue-> instack)> 0) {LinkStack_Push (SPQueue-> outstack, LinkStack_Pop (SPQueue-> instack ));}} // retrieve the top stack ret = LinkStack_Pop (SPQueue-> outstack);} return ret;} // obtain the header void * SPQueue_Header (SPQueue * queue) {TSPQueue * SPQueue = (TSPQueue *) queue; void * ret = NULL; if (NULL! = SPQueue) {if (LinkStack_Size (SPQueue-> outstack) = 0) {while (LinkStack_Size (SPQueue-> instack)> 0) {LinkStack_Push (SPQueue-> outstack, linkStack_Pop (SPQueue-> instack) ;}} ret = LinkStack_Top (SPQueue-> outstack) ;}return ret ;}// length int SPQueue_Length (SPQueue * queue) {TSPQueue * SPQueue = (TSPQueue *) queue; int ret = 0; if (NULL! = SPQueue) {ret = LinkStack_Size (SPQueue-> instack) + LinkStack_Size (SPQueue-> outstack);} return ret ;}
Running result:
First incoming queue: Header: 1 Length: 101 outbound queue 2 outbound queue 3 outbound queue 4 outbound queue 5 outbound queue 2 inbound queue: Header: 6 Length: 56 queues, 7 queues, 8 queues, 9 queues, 10 queues, 1 Queues, 2 queues, 3 queues, 4 queues, 5 queues 6 out of the queue 7 out of the queue 8 out of the queue 9 out of the queue 10 out of the queue, please press any key to continue...
In case of any errors, please do not hesitate to point out.