Example of implementation of chain stack

Source: Internet
Author: User

Examples of chained implementations of stacks

#ifndef stack_h_included #define stack_h_included #include "ds.h"//for status,ok ... #ifndef elemtype #define ELEMTYPE INT/* Data element type defaults to int */#define ELEMTYPE_TAG #endif///////////////////////////////////////    
    The storage structure of the chain stack defines the typedef struct LNODE {elemtype data;    
struct Lnode *next;    
        
} Lnode, *linklist; typedef linklist Linkstack;     
Chain stack type/////////////////////////////////////////////////////////////chain Stack basic Operation statement//construct an empty stack s    
Status Initstack (Linkstack &s);    
Destroy Stack S Status destroystack (Linkstack &s);    
The stack S is emptied of the Status clearstack (Linkstack &s);    
False Status Stackempty (linkstack s) if stack S is null to return true;    
Returns the number of elements in the stack s int stacklength (Linkstack s);    
Return top element//premises with E: Stack s exists and is not empty Status GetTop (linkstack s, Elemtype &e);    
Element e into the stack S Status Push (linkstack &s, elemtype e);  s out stack with e back out stack elements//Prerequisites: Stack s exist and not empty   
Status POPs (Linkstack &s, Elemtype &e); The implementation of the basic operation of the chain stack//construct an empty stack s Status initstack (    
    Linkstack &s) {//TODO (#1 #): Constructs an empty stack S, does not take the lead node s= (linkstack) malloc (sizeof (Lnode)); if (!    
    S) return ERROR;    
    s->next=null;    
    return OK;    
    -------------------------------------}//Destroy Stack S Status destroystack (Linkstack &s) { TODO (#1 #): Destroy Stack S, equivalent to empty stack
http://www.bianceng.cn Linkstack p; while (S) {p=s->next; Free (S); S=p; return OK; -------------------------------------}///empty stack S Status clearstack (Linkstack &s) { TODO (#1 #): Empty stack s, releasing all nodes Linkstack p,q; p=s->next; while (p) {q=p->next; Free (p); p=q; } s->next=null; return OK; -------------------------------------}//If stack S is null to return True, False Status Stackempty (Linkstack s) { TODO (#1 #): If stack S is null returns TRUE, False if (s==null) return 1; else return 0; -------------------------------------}//Returns the number of elements in stack s int stacklength (Linkstack s) { TODO (#1 #): Returns the number of elements in the stack s, Linkstack p; int x=0; p=s->next; while (P!=null) { x + +; pp=p->next; return x; -------------------------------------}//return top of stack element//premises with E: Stack s exists and is not empty Status GetTop (Linkstack s, Elemtype &e) {//TODO (#1 #): If stack S is not empty, return the top element of the stack with E
if (s==null) exit (OVERFLOW); e=s->data; return OK; -------------------------------------}//element e into Stack S Status Push (linkstack &s, Elemtype e) { TODO (#1 #): Inserts element e as the new stack top Linkstack p; p= (linkstack) malloc (sizeof (Lnode)); if (!p) exit (OVERFLOW); p->data=e; p->next=null; p->next=s->next; s->next=p; return OK; -------------------------------------}//s stack with e return stack element/premise: stack s exists and not empty Status POPs (Linkstack & Amp 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==null) returns ERROR; e=s->data; ss=s->next; return OK; -------------------------------------} #ifdef elemtype_tag #undef elemtype #undef ELE Mtype_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.