Stack of C + + data structures

Source: Internet
Author: User

Stack as one of the most commonly used data structures, has always been the most basic content of the algorithm contest, but it is a beginner's nightmare with recursion, I am here to review knowledge plus the benefit of the original intention of the new, write an article on the basis of the stack of detailed.

Stack is also called LIFO table, the LIFO table, in fact, can be imagined as a stack of books, first put up will be pressed at the bottom, and the top of the book is the last stack up.

This is still more abstract, I would like to explain here:

First the elements in the original stack are "2,7,1" from bottom to top, and a "8" is the topmost element after performing "Press 8", and then the push (2) is the same. Executing pop () pops up the topmost element and executes three times after the elements in the stack are left "2,7".

To make it easier for everyone to understand, here is the code for the handwritten stack.

#include <bits/stdc++.h>using namespacestd;intN, top =0;inta[ the];intdo[ the];intTop ()//returns the top element of the stack{    returnA[top-1];}voidPop ()//Pop Stacks{    if(Top >0) A[top]=0;}voidPushintX//Press Stack{A[top]=x; Top++;}intMain () {CIN>>N;  for(inti =1; I <= N; i++) Cin>> Do[i];//manipulating command Arrays     for(inti =1; I <= N; i++)    {        if(Do[i] = =1)//1 pressure 2 roundspush (i); Else            if(Top >0) Pop ();//stack non-empty to play, otherwise error            Elsecout <<"Error"; } cout<< Top ();//Output Stack Top}

This is just a "baby version" of the handwriting stack, the purpose is just easy to understand, the loophole is still a lot of, such as no empty function, but with a false pointer to judge, and so on, right as an understanding tool, in fact, when doing the problem is usually used in the head function <stack> the self-contained stack.

Here are some questions to help you understand.

Codevs 2058 Bracket Sequence

Sample input

2
{()}<<>>
{{{{{}}}}

Sample output

TRUE

FALSE

This is a very classic stack problem, just need to use the array to simulate the stack on the line, the left bracket pressure stack, if matching to the right parenthesis to play, if empty is legal.

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cstring>using namespacestd;Chars[2000001],a[2000001];inttop=0;intI,j,n;intMain () {CIN>>N;  for(i =1; I <= N; i++)    {        BOOLf =1; CIN>>s; intL =strlen (s);  for(j =0; J < L; J + +)        {            if(s[j]=='('|| s[j]=='{'|| s[j]=='['|| s[j]=='<') {Top++; A[top]=S[j]; }            Else                if((s[j]==')') && (a[top]=='(') && (top>0)) Top--; Else                    if(s[j]==']'&&a[top]=='['&&top>0) Top--; Else                        if(s[j]=='>'&&a[top]=='<'&&top>0) Top--; Else                            if(s[j]=='}'&&a[top]=='{'&&top>0) Top--; Else{f=0;  Break; }                                        }    if(f==0) cout<<"FALSE"<<Endl; Else        if(top>0) cout<<"FALSE"<<Endl; Elsecout<<"TRUE"<<Endl; Top=0; }    return 0;}
Codevs 2,821 days of the city

Like using the stack to simulate the stop area, the regular compression stack operation is good.

#include <bits/stdc++.h>using namespacestd;intp =1;intN, CNT;intans[ the];Chara[ the];stack<int>s;intMain () {scanf ("%d", &N); scanf ("%s", A +1);  for(inti =1; I <= N; i++)    {        if(A[i] = ='A') {Ans[p]=p; P++; Continue; }        Else if(A[i] = ='B') {s.push (i); CNT++; if(CNT >5) {cout<<"No"; return 0; }            Continue; }        Else if(A[i] = ='C')        {            if(S.empty () = =1) {cout<<"No"; return 0; }            Else{Ans[p]=S.top (); P++;                S.pop (); CNT--; }            Continue; }} cout<<"yes\n";  for(inti =1; I < P; i++) cout<< Ans[i] <<Endl; return 0;}

Write so much for the time being, it will be updated later

Stack of C + + data structures

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.