The base of the stack __ data structure

Source: Internet
Author: User

1. The base   of the stack,
    Stack, also become the stack, is an important linear structure. The stack has the characteristics of a linear table: Each element has only one precursor element and a successor element (except the first element and the last element), but unlike the linear table in operation, the stack is a linear table with limited operations, allowing insertions and deletions only at one end of the stack. Stacks can be stored in sequential storage structure and chained storage structure, and the stacks called sequential stacks, which use chain-type storage structure, are called chain stacks. Stack application is very wide, in the expression evaluation, bracket matching is often used to stack ideas. &NBSP
2. Operation of the Stack  
    allow one end of the insert and delete operation to be called the top of the stack and the other end to be called the bottom of the stack. The top of the stack is dynamically changing, indicated by a variable called the top of the stack, which is called an empty stack when there are no elements in the table. The insert operation of the stack is called the stack or the stack, and the delete operation is called a stack or a fallback.  
                                                                    

Diagram Stack Sketch
For example: The sequence of a feed stack consists of a, B, and C, and its stack sequence has five possibilities of ABC, ACB, BAC, BCA and CBA, and only the cab is an impossible output sequence. Because the ABC into the stack, c out of the stack and then B to the stack, it is impossible to a before B out of the stack, so the cab is impossible sequence.

The basic operation of the stack consists of the following seven kinds:

(1) Initstack (&s): Initialize operation, build an empty stack S.

(2) Stackempty (s): If stack S is empty, return 1, otherwise return 0.

(3) GetTop (s,&e): Returns the stack top of the stack S to E.

(4) Pushstack (&s,e): Inserts element e into the stack S to make it the new stack top element.

(5) Popstack (&s,&e): Deletes stack S's top element.

(6) Stacklength (s): Returns the number of elements in the stack S.

(7) Clearstack (s): Empty stack S.
3. Shared Stacks

The application of the stack is very extensive, and often a program needs to use multiple stacks at the same time. Using sequential stacks can be difficult to estimate precisely because of the size of the stack space, resulting in some stack overflow, and some of the stack space is idle, in order to solve this problem, you can allow multiple stacks to share a large enough continuous storage space, by leveraging the dynamic characteristics of the stack to enable a number of stack storage space to complement each other, storage space is effectively used, This is the share of the stack.

In the stack sharing problem, the most commonly used is two stacks of sharing, the implementation method is two stacks sharing a one-dimensional array space S (stacksize), two stacks of stack bottom set at both ends of the array, when there are elements into the stack, stack top position from the stack at both ends of the head to grow, when the stack of two stacks meet, the stack full. The storage of the shared stack represents the following illustration:

Figure two stacks of shared space

As pictured above, set two stacks to share a one-dimensional array space stack[m], two stacks of stacks at each end of the array space, i.e. Stack[o] and stack[m-1]. Thus, when the element is in the stack, two stacks are stretched from both ends to the middle. The storage space is complemented by dynamic changes of two stack top pointers (TOP1 and TOP2).

The data structure of the two stack spaces is defined as follows: C code #define M 100//two stack shared storage space size typedef struct {elemtype stack[m];         Two-stack shared one-dimensional array space int top[2]; Two stacks of stack top pointer} dsegstack;

4. The chain representation and realization of the stack

In the sequential stack, because the sequential storage structure needs to be statically allocated in advance, and the storage scale is often difficult to determine, if the stack space allocation is too small, may be as overflow, if the stack space allocation is too large, but also cause storage space waste. Therefore, in order to overcome the disadvantage of sequential storage, a chain-type storage structure is used to represent stacks.

the stack using chained storage is called chain stack or chain-type stack. The chain stack is composed of a node, which contains two parts of data field and pointer field. In the chain stack, the data domain of each node is used to store each element in the stack, and the pointer field is used to represent the relationship between the elements.

Diagram Chain stack diagram

The data structure of the chain stack is defined as follows: C code typedef struct Node {DataType data;   struct node *next;   }lstacknode, *linkstack; Linkstack is the top pointer of the stack and always points to the head node of the chain stack.

5. Stack and recursion

Recursion refers to the definition of a function and the invocation of itself while defining itself. If a function calls itself directly in the body of a function, it is called a direct recursive function. If you go through a series of intermediate invocations, calling your own functions indirectly is called an indirect recursive call. Recursive problems can be decomposed into small and similar problems to be solved.

The recursive algorithm has the advantages of clear structure, readability, easy realization and the correctness of recursive algorithm is easily proved. However, the execution efficiency of recursive algorithm is low, because recursion needs to be repeated into stack, time and space overhead is large. Recursive algorithms can also be converted to non recursive implementations, which is the elimination of recursion. There are two ways to eliminate recursion, one is for simple recursion can be directly used in iterations, through the loop structure can be eliminated, another way is to use the stack to implement.

Using the stack to simulate recursion can be accomplished through the following steps:

(1) Set up a work stack to save the recursive work record, including the actual parameters, return address, etc.

(2) The parameter and return address of the call function are passed into the stack.

The

    (3) uses the cyclic simulation recursive decomposition process to stack the parameters and return addresses of the recursive process at a level. When the recursive end condition is met, the stack is retreated in sequence, and the result is returned to the previous layer until the stack is empty.

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.