1. Definition of Stacks
(1) Definition of stack
Stack is a linear table that restricts insertions and deletions only at the end of a table.
(2) Abstract data types
ADT Stack (stack)
Data
The same linear table. Elements have the same type, and adjacent elements have precursors and successors.
Operation
Initstack (&s): Initializes the operation, creating an empty stack of S.
Destorystack (&s): If the stack exists, destroy it.
Clearstack (&s): empties the stack.
Stackempty (S): Returns True if the stack is empty, otherwise false.
GetTop (S, &e): If the stack exists and is not empty, use E to return the stack top element of S.
push (&s, E): If stack s exists, insert the new element e into the stack s and become the top element of the stack.
pop (&s, &e): Removes the top stack element in the stack S and returns its value with E.
Stacklength (s): Returns the number of elements of the stack S.
Endadt
2. Stack's sequential storage structure and implementation
(1) Sequential storage structure of stacks
#define stack_init_size // Span style= "color: #008000;" > Initial storage space allocation #define stackincrement //< /span> storage space allocation increment typedef struct {selemtype *base ; // selemtype *top; // int stacksize; // The currently allocated storage space, in element } Sqstack;
(2) Basic Operation
Initialize an empty stack:
{S . Base sizeof (Selemtype)); if (! S.basereturn ERROR; = S.base; = stack_init_size; return
Take the top element of the stack operation:
Status GetTop (Sqstack s, Selemtype &e) { if (s.top = = S.basereturn ERROR; 1 ); return OK;}
In-Stack operation:
Status Push (Sqstack &S, Selemtype e) { if(S.top-s.Base>=s.stacksize) {S.Base= (Selemtype *) realloc (S.Base, (s.stacksize + stackincrement) *sizeof(Selemtype)); if(! S.Base)returnERROR; S.top= S.Base+s.stacksize; S.stacksize+=stackincrement; } *s.top++ =e; returnOK;}
Out of stack operation
Status Pop (Sqstack &s, Selemtype &e) { if (s.top = = S.basereturn ERROR; = * (--s.top) ; return OK;}
3. Two stack shared storage space
The key idea is that they are at both ends of the array, moving toward the middle. Top1 and TOP2 are stack 1 and stack 2 of the top pointers, you can imagine, as long as they do not meet, two stacks can be used all the time.
In fact, the use of such data structures is usually the case when the space requirements of the two stacks are inversely related, that is, when one grows and the other stacks are shortened. Otherwise two stacks are constantly growing, and that will quickly overflow as the stack fills up.
Of course, this is only for two of stacks with the same data type of a design technique, if it is different data types of stacks, this approach not only can not better deal with the problem, but will make the problem more complex, we should pay attention to this premise.
(1) Spatial structure
(2) in-stack, out-stack operation
4. Stack's chain storage structure and implementation
(1) The chain storage structure of the stack
(2) Basic operation
"Data Structure" stack