# Data structure, data structure and Algorithm

Source: Internet
Author: User

Data structure, data structure and Algorithm
Stack

1 stack Concept
Stack: a linear table that is limited to insert and delete operations at one end of the table. It is also known as the Last In First Out (LIFO) or the First In First Out (First In Last Out) linear table.
Top: the end of a table that allows insert or delete operations. Use the stack top pointer to indicate the top elements of the stack.
Bottom stack (Bottom): it is a fixed end, also known as the header.
Empty Stack: when there are no elements in the table, it is called an empty stack.

``Set stack S = (a1, a2 ,... An), a1 is called the bottom element of the stack. an is the top element of the stack, as shown in 3-1. The elements in the stack Are a1, a2 ,... The first element of the stack should be the top element of the stack. That is, the stack is modified based on the principle of "first-in-first-out.``
Stack Abstract Data Type Definition

ADT Stack {
Data Object: D = {ai | ai ε ElemSet, I = ,..., N, n ≥ 0}
Data Relationship: R = {(ai-1, ai) | ai-1, ai, D, I = 2, 3 ,..., N}
Basic operations: initialization, stack entry, stack exit, top element fetch, stack length determine whether the stack is empty
} ADT Stack

Static sequential storage representation of stacks
``Uses a static one-dimensional array to store stacks.``

◆ The bottom of the stack remains unchanged. The top of the stack changes with the stack Entry and Exit operations. An integer variable top (called the top pointer of the stack) is used to indicate the top position of the current stack.
◆ Use top = 0 to indicate the initial status of empty stacks. Each time top points to the storage position of the stack top in the array.
◆ Add a node to the stack: first execute top plus 1 to point top to the new stack top position, and then save the data element to the top of the stack (the current position indicated by top ).
◆ Point-out Stack: first, extract the top element pointing to the top stack, and then execute top minus 1 to point top to the new top position of the stack.

Implementation of basic stack operations
``Stack type definition define MAX_STACK_SIZE 100/* stack vector size */typedef int ElemType; typedef struct sqstack {ElemType stack_array [MAX_STACK_SIZE]; int bottom; // stack bottom, actually an array subscript int top; // the top of the stack, actually an array subscript} SqStack;``
``Stack initialization SqStack Init_Stack (void) {SqStack S; S. bottom = S. top = 0; return (S );}``
``Press the stack (element into the stack) Status push (SqStack & S, ElemType e)/* make the data element e into the stack as the new stack top */{if (S. top = MAX_STACK_SIZE-1) return ERROR;/* Full stack, return ERROR flag */S. top ++;/* Add 1 to the top pointer of the stack, and leave no element in the position where the subscript is 0 */S. stack_array [S. top] = e;/* e becomes the top of the new stack */return OK;/* stack pressure success */} think: If S is changed. top ++ and S. stack_array [S. top] = e location. What is the result?``
``Bullet stack (element exit stack) ElemType pop (SqStack & S)/* top stack element */{if (S. top = 0) return ERROR;/* the stack is empty, and the ERROR mark */e = S is returned. stack_array [S. top]; S. top --; return e ;}``

When the stack is full, the calculation will inevitably produce space overflow, or "overflow" for short ". Overflow is an error. Try to avoid it.
When the stack is empty, the rollback operation will also overflow ". Underflow may be a normal phenomenon, because the initial or final state of the stack is empty during use, so underflow is often used as a condition for controlling transfer.

Dynamic sequential storage representation of stacks
``Uses a dynamic one-dimensional array to store stacks. The so-called dynamic means that the stack size can be increased as needed.``

◆ Use bottom to indicate the pointer to the bottom of the stack, and the bottom of the stack remains unchanged. The top of the stack changes with the stack entry and rollback operations. Use the top pointer to indicate the top position of the current stack.
◆ Use top = bottom as the stack empty tag. Each time top points to the next storage location (or to the top element of the stack) in the stack top group ).
◆ Adding a node to the stack: determines whether the stack is full. If the stack is full, apply for a larger memory space and save the data elements to the top of the stack (the current position indicated by the top ), then execute top plus 1 to point top to the next storage location on the top of the stack;
◆ Point-out Stack: first, execute top minus 1 to point top to the storage location of the top element of the stack, and then remove the top element of the stack.

Implementation of basic operations

``1. Stack type definition # define STACK_SIZE 100/* Initial stack vector size */# define STACKINCREMENT 10/* bucket allocation increment */typedef int ElemType; typedef struct sqstack {ElemType * bottom;/* When the stack does not exist, the value is NULL */ElemType * top;/* stack top pointer */int stacksize;/* space allocated currently, unit: Element */} SqStack;``
``2. Stack initialization Status Init_Stack (SqStack * S) {S-> bottom = (ElemType *) malloc (STACK_SIZE * sizeof (ElemType); if (! S-> bottom) // If a null pointer is returned, the memory allocation fails to return ERROR; S-> top = S-> bottom; /* When the stack is empty, the top and bottom pointers of the stack are the same */S-> stacksize = STACK_SIZE; return OK ;}``
``3 pressure stack (element into the stack) Status push (SqStack * S, ElemType e) {if (S-> top-S-> bottom> = S-> stacksize-1) {S-> bottom = (ElemType *) realloc (S-> bottom, (STACKINCREMENT + STACK_SIZE) * sizeof (ElemType);/* Full stack, append a bucket */if (! S-> bottom) return ERROR; S-> top = S-> bottom + S-> stacksize; // Why? S-> stacksize + = STACKINCREMENT;} * S-> top = e; S-> top ++;/* Add 1 to the top pointer of the stack, e becomes the top of the new stack */return OK ;}``
``4. ElemType pop (SqStack * S)/* top element of the stack */{if (S-> top = S-> bottom) return ERROR; /* stack is empty. The return failure flag */S-> top --; e = * S-> top; return e ;}``
Differences Between Address Transfer and value transfer
``ElemType pop (SqStack * S)/* top element of the pop-up stack */{if (S-> top = 0) return ERROR;/* the stack is empty, returns the failure flag */S-> top --; e = S-> bottom [S-> top]; return e;} ElemType Get_top (SqStack * S) /* the top element of the stack */{if (S-> top = 0) return ERROR;/* the stack is empty, and the Failure mark */S-> top -- is returned --; e = S-> bottom [S-> top]; return e ;}``

Related Keywords:
Related Article

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD

Learn more >

### 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.