Simulate an infinite queue with two stacks

Source: Internet
Author: User

Idea: Set two stacks, stack 1 plays the role of the queue, stack 2 plays the role of the team. When the queue, all elements into the stack 1, the stack will be filled with the ReAlloc function to append storage space
and save the original stack 1 elements. When the team, the first to determine whether the stack 2 is empty, if it is empty, will determine whether the stack 1 is empty, stack 1 is empty, the queue is empty, stack 1 is not empty
Then stack 1 of all the elements out of the stack into the stack 2, the stack 2 is full, still through the realloc append storage space, and then stack 2 elements out of the stack, if the stack 2 is not empty, stack 2 elements directly out of the stack

extern void *realloc (void *mem_address, unsigned int newsize);
Function: First release the memory area of the original mem_address, and re-allocate the space according to the size specified by newsize.
Copies the original data from the beginning to the newly allocated memory area and returns the first address of the memory area, that is, reallocating the storage

#include <stdio.h>#include<stdlib.h>#defineStack_init_size 3//defines the capacity of the stack's initial space#defineStackincrement 3//allocation increment for storage spacetypedefCharElementType;//Stack 1 for stack, stack 2 for stacktypedefstructsqstack{ElementType*top;//stack Top pointerElementType *Base;//Stack Bottom pointer    intStackSize//The currently allocated space, in units of the number of stack elements}sqstack;sqstack*S1 = (sqstack*)malloc(sizeof(Sqstack)); Sqstack*s2 = (sqstack*)malloc(sizeof(Sqstack));intInistack () {S1-Base= (elementtype*)malloc(sizeof(ElementType) *stack_init_size);//Access ViolationS2->Base= (elementtype*)malloc(sizeof(ElementType) *stack_init_size); if(!s1->Base&&!s2->Base)        return 0; S1->top = s1->Base;//Stack top pointer and stack bottom pointer position when stack emptyS2->top = s2->Base; S1->stacksize = S2->stacksize =stack_init_size; return 1;}intEnstack (CharTintc) {    if(c = =1)    {        if(s1->top-s1->Base= = s1->stacksize)//Stack 1 Full{S1-Base= (elementtype*)realloc(s1->Base,sizeof(ElementType) * (s1->stacksize+stackincrement));//Append storage space            if(!s1->Base)//failure to open space                return 0; S1->stacksize + = stackincrement;//Modify Stack capacity        }        * (s1->top) + + =T; return 1; }    Else{        if(s2->top-s2->Base= = s2->stacksize)//Stack 2 full{s2-Base= (elementtype*)realloc(s2->Base,sizeof(ElementType) * (s2->stacksize+stackincrement));//Append storage space            if(!s2->Base)//failure to open space                return 0; S2->top = s2->Base+ s2->stacksize;//Modify the new value of the top pointer of the stackS2->stacksize + = stackincrement;//Modify Stack capacity        }        * (s2->top) + + =T; return 1; }}intPopstack (Char&t) {    if(S2->top = = s2->Base)//Stack 2 Empty    {        if(S1->top = = s1->Base)//If stack 1 is also empty, the queue is empty            return 0; Else{//Stack 1 All elements out of the stack, into the stack 2             while(S1->top! = s1->Base) {Enstack (* (--s1->top),2);//into the Stack 2} }} t= * (--s2->top); return 1;}voidQueuemenu () {printf ("\t\t\t~~~ two stacks analog infinite queue ~~~\n"); printf ("\t\t\t\t1. Elements queued \ n"); printf ("\t\t\t\t2. Elements out of the team \ n"); printf ("\t\t\t\t3. Display queue \ n"); printf ("\t\t\t\t4. Emptying the queue \ n");}intEnqueueChart) {    returnEnstack (T,1);}intDequeueChar&t) {    returnPopstack (t);}intShowqueue () {if(S2->top = = s2->Base&& S1->top = = s1->Base)        return 0; Else{printf ("the queue elements are:"); ElementType*s = s2->top;  while(s! = s2->Base) printf ("%c",*(--s)); //the operation of the stack is still heres = s1->Base;  while(S1->top! =s) printf ("%c", *s++); printf ("\ n"); return 1; }}intDelqueue () { Free(s1->Base);  Free(s2->Base); returninistack ();}intMain () {intA;    Inistack ();  while(1) {queuemenu (); printf ("Please enter your choice:"); scanf ("%d",&a);  while(A <1|| A >4) {fflush (stdin);//Clear CacheSystem"Pause"); System ("CLS");            Queuemenu (); printf ("Please re-enter your choice:"); scanf ("%d",&a); }        //Inistack ();        CharT; Switch(a) { Case 1: Fflush (stdin);//printf"Please enter the element to be queued:"); scanf ("%c",&t); if(Enqueue (t)) printf ("Elements queued successfully!\n"); Elseprintf ("team full, element failed!\n");  Break;  Case 2:            if(dequeue (t)) printf ("the team elements are:%c\n", T); Elseprintf ("team is empty! The team failed!\n");  Break;  Case 3:            if(!showqueue ()) printf ("queue is empty!\n");  Break; default:            if(Delqueue ()) printf ("queue empty \ n"); Elseprintf ("re-creation failed, queue not emptied \ n");        } fflush (stdin); System ("Pause"); System ("CLS"); }    return 0;}

Simulate an infinite queue with two 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.