Data structure--stack

Source: Internet
Author: User

data Structure--stack

Objective:

Everybody is good, later will upload the data structure a series of problem-solving record and the study record, thank everybody's concern.

(limited to my experience, so the error of the blog, please review, thank you)

Body:

First day:

Today I learned the stack (in fact, it will be), just a whim to write the data structure of the learning record, so I wrote this blog, do not understand why the last blog post why so many people read, I hope to continue to pay attention.

Well, straight to the point, today is the following:

1. What is a stack?

2. Operation function of Stack

3. The application of the stack detailed description

4. Application of the stack in the title

Enter the first article below: What is a stack?

Yes, what is the stack?

So, have you ever seen a dead end? Now the city is more and more cars, for example, you drive nine o'clock home at night, if there is no parking space, your first idea is to put the car where? Anyway, I would like to block the car outside, in front of the car not to go tomorrow.

That's it:

Obviously, assuming the front on the right side, C1 is the most advanced, but C1 to wait until C5, C4, C3, C2 first all out, to come out, so, the stack of this linear structure has a very practical characteristics-advanced out, which makes stack become a very useful tool for the problem.

You can think of this stack as a "dead end", as long as the elements in front of you do not come out, or are not "pop", you will not be read, just as you take a small burden in a box.

The stack is defined as follows:

First, data push and pop are two different things in a system or data structure stack!

Inserting is to increase the data popup is to delete the data, these operations can only be from the top of the stack as the minimum address as a constraint interface interface, but read the data in the stack is casual without interface constraints said. Many people misunderstand the idea and confuse the stack. And the system stack in the computer architecture and a cross-component interaction of the medium area of the function of the CPU and memory of the communication channel, the CPU only from the system to our own written application of the stack entry specified in the linear read execution instructions, with an image of the word to describe it is pipeline.

The internal CPU interaction is specifically described in the EU and Biu concepts. (Turn from 360)
The stack, as a data structure, is a special linear table that can only be inserted and deleted at one end. It stores data in the last-in, first-out principle, and the first data is pushed into the bottom of the stack, and the final data is at the top of the stack.

When the data needs to be read, the data pops up from the top of the stack (the last data is read first). The stack has a memory function, and the stack's insertion and deletion operations do not need to change the bottom pointer.


Stacks are special linear tables that allow insertions and deletions on the same side. One end of the allowed insert and delete operations is called the top of the stack, and the other end is the bottom of the stack (bottom);

the bottom of the stack is fixed, and the stack top floats; When the number of elements is zero, it is called an empty stack.

Insertion is generally called a stack (PUSH), and deletion is called a fallback (POP). A stack is also called a LIFO table.

Day two:

2. operation function of Stack

The operation function of the stack? Is the system available? Or do you want to write a stack template class yourself?

Learn the data structure, you should know that Stl,stl is a very powerful thing, but you must not sneak to call its public function, of course, the first thing to #include< data structure name, in this chapter, is #include<stack> This includes most of the functions that stack needs to use.

Defined:

Here's how to define it:

Stack<int/char/bool....> s/* (stack name, of course, not necessarily s) */;

You can also define this:

Char/int/bool. s[10000];//simulate a stack

Main functions:

Stack

1. S.push (Int/char ... n):

The function, like push, is to push an element into the stack, that is, to push into the position where the deepest depth and position are present.

Precautions and Operating Procedures:

① if top≥n, then give overflow information, make the wrong deal (before the stack first check whether the stack is full, overflow, dissatisfaction is ②);
② top=top+1 (stack pointer plus 1, point to the address of the stack);
③s (TOP) =n, End (n is the element of the new stack);

2. S.pop ():

Pop: Pop-up, as the name implies, pops up the top of the element, for the action function, then top minus one.

Precautions and Operating Procedures:

If top≤0, then give the underflow information, make the wrong deal (before the stack check whether it is empty stack, empty then overflow; not empty is ②);
②x=s (TOP), (the element after the stack is assigned to X):
③top=top-1, end (stack pointer minus 1, point to top of stack).

3. S.size ()

This, of course, returns the size of the stack.

4. S.top ()

This function is exactly the same as the pop function, which accesses the top element of the stack with the top pointer, but does not eject.

5. Empty ()

Determines whether the stack is empty, true returns 1, and vice versa returns 0

Operation Example:

Stack<int> s;

Operations | Stack | Output

Empty () |  | True

Push (7) |    7 | No

Push (2) |    7 2 | No

Top () |     7 2 | 2

Pop () |     7 | 2

Empty () |  7 | False

Size () |      7 | 1

View Code

3. The application of the stack detailed description

The application of the stack is extensive, the reason is the above mentioned advanced after-out, LIFO characteristics.

Stacks can be used in many topics

For example, the conversion of the input system.

Examples and explanations are given below.

4. Application of the stack in the title

A typical stack problem:

Stack: Bracket matching problem
Difficulty level: B; run time limit: 1000ms; operating space limit: 51200KB; code length limit: 2000000B
Question Description

Given a string containing only the size brackets, please determine if the parentheses in this string are used correctly. The use of parentheses is mathematically different from the distinction between large and small brackets without a level. For example (() {([])}) is the correct string, and ([)] ([)] is wrong.

Input
A string that contains only large parentheses, and the string length does not exceed 1000.
Output
Yes or No. If the parentheses use the correct output Yes, output No if an error is used.
Input example
((){([])})
Output example
Yes

the first response to this question, of course, is to use the stack structure, but how to use it specifically?

1. Encounter a bracket to judge, is left parenthesis to let it into the stack, not to see the top of the stack that element is not the corresponding front bracket, if not, directly return 0;

2. If it is, eject the top element of the stack.

3. Determine if the stack height is 0, the output is no, the output is yes.

View Code

You can also do this (reprint)

View Code



Stack is still very simple, as long as the study will find that: it is the simplest of data structure.

Data structure--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.