Stack of data structure basics

Source: Internet
Author: User

Stack structure of data structure:

1. Features of the stack:

Compared to a normal array, the stack controls the method of operation, the element can only be stacked on top of the stack, but also only on the stack top out of the stack.

2. Use of the stack:

It is used in the in-depth priority search (DFS), for example, now there is a diagram as follows:

Now to start from a to traverse the entire map, then first look at a connected to the B, D, any choice, such as B, then to save a, press it into the stack, and then traverse B,b to C, then C without the rest of the path can be returned, take out the top of the stack b,b there is no other path, and then remove the top Now a can go D, then D press into the stack, walk E, return to D, and then return to a, now a is the starting point, and there is no other path, then the whole graph is traversed completely.

The order of Access is: A->b->c->b->a->d->e->d->a (roughly)

In this search, the role of the stack can be clearly seen.

In the program design, in-depth first search is an important algorithm, but also very common, the classic maze to find a way out of the problem will need to use it.

3. How the stack is written:

If your own script stack template, can be encapsulated into a struct or class, first of all, the element ontology, into the stack operation, the stack operation, take the top element of the stack, these three are the most basic. For example, if I want to build an int type stack, it can be encapsulated as follows:

1typedefstruct2 { 3   intst[ +];//elements inside the stack4   intTop//Stack top, the original stack inside nothing, can be set to 05   BOOLPushintData//in-stack operation6   {7     if(top>= +)//indicates that the stack has been filled8     {9printf"Full Stack! \ n");Ten       return false;//returns 0 indicates no success in stack One     } Ast[top++]=data; -     return true; -   } the   BOOLPop ()//out of the stack, the element that is now on top of the stack -   { -     if(top==0)//represents a stack empty -     { +printf"Stack empty! \ n"); -       return false; +     } Atop--; at   } -   intFront ()//take the stack head element -   { -     returnst[top-1]; -   } -}stack;

If in some programming competitions, such as ACM, generally do not do stack encapsulation, or directly call the STL, or simply write down on it, as follows:

int st[1000],top=0;

Into the stack: st[top++]=data;

Out of Stack: data=st[--top];

4. Instructions for the stack

As with the queue, is the most fundamental piece of data structure, to be proficient, at the same time to understand his special usage, such as monotonous stack.

Stack of data structure basics

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.