Use of "C + +" STL queues and stacks

Source: Internet
Author: User

The STL Standard Template Library for C + + provides basic operations for queues and stacks. Below, we introduce the use of STL queue and STL stack by two demos respectively.

Demo1:stl Queue

"title" Card Game (topic from Rujia "Introduction to algorithmic competition")

Another stack of cards on the table, from the first card (that is, the card at the top) starts from top to bottom numbered to 1~n. When there are at least two cards left, do the following: Throw away the first card and put the new first one at the end of the stack. Enter n, output each time you discard the card, and the last remaining card.

Example input: 7

Sample output: 1 3 5 7 4 2 6

The "analysis" of these cards is a first-in-one-out (FIFO) queue, each time to the first two of the Pai, one out of the queue, and the second to the end of the team.

Code

#include <cstdio> #include <queue>using namespace Std;int main () {queue<int> q;int n;scanf ("%d",& n); for (int i = 0;i < n;i++) {Q.push (i+1);} while (Q.size () >=2) {printf ("%d\t", Q.front ()),//The output requires the first sheet of the team, and out of the queue q.pop (); Q.push (Q.front ());//Put the team head second to the end of the team Q.pop ();} printf ("%d\n", Q.front ());//Output the remaining last return 0;}


Demo2:stl Stack

"title" Track (title from Rujia "Introduction to Algorithmic competition")

A city has a railway station track laying, n carriages from a direction into the station, according to the sequence number of 1~n. Your task is to have them enter the B-direction rails in a certain order and exit the station. In order to reorganize the carriages, you can use the transit station C. This is a station that can park any number of carriages. But as the end caps, the carriages entering C must drive out C in reverse order. For each compartment, once you move from a to C, you can't go back to a, and once C moves into B, you can't go back to C, in other words, there are only two options at any point: a->c,c->b.


Sample input:

5

1 2 3) 4 5

5

5 4 1) 2 3

6

6 5 4 3 2 1

Sample output:

Yes

No

Yes


"Analysis" A, b stations for the queue, C-station LIFO stack. Initialize the A station queue to the 1~n,b station queue to initialize the order of user input, making the following loop until B is listed as empty:

1. If the team head of station A is equal to the head of station B, direct the train of the a station to station B (queue A and B).

2. Otherwise if the C stack top element and the B team head element are equal, the C's top train will be driven into B (c out-stack operation, B-out queue operation).

3. Otherwise if the a queue is not empty, a queue head element is placed in the C stack,

4. Otherwise the tag is unsuccessful and jumps out of the loop.

Code

#include <cstdio> #include <stack> #include <queue>using namespace Std;int main () {int N,temp;while ( scanf ("%d", &n) ==1) {stack<int> c;queue<int> a,b;for (int i=1;i<=n;i++) {scanf ("%d", &temp); A.push (i); B.push (temp);} int Ok=1;while (b.size ()!=0) {if (A.size ()!=0&&a.front () ==b.front ()) {A.pop (); B.pop ();} else if (c.size ()!=0 && B.front () ==c.top ()) {B.pop (); C.pop ();} else if (a.size ()!=0) {C.push (A.front ()); A.pop ();} else {ok=0;break;}} if (OK) printf ("Yes"); elseprintf ("No");} return 0;}



Use of "C + +" STL queues and 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.