Stack constructor

xiaoxiao2021-03-06  39

Stack is a special linear table, and their logical structure and linear meter are the same, but their calculation rules have more limitations, so they are also known as linear gauges for operations. Stacks are widely used in various programming. Stack definition and basic operation

1. The stack definition stack is a linear table that limits only inserting and deleting an operation only on the table. (1) Usually, the end of the insertion, the deletion is the top of the stack, and the other end is called the bottom (Bottom). (2) referred to as empty stack when there is no element in the table. (3) The linear table of the backward first out out, referred to as the LIFO table. The modification of the stack is carried out according to the principles of the advancement. Each time you delete (decorned), the "latest" element in the current stack, that is, the element is inserted (in the stack), and the first inserted is the bottom of the stack, to finally delete. 2, the basic operation of the stack (1) INITSTACK (s) constructs an empty stand S. (2) DestroyStack (s) destroys a stack that exists. (3) ClearsTack (s) Sets the presence of a stack. (4) STACKEMPTY (S) begins with an air. If S is an empty stack, returns true, otherwise returns false. (5) Stackfull (s) arrest. If S is a full stack, returns true, otherwise returns false.

Note: This operation is only applicable to the order of the stack. (6) PUSH (S, X) inrest. If the stack S is dissatisfied, the element X is inserted into the top of S. (7) POP (S, E) is quenched. If the stack S is non-empty, then the top elements of S are deleted and the element is returned to E. (8) StackTOP (S, E) take the top elements of the stack. If the stack S is non-empty, return to the top element in E, but does not change the state of the stack. (9) StackTraverse (S, Visit ()) stack Visit method traverses.

/***************************

#include "stdlib.h" #include "typedef.h"

Typedef int status; typedef char class;

Typedef struct stack {selemtype * top; int stcksize;} SQSTACK;

Status InitStack (SqStack * S); Status DestroyStack (SqStack * S); Status ClearStack (SqStack * S); Status StackEmpty (SqStack S); Status StackFull (SqStack S); int StackLength (SqStack S); SElemType GetTop (SqStack S ); Status push (Sqstack * S, SELEMTYPE E); STATUS POP (Sqstack * S, SELEMTYPE * E); Status StackPop (Sqstack * S, SELEMTYPE * E); STATUS StackTraverse (SQSTACK S, STATUS (* Visit) () ); Status initstack (SQSTack * s) {s-> base = (SELEMTYPE *) Malloc (STACK_INIT_SIZE * SIZEOF (SELEMTYPE)); if (! S-> Base) EXIT (OVERFLOW); S-> TOP = S-> Base; S-> stacksize = stack_init_size; return ok;} status destroystack (SQSTACK * S) {free (s); free (s);} status clearStack (SQSTACK * S) {S-> TOP = S- > base;} status stackempty (s.top == s.base) Return true; else return false;} status stackfull (SQSTACK S) {IF ((S. (S. (TOP S-> stacksize) == S.BASE) && (S.TOP == S.BASE)) Return True; Else Return False;} int stacklength (sqstack s) {INT i; selectpe * p; i = 0; p = s.base; While (p! = S.top) {p ; i ;} returni} selemtype gett OP (Sqstack S) {if (S.TOP == S.Base) Return Error; Return * (S.TOP-1);} 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) Exit (overflow); S-> TOP = S-> Base S-> Stacksize; S-> stacksize = stackincrement;} * (S-> TOP ) = E; RETURN OK;} Status Pop (Sqstack * S, SELEMTYPE * e) {if (S-> TOP == S-> Base) Return Error; * E = * (- (S-> TOP)); returno} status stacktop (Sqstack * S, SELEMTYPE * E) {IF (S->

TOP == S-> Base) Return Error; * E = * (S-> TOP); Return OK;} Status StackTraverse (Sqstack S, STATUS (* VIS) ()) {while (S.TOP> S.BASE Visit (- S.TOP);} / *********************** TYPEDEF.H ********** ******* /

#define TRUE 1 # define FALSE 0 # define OK 1 # define ERROR 0 # define INFEASIVLE -1 # define OVERFLOW -2 # define LIST_INIT_SIZE 100 # define LISTINCREMENT 10 # define EQUAL 1 # define STACK_INIT_SIZE 100 # define STACKINCREMENT 10 # define MAX 100 #define maxSize 100

转载请注明原文地址:https://www.9cbs.com/read-69393.html

New Post(0)