Self-built Algorithm Library of Data Structure-sequential stack and self-built algorithm of Data Structure

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.