How the stack is stored:
1, sequential stack: the use of a group of address consecutive storage units from the bottom of the stack to the top of the stack of data elements, while the top of the pointer to indicate the stack of elements in the order stack position
2, Chain stack: the use of linked list to achieve
Class C language definition for sequential stacks:
typedef struct{
Selemtype *base;
Selemtype *top; The purpose of setting the stack bottom two pointers is to make it easier to determine whether the stack is empty
int stacksize; The maximum capacity currently available for the stack.
}sqstack;
The module description of the sequential stack:
struct STACK {
Selemtype *base;
Selemtype *top;
int stacksize;
};
typedef struct STACK Sqstack;
Status Initstack (Sqstack &s);
Status Destroystack (Sqstack &s);
Status Clearstack (Sqstack &s);
Status stackempty (Sqstack S);
int Stacklength (Sqstack S);
Status GetTop (Sqstack s,selemtype &e);
Status Push (Sqstack &s,selemtype e);
Status POPs (Sqstack &s,selemtype &e);
Status Stacktraverse (Sqstack s,status (*visit));
Status Initstack (Sqstack &s) {
S.base= (Selemtype *) malloc (stack_init_size *sizeof (elemtype));
if (! s.base) exit (OVERFLOW);
S.top=s.base;
S.stacksize=stack_ini_size;
return OK;
}//inistack
Status Destroystack (Sqstack &s); {
}//destroystack
Status Clearstack (Sqstack &s); {
S.top=s.base;
}//clearstack
Status stackempty (Sqstack S); {
if (s.top==s.base) return TRUE;
else return FALSE;
}//stackempty
int Stacklength (Sqstack S); {
int i; Selemtype *p;
i=0;
P=s.top;
while (p!=s.base) {p++; i++}
}//stacklength
Status GetTop (Sqstack s,selemtype &e); {
if (s.top==s.base) return ERROR;
e=* (s.top-1);
return OK;
}//gettop
Status Push (Sqstack &s,selemtype e); {
if (s.top-s.base>=s.stacksize) {
S.base= (Elemtype *) realloc (s.base,
(S.stacksize + stackincrement) * sizeof (elemtype));
if (! s.base) exit (OVERFLOW);
S.top=s.base+s.stacksize;
S.stacksize+=stackincrement;
}
*s.top++=e;
return OK;
}//push
Status POPs (Sqstack &s,selemtype &e); {
if (s.top==s.base)
return ERROR;
E=*--s.top;
return OK;
}//pop
Status Stacktraverse (Sqstack s,status (*visit)); {
}//stacktraverse
The above pseudocode C language source code