Implementation examples for sequential stacks

Source: Internet
Author: User
Tags empty

Sequence implementation examples of stacks

#ifndef stack_h_included #define stack_h_included #include "ds.h"//for status,ok ... #ifndef elemtype #d efine Elemtype INT/* Data element type default to int */#define ELEMTYPE_TAG #endif #define SELEMTYPE elemtype//////////// The storage structure of the sequential stack is defined #define STACK_INIT_SIZE 100/* Storage space Initial allocation capacity * * #de Fine stackincrement 10/* Storage space allocation increment * * typedef struct {elemtype *base;//The base is null before the stack is constructed and after the stack is destroyed ELEMT Ype *top; stack top pointer int stacksize;    
The currently allocated storage space (number of elements)} Sqstack; The basic operation statement of the sequential stack//construct an empty stack s Status initstack (Sqstack & Amp    
S);    
Destroy Stack S Status destroystack (Sqstack &s);    
The stack S is emptied of the Status clearstack (Sqstack &s);    
False Status Stackempty (sqstack s) if stack S is null to return true;    
Returns the number of elements in the stack s int stacklength (Sqstack s);  Return top element//premises with E: Stack s exists and is not empty Status GetTop (sqstack s, Elemtype &e);  
Element e into the stack S Status Push (sqstack &s, elemtype e);    
        
s out stack with e back out stack elements//premises: Stack S exist and not empty Status POPs (Sqstack &s, Elemtype &e); The implementation of the basic operation of the sequential stack//construct an empty stack s Status initstack    
(Sqstack &s)    
    {//TODO (#1 #): Constructs an empty stack S s.base= (elemtype*) malloc (stack_init_size * sizeof (elemtype)); if (!    
    s.base) exit (OVERFLOW);    
    Ss.top=s.base;    
    S.stacksize=stack_init_size;    
    return OK; -------------------------------------}//Destroy Stack S Status destroystack (sqstack &s) {/    
    /TODO (#1 #): Destroy Stack S, equivalent to empty stack free (s.base);    
    S.base=null;    
    return OK;  -------------------------------------}///empty stack S Status clearstack (sqstack &s) {//    
    TODO (#1 #): Empty stack s, releasing all nodes Elemtype E;    
    while (Pop (s,e)!=error);    
    return OK; //-------------------------------------}//If stack S is null to return True, False Status Stackempty (Sqstack s) {//TODO (#1 #): If Stack s    
    Returns true for NULL, otherwise False if (s.top==s.base) return OK;    
    return ERROR;  -------------------------------------}//Returns the number of elements in stack s int stacklength (Sqstack s) {//    
    TODO (#1 #): Returns the number of elements in the stack S return s.top-s.base; -------------------------------------}//return top of stack element//premises with E: Stack s exists and is not empty Status GetTop (sqstack s, E    
    Lemtype &e) {//TODO (#1 #): If the stack S is not empty, return the top element of the stack with E if (s.top ==s.base) returns ERROR;    
    e=* (s.top-1);    
    return OK;    
    -------------------------------------}//element e into Stack S Status Push (sqstack &s, Elemtype e) { TODO (#1 #): Inserts element e as the new stack top if (s.top-s.base>=s.stacksize) {s.base= (Elemtype *) realloc    
        (S.base, (s.stacksize+stackincrement) *sizeof (elemtype)); If!    
        s.base) exit (OVERFLOW);    
        Ss.top=s.base+s.stacksize;    
    S.stacksize+=stackincrement;    
    } *s.top++=e;    
    *s.top=e;    
    s.top++;    
    return OK; -------------------------------------}//s stack with e return stack element/premise: stack s exists and not empty Status POPs (Sqstack &am P    
    S, Elemtype &e) {//TODO (#1 #): If the stack S is not empty, then delete the top element of the stack with e return if (s.top==s.base) returns ERROR;    
    E=*--s.top;    
    return OK;     //-------------------------------------
www.bianceng.cn} #ifdef elemtype_tag #undef elemtype #undef elemtype_tag #endif #endif

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.