"Getting Started classic"--6.21

Source: Internet
Author: User

Data Structure Basics:

The so-called data structure is the integration of complex data into an orderly structure, we are most familiar with the array, is actually a data structure, it is essentially a linear table, that is, "all the elements in line", the same as the data structure of the linear table and queues, stacks, lists, etc., usually in the processing of some ring structure, We can turn it into a linear table by truncating it from any point.

Queue:

Card game: There is a stack on the table, starting from the first card (that is, the card at the top), numbered 1~n from top to bottom. 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.

Sample input: 7.

Sample output: 1 3 5 7 4 2 6

Analysis: Observe the process, it is very like a long team, the first team to go, and then the first team of people came to the end of the team ... In fact, we introduced the two linear structures in the note column of the AHA algorithm, which can be implemented based on C and pointers, and this article mainly introduces the implementation of STL (Standard Template Library) in the more common C + +.

The specific usage is as follows:

#include <cstdio>#include<queue>using namespaceStd;queue<int> q;//Queue operation 0, defined.  intMain () {intN; scanf ("%d",&N);  for(inti =1; I <= n;i++) q.push (i);//Queue Action 1, adding elements to the end of the team      while(!q.empty ())//Queue Action 2 to determine if the queue is empty{printf ("%d", Q.front ());//Queue Action 3, returns the first element of the queueQ.pop (); //Queue Operation 4, discard the first element of the queueQ.push (Q.front ());    Q.pop (); }     return 0;}

Stack:

EX2:

There is a railway station in a city. There are n carriages entering the station from a direction, numbered 1~n in the order of pit stops. 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 because of the end cap, the carriages that enter 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 you move from C to B, you can't go back to C. In other words, at any moment, there are only two options: A->c and C->b.

Analysis: It can be said that the train track problem is a very fit stack in the life of the data model, the stack can also be seen as a specific example: the existence of badminton or table tennis box, potato chips in the cylinder packaging, we are not difficult to summarize the stack of a property, advanced stack after the stack.

Back to this question, the broker C is a stack model that is laid out. Here we begin to simulate the process of judging the program.

We use two variables a, b to separate the simulation process of a, B station this in the matching subscript. At the same time a also has the function of indicating the marking.

Its matching simulation process is as follows.

First to determine whether the head element in the a station is equal to the element that the B station is currently matching, if the same, we will put this element into the stack and then out of the stack, two position variables A, B are added 1.

If not equal, do not worry about the first element of a station into the stack, you should first determine whether the top element of the stack can be matched with the element B is matching, if matching, pop up the top element of the stack.

After the above two judgments, the pressure stack operation.

If none of the first three steps have been made, it means that the a station is empty, the stack is empty but cannot be matched, indicating that there is no arrangement.

The simple reference code is as follows.

#include <cstdio>#include<stack>using namespacestd;Const intMAXN = ++5;intN, TARGET[MAXN];intMain () { while(SCANF ("%d", &n)! =EOF) {Stack<int>s;  for(inti =1; I <= n;i++) scanf ("%d",&Target[i]); intb; A= B =1; intOK =1;  while(B <=N) {if(A = = Target[b]) {a++, b++;} Else if(!s.empty () && s.top () = = Target[b]) {S.pop (), b++;} Else if(A <= N) {S.push (a++);} Else{OK =0; Break;} } printf ("%s\n"Ok?"Yes":"No"); }}

"Getting Started classic"--6.21

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.