"Sword Point offer" two queue implementation stacks

Source: Internet
Author: User

Reprint Please specify the Source: http://blog.csdn.net/ns_code/article/details/25076689


title: simulate a stack with two queues, which is the queue-out and queued operation with two queues. To implement the stack's stack and stack operation.

idea: draw a sketch slightly. It is not difficult to come up with a solution to the problem. Ideas such as the following:

If there are two queues Q1 and Q2, when both are empty, the in-stack operation can be simulated with a queued operation. Be able to randomly select an empty queue, if Q1 is selected for the stack operation. Now if a,b,c into the stack sequentially (that is, go to queue Q1). At this point if you want to simulate the stack operation, you need to c out of the stack, due to the top of the stack. This is the time to consider using the empty queue Q2. A A, a, and then into the queue Q2, then the last element of Q1 C out of the team can be, at this time Q1 into an empty queue, Q2 has two elements. Team head element is a, the tail element is B, then if you run into the stack operation, you need to enter the elements into the Q1 and Q2 in the non-empty queue, that is, into the Q2 queue, out of the stack, as before, will Q2 except the last element all out of the team, and then into the queue Q1, Then the last element of the Q2 will be out of the team.

Implementation code such as the following:

/* Simulate a stack operation with two queues */void push (Pqueue ps1,pqueue ps2,int val) {if (Is_empty (pS2)) En_queue (PS1, Val); Elseen_queue (PS2, Val);} /* Simulate stack operation with two queues */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);} The last element of the queue is out of the team. As the 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);} De_queue the last element of the queue as a stack element (Ps1,pdata); return true;}}

The complete code (with the once-written chained queue) is for example the following:

/******************************************************************* title: Simulate a stack with two queues ****************************** /#include <stdio.h> #include <stdlib.h>typedef struct node{int data ; struct Node *pnext;}  node,*pnode;typedef struct Queue{pnode front;   Team head pointer pnode rear; The tail of the team}queue,*pqueue; Pqueue create_queue (); bool Is_empty (pqueue); void En_queue (pqueue, int); bool De_queue (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 team//create a queue and test pqueue pS1 = Create_queue (); Pqueue pS2 = Create_queue ();p ush (ps1,ps2,4);p ush (ps1,ps2,5);p rintf ("The length of pS1:%d\n", Length (pS1));p rintf ("the Length of pS2:%d\n ", Length (pS2)), if (pop (ps1,ps2,&pdata)) printf ("%d is POPs out\n ", pData); elseprintf (" Stack is Empty,can not pop\n ");p rintf (" The length of the pS1:%d\n ", Length (pS1));p rintf (" The length of the pS2:%d\n ", Length (pS2));p Ush (ps1,ps2,6);p rintf ("The length of pS1:%d\n", Length (pS1));p rintf ("The length of the pS2:%d\n", Length (pS2));p Ush (ps1,ps2,7); printf ("The length of pS1:%d\n", Length (pS1));p rintf ("The length of pS2:%d\n", Length (pS2)); if (Pop (ps1,ps2,&pdata) printf ("%d is POPs out\n", pData); elseprintf ("Stack is Empty,can not pop\n");p rintf ("The length of pS1:%d\n", Length (pS1)) ;p rintf ("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");p rintf ("The length of the pS1:%d\n", Length (pS1));p rintf ("The length of the pS2:%d\n", Length (pS2) ), if (pop (ps1,ps2,&pdata)) printf ("%d is POPs out\n", PData), elseprintf ("Stack is Empty,can not pop\n");p rintf ("the Length of pS1:%d\n ", Length (pS1));p rintf (" The length of the pS2:%d\n ", Length (pS2)), if (pop (ps1,ps2,&pdata)) printf ("%d is POPs out\n ", pData); elseprintf (" Stack is Empty,can not pop\n "); return 0;} /* Create an empty queue, the team head pointer and the tail pointer all point to the head node, the head node does not hold data, just hold the pointer */pqueue create_queue () {Pqueue PS = (pqueue) malloc (sizEOF (Queue));p S->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;} /* Infer if the queue is empty */bool is_empty (Pqueue PS) {if (Ps->front = = Ps->rear) return True;elsereturn false;} /* Enter the team function, enter the team from the end of the team, the team head pointer 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;} /* Out of the line function, from the team head out of the team, the tail pointer remains the same, but when the last element out of the team, it is necessary to assign a value to the tail pointer again, so that it points to the head 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;// Here is the queue header element out of the special case, in general, delete the team head element//only need to change the pointer in the head node. But when the last element in the queue is deleted. The end-of-queue pointer is also missing, so you need to assign another value to the tail pointer (point to the Head node). if (ps->rear = = p) ps->rear = Ps->front;free (p);} return true;} /* Traverse the queue. */void traverse_queue (Pqueue PS) {if (is_empt) from the opponent to the end of the team.Y (PS)) printf ("There is no data in the queue!\n"), else{pnode pcurrent = ps->front->pnext; printf ("now datas int the Q Ueue are:\n "), while (pcurrent) {printf ("%d ", pcurrent->data);p current = Pcurrent->pnext;} printf ("\ n");} return;} /* */int length of the queue (Pqueue pS) {int count = 0; Pnode pcurrent = ps->front->pnext; while (pcurrent) {count++;p current = Pcurrent->pnext;} return count;} /* Destroy the queue, the head node is destroyed, and finally the PS node is destroyed and pointed to null. Avoid vertical pointer generation */void destroy_queue (pqueue PS) {if (Is_empty (PS)) Return;else{while (ps->front) {ps->rear = ps-> Front->pnext;free (ps->front);p s->front = Ps->rear;}} Free (PS);p s = 0;return;} /* Simulate a stack operation with two queues */void push (Pqueue ps1,pqueue ps2,int val) {if (Is_empty (pS2)) En_queue (PS1, Val); Elseen_queue (PS2, Val);} /* Simulate stack operation with two queues */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);} The last element of the queue is out of line, as the out 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);} The last element of the queue is out of the team. As a stack element de_queue (ps1,pdata); return true;}}

Test results:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbnnfy29kzq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

"Sword Point offer" two queue implementation stacks

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.