Self-built Algorithm Library of Data Structure-sequential stack and self-built algorithm of Data Structure
This article focuses on the basic series of network courses on data structures (3): sequential storage structure and basic operation implementation of the 3rd class stacks in stacks and queues.
Follow the suggested methods in [Video] section of "0207 program algorithm change" to build your own professional infrastructure algorithm library.
The algorithm library of the sequential stack uses the multi-file organization form of the program, including two files:
1. header file: sqstack. h, which contains the code, macro definition, and Declaration of functions to implement the algorithm for defining the data structure of the sequential stack;
# Ifndef SQSTACK_H_INCLUDED # define MaxSize 100 typedef char ElemType; typedef struct {ElemType data [MaxSize]; int top; // Stack pointer} SqStack; // define the void InitStack (SqStack * & s) for the sequence stack type; // initialize the stack void DestroyStack (SqStack * & s); // destroy the stack bool StackEmpty (SqStack * s ); // whether the stack is empty int StackLength (SqStack * s); // return the number of elements in the stack -- stack length bool Push (SqStack * & s, ElemType e ); // inbound bool Pop (SqStack * & s, ElemType & e); // outbound stack bool GetTop (SqStack * s, ElemType & e ); // obtain the top Data Element void DispStack (SqStack * s); // output stack # endif // SQSTACK_H_INCLUDED
2. source file: sqstack. cpp, including the definition of functions that implement various algorithms
# Include <stdio. h> # include <malloc. h> # include "sqstack. h "void InitStack (SqStack * & s) {s = (SqStack *) malloc (sizeof (SqStack); s-> top =-1 ;} void DestroyStack (SqStack * & s) {free (s);} int StackLength (SqStack * s) // return the number of elements in the stack -- stack length {return (s-> top + 1);} bool StackEmpty (SqStack * s) {return (s-> top =-1);} bool Push (SqStack * & s, ElemType e) {if (s-> top = MaxSize-1) // when the stack is full, return false upon Stack Overflow; s-> top ++; s-> data [s-> top] = e; return true ;} bool Pop (SqStack * & s, ElemType & e) {if (s-> top =-1) // if the stack is empty, return false if the stack overflows; e = s-> data [s-> top]; s-> top --; return true;} bool GetTop (SqStack * s, ElemType & e) {if (s-> top =-1) // if the stack is empty, return false is exceeded under the stack; e = s-> data [s-> top]; return true;} void DispStack (SqStack * s) // output stack {int I; for (I = s-> top; I> = 0; I --) printf ("% c", s-> data [I]); printf ("\ n ");}
3. Create a source file (such as main. cpp) in the same project, compile the main function, and complete related testing. Example:
# Include <stdio. h> # include "sqstack. h "int main () {ElemType e; SqStack * s; printf (" (1) initialize stack s \ n "); InitStack (s); printf (" (2) STACK: % s \ n ", (StackEmpty (s )? "Null": "non-empty"); printf ("(3) stack elements a, B, c, d, e \ n"); Push (s, 'A'); Push (s, 'B'); Push (s, 'C'); Push (s, 'd); Push (s, 'E'); printf ("(4) STACK: % s \ n", (StackEmpty (s )? "Null": "Not empty"); printf ("(5) stack length: % d \ n", StackLength (s); printf ("(6) elements from the top of the stack to the bottom of the stack: "); DispStack (s); printf (" (7) Output stack sequence: "); while (! StackEmpty (s) {Pop (s, e); printf ("% c", e);} printf ("\ n"); printf ("(8) STACK: % s \ n ", (StackEmpty (s )? "Null": "Not empty"); printf ("(9) Release stack \ n"); DestroyStack (s); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.