stack as a special linear table, in the computer has the sequential storage structure and the chain storage structure two kinds of storage structure, according to this we divide the stack into the sequential stack and the chain stack
Sequential stacks
top : Use top to dynamically represent the position of the top element of the stack in the sequence stack, when top=-1 indicates that the stack is empty
This is the definition of the data type of the stack.
typedef char ELEMTYPESTACK;TYPEDEF struct{ elemtypestack elem[maxsize]; int top;} Firstack;
Using an array to store the data, the corresponding subscript indicates his position in the stack, top represents the subscript of the top element of the stack, and if the stack is empty, the Top=-1
initialization of the stack
void Init (Firstack *s) {
We use a sequential stack pointer to represent a sequential stack, and when we initialize a sequence stack to NULL, we set his top value to 1.
stacking operations on sequential stacks
into the stack bool Pushstack (firstack *s,elemtypestack x) { if (s->top==maxsize-1) return false; else { (s->top) + +; s->elem[s->top]=x; return true;
In the stack, first check whether the stack is full, if not full, then top self-increment, and put X into the stack top
overflow : If the stack is full, but also into the stack
Stacking of sequential stacks
BOOL Popstack (firstack *s,elemtypestack x) { if (s->top==-1) return false; else { x=s->elem[s->top]; s->top--; return true; }}
You should first check if the stack is empty, and if it is not empty, assign the value of the top element of the stack to x and then the top self-decrement
underflow : If the stack is empty and the stack is out, the stack underflow will occur
Reading stack top element of sequential stack
Before reading the top element of the stack, first determine whether the stack is empty, if not empty, the top element of the stack is assigned to X
Stack of data Structures (1)--Sequential stacks