The application of stack--the function of sorting stack and implementing queue with stack

Source: Internet
Author: User

One: Write an algorithm that arranges the elements in the stack in ascending order. The implementation of the stack is unknown, the algorithm can only be completed by the stack, the function can be used to push, pop, top, empty and so on.

Idea: You can use a different stack to do the sorting.

1, from the original stack in turn pop-up elements into the auxiliary stack;

2, whenever the element to be pressed into is the auxiliary stack is not in ascending order, the elements inside the auxiliary stack is re-pressed into the original stack;

3, until the elements in the auxiliary stack are smaller than the current element to be pressed into;

4, press into the current element.

The code is as follows:

1#include <iostream>2#include <string>3#include <stack>4#include <stdlib.h>5#include <time.h>6 7 using namespacestd;8 9 voidStack_print (stack<int>tmp)Ten { One      while( !tmp.empty ()) A     { -cout << tmp.top () <<" " ; - Tmp.pop (); the     } -cout <<Endl; - } -  +stack<int> Stack_sort (stack<int>s) - { +     if(S.size () <=1) A         returns; at  -stack<int>R; -  -      while(!s.empty ()) -     { -         intTMP =s.top (); in S.pop (); -          while(!r.empty () && tmp >r.top ()) to         { + S.push (R.top ()); - R.pop (); the         } * R.push (TMP); $     }Panax Notoginseng     returnR; - } the  + intMainintargcChar Const*argv[]) A { the Srand (Time (NULL)); +  -stack<int>s; $      for(inti =0; I <Ten; ++i) $     { -S.push (rand ()% -); -     }     the Stack_print (s); - Wuyistack<int> tmp =Stack_sort (s); the Stack_print (TMP); -      Wu     return 0; -}
View Code

Two: How to use two stacks to realize the function of the queue?
Idea: Suppose there are two stacks of T1 and T2;

1. Save the elements just queued to T1.

2, if need to out of queue, you need to determine whether T2 is empty.

A) If T2 is empty, all elements will be T1 into the T2 (at this point in the order of the elements in the T2 element in order to achieve the first-out function), and then remove the top element of the T2 stack;

Second) If the T2 is non-empty (at this point in the T2 element is T1 before pressing in, its stack top is the first element of the queue), then directly remove the T2 stack top can be.

The code is as follows:

1#include <iostream>2#include <string>3#include <stack>4#include <stdlib.h>5#include <time.h>6 7 using namespacestd;8 9Template <typename t>Ten classMqueue One { A  Public: - Mqueue () {} -  the     intSize ()Const; -  -     BOOLEmpty ()Const -{returnSize () = =0;} +  -     voidPushConstT &); +     ConstT Front (); A     voidpop (); at Private: -stack<t> T1;//implement into the "team" function -Stack<t> T2;//achieve the "team" function - }; -  -Template <typename t> in intMqueue<t>::size ()Const - { to     returnT1.size () +t2.size (); + } -Template <typename t> the voidMqueue<t>::p Ush (ConstT &data) * { $ T1.push (data);Panax Notoginseng } -  theTemplate <typename t> + ConstT mqueue<t>:: Front () A { the     if(T2.empty ())//if T2 is empty, the elements in the T1 are pressed into the T2 +     { -          while(!t1.empty ()) $         { $ T2.push (T1.top ()); - T1.pop (); -         } the     } - Wuyi     returnt2.top (); the } -  WuTemplate <typename t> - voidMqueue<t>::p op () About { $     if(T2.empty ())//if T2 is empty, the elements in the T1 are pressed into the T2 -     { -          while(!t1.empty ()) -         { A T2.push (T1.top ()); + T1.pop (); the         } -     } $ T2.pop (); the } the  the intMainintargcChar Const*argv[]) the { -mqueue<int>Myq; in  the      for(inti =0; I <Ten; ++i) the     { AboutMyq.push (rand ()% -); the     } thecout <<"Init Size:"<< myq.size () <<Endl; the      +      while( !myq.empty ()) -     { thecout << Myq.front () <<" ";Bayi Myq.pop (); the     } thecout <<Endl; -cout <<"End Size:"<< myq.size () <<Endl; -     return 0; the}
View Code

Complete.

The application of stack--the function of sorting stack and implementing queue with stack

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.