Stack: linear structure, LIFO. A stack is a special linear table (sequential table, linked list) that deletes and inserts only at the end of a table.
Note: For stacks, the footer is called the stack Top (top) of the stack, and the table header is called the bottom of the stack (bottom).
Initialize, destroy, determine if empty, calculate the length of the stack, empty the stack, push data into the stack, eject the data stack, won These kinds of operations. where
After understanding the basic structure and operation of the stack, it is natural to think of
because the nature of the stack is a linear table, the linear table has two forms of storage, then the stack is divided into The sequential storage structure of stacks and the chain storage structure of stacks.
The first stack contains no data, called an empty stack, at which point the top of the stack is the bottom. Then the data is entered from the top of the stack, the stack top stack is separated, and the current capacity of the stack becomes larger. The data is ejected from the top of the stack when it is out of the stack, the stack top is down, and the current capacity of the stack becomes smaller.
Top of stack--high address;
bottom of stack--low address.
Defines a sequential stored stack that contains three elements: base, Top, stackSize.
typedef struct{elemtype *base;//Stack bottom elemtype *top;//stack top int stacksize;//stack currently usable maximum capacity}sqstack;
Or:
typedef int ELEMTYPE; typedef struct{elemtype Data[maxsize];int top;//is used to label the top position of the stack int stackSize;}
where base is the pointer variable at the bottom of the stack, top refers to the pointer variable at the top of the stack, and stacksize refers to the maximum capacity currently available for use by the stack.
Create a stack:
#define Stack_init_size Initstack (Sqstack *s) {s->base = (Elemtype *) malloc (stack_init_size * sizeof (elemtype)); I F (!s->base) exit (0); S->top = s->base; At first, the top of the stack is the bottom s->stacksize = stack_init_size;}
into the stack operation:
The stack operation is called the stack operation, which is to store the data in the stack.
The stack operation is performed at the top of the stack, and each time a data is pressed into the stack, the top pointer will be +1 until the stack is full.
The stack operation:
The stack operation is to take out the data at the top of the stack and move the top of the stack with the pointer.
whenever a data is popped from the stack, the current capacity of the stack is 1.
Into the stack, the value is pressed into the stack, then change the pointer top, the top pointer will be added 1;
When the stack is out, subtract the top pointer by 1, and then remove the value pointed to by the top pointer from the stack;
Note: The position of the top pointer at the beginning is actually where the data will be stored, so the area it points to is empty and you want to take the data out of the stack by first pointing the top pointer to that position.
Pop (Sqstack *s, Elemtype *e) {if (s->top = = s->base) //Stack is empty return;*e = *--(s->top);}
Stack of other operations: empty, Destroy, calculate the current capacity of the stack, and so on.
1, empty: empty A stack, is the stack of elements are all obsolete, but the stack itself physical space does not change (not destroy).
So all we have to do is assign the contents of the S->top to s->base so that s->base equals s->top, which means the stack is empty.
This principle is the same as advanced formatting but simply emptying the file list without overwriting the hard disk.
Clearstack (sqstack*s) {s->top= s->base;}
2.destroying a stack
Destroying a stack is different from emptying a stack, destroying a stack to free up the physical memory space occupied by the stack, so don't confuse the destruction of a stack with emptying a stack.
Destroystack (Sqstack *s) {int i, len; len = s->stacksize; for (i=0; i < Len; i++) {free (s->base); s->base++;} S->base = S->top = Null;s->stacksize = 0;}
3,
The current capacity of the calculation stack
The current capacity of the calculation stack is the number of elements in the stack, so just return s.top-s.base.
Note that the maximum capacity of the stack is that the stack occupies the size of the memory space, the value of which is s.stacksize, and the current capacity of the stack is not a concept oh.
int Stacklen (Sqstack s) {return (s.top–s.base);//Two address subtraction, returns the number of data stored at that address}
(The mathematical calculation of the pointer: + + 、--、 subtraction, but the pointer does not add!) )
Chained storage structure for stacks (stack chain)
Stack because it is just the top of the stack to do the insert and delete operations, so the better way is to put the top of the stack on the head of a single-linked list, the top of the stack pointer and the single-linked list of the head pointer.
The chain storage structure of the stack (stack chain) teypedef struct Stacknode{elemtype data;//the data struct Stacknode *next;} Stacknode, *linkstackptr;teypedef struct linkstack{linkstackprt top;//top pointer int count; Stack element counter}//for the push operation of the stack chain, assuming that the new node with element value E is s,top for the stack top pointer status push (Linkstack *s, Elemtype e) {linkstackptr p = (linkstackptr) malloc (sizeof (Stacknode));p->data = E;p->next = S->top;s->top = P;s->count++;return OK;} The stack pop operation, assuming that the variable p is used to store the top node of the stack to be deleted, move the stack top pointer down one bit, and finally release p. Status Pop (Linkstack *s, Elemtype *e) {linkstackptr p;if (Stackempty (*s)) //Determine if empty stack return ERROR; *e = s->top->d Ata;p = S->top;s->top = S->top->next;free (p);s->count--; return OK;}
Chained storage structure for stacks and stacks (stack chain)