1. Basic Concepts:
The C-language stack refers to a linear table that restricts insertions and deletions only at the end of the table.
as a commonly used data structure in C language, stack is a special linear table which can only be inserted and deleted at one end. It stores the data according to the advanced principle, the first data is pressed into the bottom of the stack, the final data is on the top of the stack, the data needs to be read from the top of the stack to pop up (the last data is first read out). The stack has memory function, the stack inserts and deletes the operation, does not need to change the stack bottom pointer.
Stacks are special linear tables that allow inserts and deletions at the same end. One end of the allowed insert and delete operation is called the top of the stack, the other end is the stack bottom (bottom), the stack bottom is fixed, and the stack top is floating, and the number of elements in the stack is zero called an empty stack. Inserts are generally called the Feed stack (PUSH), and deletions are called Fallback stacks (POPs). The stack is also called a LIFO table.
In a computer system, a stack is a dynamic memory region with the above attributes. The program can press the data into the stack, or it can eject the data from the top of the stack. In the i386 machine, the top of the stack is positioned by a register called ESP. The operation of the stack makes the address of the top of the stack smaller, and the pop-up operation makes the address of the top of the stack larger.
Stack plays an important role in the operation of the program. Most importantly, the stack holds the maintenance information needed for a function call, which is often referred to as a stack frame or an activity record. Stack frames generally contain the following information:
(1) Return address and parameter of function
(2) Temporary variables: including non-static local variables of functions and other temporary variables that the compiler automatically generates
2. Implementation code:
#define STACK_INIT_SIZE 10/* Storage space initial allocation/#define STACKINCREMENT 2/* Storage space allocation increment/typedef struct SQSTACK {Selemtyp e *base; /* The value of base is null */Selemtype *top before and after the stack is constructed; /* stack top pointer/int stacksize; /* The currently allocated storage space, in element unit */}sqstack; /* Sequential stack/Status initstack (Sqstack *s) {/* Constructs an empty stack S/* (*s). base= (Selemtype *) malloc (stack_init_size*sizeof (Selemtype))
; if (!) ( *s). Base) exit (OVERFLOW);
/* Storage Allocation failure/* (*s). top= (*s). Base;
(*s). Stacksize=stack_init_size;
return OK;
Status Destroystack (Sqstack *s) {/* Destroy stack s,s no longer exists */free ((*s). Base);
(*s). Base=null;
(*s). Top=null;
(*s). stacksize=0;
return OK;
Status Clearstack (Sqstack *s) {/* put S to empty stack//(*s). top= (*s). Base;
return OK;
Status Stackempty (Sqstack s) {/* If stack S is empty stack, returns True, otherwise returns false/if (s.top==s.base) return true;
else return FALSE;
The int stacklength (Sqstack s) {//* Returns the number of elements of S, that is, the length of the stack/return s.top-s.base; } Status GetTop (Sqstack s,selemtype *e) {/* If the stack is not empty, then return the top element of S in E, and return OK, otherwise return error * *
if (s.top>s.base) {*e=* (s.top-1);
return OK;
else return ERROR;
Status Push (Sqstack *s,selemtype e) {/* insert element e As new stack top element/if (*s). top-(*s) base>= (*s). stacksize)/* stack full, append storage space * *
{(*s). base= (Selemtype *) realloc ((*s). Base, ((*s). Stacksize+stackincrement) *sizeof (Selemtype)); if (!) ( *s). Base) exit (OVERFLOW);
/* Storage Allocation failure//(*s). top= (*s). base+ (*s). stacksize;
(*s). Stacksize+=stackincrement;
} * ((*s). top) ++=e;
return OK; Status POPs (Sqstack *s,selemtype *e) {* * If the stack is not empty, then delete the top element of S, return its value with E and return OK, otherwise error */if (*s). top== (*s). Base) return
ERROR;
*e=*--(*s). Top;
return OK; Status Stacktraverse (Sqstack s,status (*visit) (Selemtype)) {/* Call function visit () from the bottom of the stack to the top of the stack, in turn, for each element in the stack.
* * * once visit () fails, the operation fails/while (S.top>s.base) visit (*s.base++);
printf ("\ n");
return OK; #include "c1.h" typedef int SELEMTYPE;
/* Define stack element type, this sentence should be in front of C3-1.h * * #include "c3-1.h" #include "bo3-1.c" Status visit (selemtype c) {printf ("%d", c);
return OK; } void Main () {int J;
Sqstack s;
Selemtype e;
if (Initstack (&s) ==ok) for (j=1;j<=12;j++) Push (&S,J);
printf ("The elements in the stack are:");
Stacktraverse (S,visit);
Pop (&s,&e);
printf ("Pop-up stack top element e=%d\n", e);
printf ("Stack null No:%d (1: null 0: NO) \ n", Stackempty (s));
GetTop (s,&e);
printf ("Stack top element e=%d stack length is%d\n", e,stacklength (s));
Clearstack (&s);
printf ("The stack is empty after the stack is emptied:%d (1: null 0: NO) \ n", Stackempty (s));
Destroystack (&s);
printf ("Destroy Stack, s.top=%u s.base=%u s.stacksize=%d\n", S.top,s.base, s.stacksize); }