[Data structure] stack applications-row editing program (c ++) and data structure --

Source: Internet
Author: User

[Data structure] stack applications-row editing program (c ++) and data structure --

Header file:



# Pragma once # include <iostream> # include <assert. h> using namespace std; template <class Type> class SeqStack {public: SeqStack (size_t sz = INIT_SZ );~ SeqStack (); public: bool empty () const; bool full () const; void show () const; bool push (const Type & x); bool pop (); void gettop (Type & x); int length () const; void clear (); void destory (); void quit_system (Type & x); private: enum {INIT_SZ = 64}; Type * base; int capacity; int top ;}; template <class Type> SeqStack <Type >:: SeqStack (size_t sz = INIT_SZ) {capacity = sz> INIT_SZ? Sz: INIT_SZ; base = new Type [capacity]; assert (base! = NULL); top = 0;} template <class Type> SeqStack <Type> ::~ SeqStack () {destory () ;}// determine whether the stack is full. <class Type> bool SeqStack <Type >:: full () const {return (top >= capacity);} // determines whether the stack template is empty. <class Type> bool SeqStack <Type >:: empty () const {return (top = 0);} // display template <class Type> void SeqStack <Type >:: show () const {if (top = 0) {cout <"the stack is empty! "<Endl; return;} for (int I = top-1; I> = 0; -- I) {cout <base [I] <endl ;}} // the inbound stack template <class Type> bool SeqStack <Type>: push (const Type & x) {if (full () {cout <"the stack is full, can not enter! "<Endl; return false;} else {base [top] = x; top ++; return true ;}} // out-of-stack template <class Type> bool SeqStack <Type>: pop () {if (empty () {cout <"the stack is empty, can not pop! "<Endl; return false;} else {top --; return true ;}// obtain the top-stack element template <class Type> void SeqStack <Type> :: gettop (Type & x) {x = base [top-1] ;}// evaluate the stack length template <class Type> int SeqStack <Type >:: length () const {return top;} // clear the stack template <class Type> void SeqStack <Type >:: clear () {top = 0 ;} // destroy the stack template <class Type> void SeqStack <Type>: destory () {delete [] base; base = NULL; capacity = top = 0 ;} // exit the system template <class Type> void SeqStack <Type >:: quit_system (Type & x) {x = 0 ;}


Main function:



#include "Edlin.h"int main(){SeqStack<char> mystack;char ch;cout << "please enter:" << endl;cout << "*  @:delete all    $:delete one  *" << endl;while ((ch = getchar()) != EOF){switch (ch){case '@':mystack.clear();cout << "please enter:" << endl;break;case '$':mystack.pop();break;default:mystack.push(ch);break;}}mystack.show();return 0;}





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.