Two queues implement one stack

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/ns_code/article/details/25076689


Question:Two queues are used to simulate a stack, that is, two queues are used for outbound and inbound operations.

Ideas:It is difficult to figure out a solution to the problem by drawing a sketch. The idea is as follows:

Assume that there are two queues Q1 and Q2. when both are empty, the inbound operation can be simulated by the queuing operation. You can select an empty queue at will. Assume that Q1 is selected for the inbound operation, now suppose a, B, and c are in the stack in sequence (that is, they are in the queue Q1 in sequence). If you want to simulate stack operations, you need to export c out of the stack, because at the top of the stack, in this case, you can use an empty queue Q2 to queue a and B in sequence from Q1, and then enter the queue Q2. Then, release the last element c of Q1, and change Q1 to an empty queue, there are two elements in Q2: the first element of the team is a and the end element is B. If you perform the stack import operation, you need to enter the element into the non-empty queue in Q1 and Q2, that is, to enter the Q2 queue, if the stack is output, it is the same as above. In addition to the last element of Q2, it will all go to the queue Q1 in sequence, and then the last element of Q2 will go out of the queue.

The implementation code is as follows:

/* Use two queues to simulate the stack operation */void push (PQUEUE pS1, PQUEUE pS2, int val) {if (is_empty (pS2) en_queue (pS1, val ); elseen_queue (pS2, val);}/* use two queues to simulate stack operations */bool pop (PQUEUE pS1, PQUEUE pS2, int * pData) {if (is_empty (pS1) & is_empty (pS2) return false; int DelData; if (! Is_empty (pS2) {int len = length (pS2); while (len --> 1) {de_queue (pS2, & DelData); en_queue (pS1, DelData );} // send the last element of the queue to the queue as the output stack element de_queue (pS2, pData); return true;} if (! Is_empty (pS1) {int len = length (pS1); while (len --> 1) {de_queue (pS1, & DelData); en_queue (pS2, DelData );} // send the last element of the queue to the queue as the output stack element de_queue (pS1, pData); return true ;}}

The complete code (using the previously written chain Queue) is as follows:

/***************************************: Use two queues to simulate a stack ********************************** * ********************************/# include <stdio. h> # include <stdlib. h> typedef struct Node {int data; struct Node * pNext;} NODE, * PNODE; typedef struct Queue {PNODE front; // The team head pointer PNODE rear; // team end pointer} QUEUE, * PQUEUE; PQUEUE create_queue (); bool is_empty (PQUEUE); void en_queue (PQUEUE, int); bool de_queu E (PQUEUE, int *); void destroy_queue (PQUEUE); void traverse_queue (PQUEUE); int length (PQUEUE); void push (PQUEUE, PQUEUE, int ); bool pop (PQUEUE, PQUEUE, int *); int main () {int pData; // used to save the element value of the queue // create a queue and test the PQUEUE pS1 = create_queue (); PQUEUE pS2 = create_queue (); push (pS1, pS2, 4 ); push (pS1, pS2, 5); printf ("the length of pS1: % d \ n", length (pS1); printf ("the length of pS2: % d \ n ", length (pS2); if (pop (pS1, pS2, & pData) p Rintf ("% d is pop out \ n", pData); elseprintf ("Stack is empty, can not pop \ n"); printf ("the length of pS1: % d \ n ", length (pS1); printf (" the length of pS2: % d \ n ", length (pS2); push (pS1, pS2, 6); printf ("the length of pS1: % d \ n", length (pS1); printf ("the length of pS2: % d \ n ", length (pS2); push (pS1, pS2, 7); printf ("the length of pS1: % d \ n", length (pS1 )); printf ("the length of pS2: % d \ n", length (pS2); if (pop (pS1, pS2, & pD Ata) printf ("% d is pop out \ n", pData); elseprintf ("Stack is empty, can not pop \ n "); printf ("the length of pS1: % d \ n", length (pS1); printf ("the length of pS2: % d \ n", length (pS2 )); if (pop (pS1, pS2, & pData) printf ("% d is pop out \ n", pData); elseprintf ("Stack is empty, can not pop \ n "); printf (" the length of pS1: % d \ n ", length (pS1); printf (" the length of pS2: % d \ n ", length (pS2); if (pop (pS1, pS2, & pData) printf (" % d is p Op out \ n ", pData); elseprintf (" Stack is empty, can not pop \ n "); printf (" the length of pS1: % d \ n ", length (pS1); printf ("the length of pS2: % d \ n", length (pS2); if (pop (pS1, pS2, & pData )) printf ("% d is pop out \ n", pData); elseprintf ("Stack is empty, can not pop \ n"); return 0 ;} /* Create an empty queue. Both the head pointer and tail pointer point to the header node. the header node does not store data, but only the pointer */PQUEUE create_queue () {PQUEUE pS = (PQUEUE) malloc (sizeof (Queue); pS-> front = (PNODE) malloc (sizeof (NODE); if (! PS |! PS-> front) {printf ("pS or front malloc failed !! "); Exit (-1);} else {pS-> rear = pS-> front; pS-> front-> pNext = NULL;} return pS ;} /* determine whether the queue is empty */bool is_empty (PQUEUE pS) {if (pS-> front = pS-> rear) return true; elsereturn false ;} /* enter the function from the end of the team. the pointer to the team header remains unchanged */void en_queue (PQUEUE pS, int e) {PNODE pNew = (PNODE) malloc (sizeof (NODE )); if (! PNew) {printf ("pNew malloc failed"); exit (-1);} else {pNew-> data = e; pNew-> pNext = NULL; pS-> rear-> pNext = pNew; pS-> rear = pNew;} return;}/* the team-out function. The team-out function remains unchanged at the end of the team, however, when the last element leaves the team, you need to assign a new value to the team's tail pointer so that it points to the header node */bool de_queue (PQUEUE pS, int * pData) {if (is_empty (pS )) return false; else {PNODE p = pS-> front-> pNext; * pData = p-> data; pS-> front-> pNext = p-> pNext; // This is a special case where the queue Header element leaves the queue. Generally, when deleting the Header element, // you only need to modify the pointer in the header node, however, when the last element in the queue is deleted, // The tail pointer of the queue is also lost. Therefore, you need to assign a new value to the tail pointer (pointing to the header node ). If (pS-> rear = p) pS-> rear = pS-> front; free (p);} return true;}/* traverse the queue, output the element */void traverse_queue (PQUEUE pS) {if (is_empty (pS) printf ("there is no data in the queue! \ N "); else {PNODE pCurrent = pS-> front-> pNext; printf (" Now datas int the queue are: \ n "); while (pCurrent) {printf ("% d", pCurrent-> data); pCurrent = pCurrent-> pNext;} printf ("\ n");} return ;} /* calculate the queue length */int length (PQUEUE pS) {int count = 0; PNODE pCurrent = pS-> front-> pNext; while (pCurrent) {count ++; pCurrent = pCurrent-> pNext;} return count;}/* destroy the queue. the header node is also destroyed, and the pS node is also destroyed and pointed to null, avoid generating vertical pointers */void destroy_queue (PQUEUE pS) {If (is_empty (pS) return; else {while (pS-> front) {pS-> rear = pS-> front-> pNext; free (pS-> front); pS-> front = pS-> rear;} free (pS); pS = 0; return ;} /* use two queues to simulate the stack operation */void push (PQUEUE pS1, PQUEUE pS2, int val) {if (is_empty (pS2) en_queue (pS1, val ); elseen_queue (pS2, val);}/* use two queues to simulate stack operations */bool pop (PQUEUE pS1, PQUEUE pS2, int * pData) {if (is_empty (pS1) & is_empty (pS2) return false; int DelData; if (! Is_empty (pS2) {int len = length (pS2); while (len --> 1) {de_queue (pS2, & DelData); en_queue (pS1, DelData );} // send the last element of the queue to the queue as the output stack element de_queue (pS2, pData); return true;} if (! Is_empty (pS1) {int len = length (pS1); while (len --> 1) {de_queue (pS1, & DelData); en_queue (pS2, DelData );} // send the last element of the queue to the queue as the output stack element de_queue (pS1, pData); return true ;}}

Test results:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.