Stack
Concept of 1 stacks
Stack: a linear table that restricts insertion and deletion at one end of a table. Also known as the LIFO LIFO (last in first out) or the advanced post-out filo (firstly out) linear table.
Top: One end of the INSERT, delete operation is allowed, also known as the footer. Use the top of the stack pointer to indicate the top element of the stack.
Stack Bottom (Bottom): Is a fixed end, also known as a table header.
Empty stack: When there are no elements in the table, it is called an empty stack.
设栈S=(a1,a2,…an),则a1称为栈底元素,an为栈顶元素,3-1所示。栈中元素按a1,a2,…an的次序进栈,退栈的第一个元素应为栈顶元素。即栈的修改是按后进先出的原则进行的。
Abstract data type definition for stacks
ADT stack{
Data object: D ={ai|ai∈elemset, i=1,2,..., n,n≥0}
Data relations: R ={(ai-1, ai) | ai-1, Ai∈d, i=2,3,..., n}
Basic operations: Initialize, stack, stack, take the top of the stack, calculate the length of the stack, determine whether the stack is empty, etc.
} ADT Stack
Static sequential storage representation of stacks
采用静态一维数组来存储栈。
The bottom of the stack is fixed; the top of the stack changes with the stack and stack operation, and an integer variable top (called the top pointer of the stack) is used to indicate the current stack top position.
Use Top=0 to represent the initial state of the stack empty, each time top points to the top of the stack in the array where it is stored.
Node-to-stack: first perform top plus 1, make top point to the new stack top position, and then save the data element to the top of the stack (the current position indicated by top).
Node out of the stack: first the top point of the stack to take out, and then perform top minus 1, so that top point to the new stack top position.
Implementation of the basic operation of the stack
栈的类型定义define MAX_STACK_SIZE 100 /* 栈向量大小 */typedef int ElemType ;typedefstruct sqstack{ ElemType stack_array[MAX_STACK_SIZE] ;int //栈底,实为数组下标int top; //栈顶,实为数组下标}SqStack ;
栈的初始化SqStack Init_Stack(void){ SqStack S ;S.bottom=S.top=0 ; return(S) ;}
press stack (element into stack) Status push ( Sqstack &s, elemtype e) /* make the data element e into the new stack top */ {if (S.top ==max_stack_size-1 ) return error s.top + ; /* Stack top pointer plus 1, subscript 0 position do not put element */ S.stack _array[s .top ] =e ; /* e becomes the new stack top */ return ok} think: If swap s.top + and S.stack
_array[s.top ]=e location, what's the result?
弹栈(元素出栈)ElemType pop( SqStack &S) /*弹出栈顶元素*/{ if ( S.top==0 )return ERROR ; /* 栈空,返回错误标志 */e=S.stack_array[S.top] ; S.top-- ; return e ; }
When the stack is full, the stack operation must produce a space overflow, referred to as "overflow". Overflow is an error state that should be avoided.
When the stack is empty, the stack operation will also produce overflow, referred to as "underflow". Underflow may be normal, because when the stack is used, its initial or final state is empty stack, so underflow is often used as a condition for control transfer.
Dynamic sequential storage representation of stacks
采用动态一维数组来存储栈。所谓动态,指的是栈的大小可以根据需要增加。
Use bottom to represent the stack bottom pointer, the stack bottom is fixed, and the top of the stack changes with the stack and stack operation. Indicates the current top position of the stack with top (called the top pointer of the stack).
With Top=bottom as the stack empty tag, each top points to the next storage position in the stack topness group (or to the top element of the stack).
Node into the stack: determine if the stack is full, if the stack is full, then re-request a larger memory space, and then save the data element to the top of the stack (top refers to the current position), and then perform top plus 1, so that top point to the top of the next storage location;
Node out stack: First perform top minus 1, so that top points to the storage location of the top element of the stack, and then takes out the top element of the stack.
Implementation of basic operations
1 栈的类型定义#define STACK_SIZE 100 /* 栈初始向量大小 */10 /* 存储空间分配增量 */typedef int ElemType ;typedefstruct sqstack{ ElemType *bottom; /* 栈不存在时值为NULL */ElemType *top; /* 栈顶指针 */int stacksize ; /* 当前已分配空间,以元素为单位 */}SqStack ;
2 栈的初始化Status Init_Stack(SqStack *->bottom=**sizeof(ElemType));if (!-> //若返回空指针,内存分配失败 return -> top=-> bottom ; /* 栈空时栈顶和栈底指针相同 */-> stacksize=return OK ;}
3Stack (element into 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));/ * Stack full, append storage space * /if(!S -BottomreturnERROR; S -Top=S -Bottom+S -stacksize;//Why? S -StackSize+=Stackincrement;}*S -Top=E S -Top++;/ * Stack top pointer plus 1,e to become the new stack top * /returnOK;}
4 弹栈(元素出栈)ElemType pop( SqStack *S) /*弹出栈顶元素*/{ if ( S->top== S-> bottom ) return ERROR ; /* 栈空,返回失败标志 */S-> top-- ; e=*-> top ; return
The difference between address passing and value passing
elemtype pop (sqstack *s) /* popup stack Top element */ {if (S-> top== 0 ) Span class= "Hljs-keyword" >return ERROR; /* stack empty, return failure flag */ S-> top--; E =s-> bottom[s-> top]; return e; } elemtype get_top (sqstack *s) /* pop-up stack top element */ {if (S-> top== 0 ) return ERROR; /* stack empty, return failure flag */ S-> top--; E =s-> bottom[s-> top]; return e; }
Data structure-stack dynamic static sequential storage