Application of "Data structure" stack--line editing program (c + +)

Source: Internet
Author: User

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 ();p ublic: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);p rivate: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;ba SE = new Type[capacity];assert (base! = NULL); top = 0;} Template<class Type>seqstack<type>::~seqstack () {destory ();} Determine if the stack is full template<class type>bool seqstack<type>::full () Const{return (top >= capacity);} Determine if the empty stack template<class type>bool seqstack<type>::empty () const{return (top = = 0);} Show 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;}} Into the stack template<class type>bool seqstack<type>::p ush (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>::p op () {if (empty ()) {cout << "the stack is Empty,can not PO p! "<< Endl;return false;} Else{top--;return true;}} Get stack top element template<class type>void seqstack<type>::gettop (Type &x) {x = base[top-1];} Find the stack length template<class type>int seqstack<type>::length () Const{return top;} Empty stack template<class type>void seqstack<type>::clear () {top = 0;} Destroy Stack Template<class type>void seqstack<type>::d estory () {delete[]base;base = null;capacity = top = 0;} Exit 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 <& Lt "*  @:d elete all    $:d elete One  *" << endl;while ((ch = getchar ())! = EOF) {switch (ch) {case ' @ ': mystack.cl Ear (); cout << "Please enter:" << endl;break;case ' $ ': Mystack.pop (), Break;default:mystack.push (ch); break;}} Mystack.show (); return 0;}





Application of "Data structure" stack--line editing program (c + +)

Related Article

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.