Implementation of sequential stacks

Source: Internet
Author: User

The following functions are implemented for the sequential stack:

void Meau ();//Menu function void Initstack (stack *st);//Initialize stack bool Isfull (stack *st);//Determine if stack is full bool IsEmpty (stack *st);// Determine if the stack is empty bool Push (stack *st,elemtype x),//into the stack bool Pop (stack *st,elemtype *x),//Stack int lenth (stack *st),//The length of the stack bool GetTop ( Stack *st,elemtype *x); <span style= "White-space:pre" ></span>//get stack top element void Showstack (stack *st);          <span style= "White-space:pre" ></span>//Print List contents void Clear (Stack *st);//Empty stack void destory (stack *st);//Destroy Stack

stack.h:

#ifndef __stack_h__#define __stack_h__#include<assert.h>//assert#include<stdlib.h>//malloc#include <iostream>//cin coutusing namespace std; #define STACK_DEFAULT_SIZE 10//stack default size typedef int ELEMTYPE;TYPEDEF struct Stack{elemtype *base;int top;int capacity;} Stack;void Meau ();//Menu function void Initstack (stack *st);//Initialize stack bool Isfull (stack *st);//Determine if stack is full bool IsEmpty (stack *st);// Determine if the stack is empty bool Push (stack *st,elemtype x),//into the stack bool Pop (stack *st,elemtype *x),//Stack int lenth (stack *st),//The length of the stack bool GetTop ( Stack *st,elemtype *x); <span style= "White-space:pre" ></span>//get stack top element void Showstack (stack *st);          <span style= "White-space:pre" ></span>//Print List contents void Clear (Stack *st);//Empty stack void destory (stack *st);//Destroy Stack # endif

Stack.cpp

#include "stack.h"/* Initialize stack */void initstack (Stack *st) {elemtype *s = (Elemtype *) malloc (sizeof (elemtype) * Stack_default_ SIZE); assert (s! = NULL); st->base = s;st->capacity = Stack_default_size;st->top = 0;} /* Determine if the stack is full */bool isfull (stack *st) {return st->top = = st->capacity;} /* Determine if the stack is empty */bool IsEmpty (stack *st) {return st->top = = 0;} /* Enter stack */bool push (stack *st,elemtype x) {if (Isfull (ST)) {cout<< "The stack is Full,can ' t push anymore!" <<endl;return false;} st->base[st->top++] = X;return true;} /* Out Stack */bool Pop (Stack *st,elemtype *x) {if (IsEmpty (ST)) {cout<< "The stack is empty!" <<endl;return false;} *x = St->base[--st->top];return true;} /* Calculate the length of the stack */int lenth (stack *st) {return st->top;} /* Get stack top element */bool GetTop (Stack *st,elemtype *x) {if (IsEmpty (ST)) {cout<< "The stack is empty!" <<endl;return false;} *x = St->base[--st->top];return true;} /* Print List Contents */void showstack (Stack *st) {for (int i = st->top-1;i>=0;--i) {cout<<st->base[i]<< "---> ";} cout<< "NULL" &LT;&LT;ENDL;} Empty stack void Clear (Stack *st) {st->top = 0;st->capacity = 0;} Destroys stack void destory (Stack *st) {Clear (ST); free (st->base); st->base = NULL;}                                 menu function void Meau () {cout<< "*********************seqstack**********************" <<endl;cout<< "* Zyh_helen * "<<endl;cout<<" * [1]push [2]pop [3]lenth] [4]gettop * "<& lt;endl;cout<< "* [5]showstack [6]clear *" &LT;&LT;ENDL;}


Main.cpp:

#include "stack.h" int main () {stack st; Elemtype Item;int select = 1;initstack (&st); while (select) {Meau ();cout<< "Please choose the num of function:" <<endl;cin>>select;switch (Select) {case 1:cout<< ' input the item you want to input ( -1 as a end) "<< Endl;while (Cin>>item,item! =-1) {Push (&st,item);} Break;case 2:pop (&st,&item); Cout<<item<<endl;break;case 3:cout<< "The length of the stack is:" <<lenth (&st) <<endl;break;case 4:gettop (&st,&item);cout<< "The top item is:" << Item<<endl;break;case 5:showstack (&st); Break;case 6:clear (&st); break;default:break;}} Destory (&st); return 0;}



specific functions: hope that the reader self-test, if there is a mistake welcome to propose changes in opinion ----->>>zyh_helen


Implementation of sequential stacks

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.