【資料結構】棧的應用--行編輯程式(c++),資料結構--

來源:互聯網
上載者:User

【資料結構】棧的應用--行編輯程式(c++),資料結構--

標頭檔:



#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();}// 判斷棧是否滿了template<class Type>bool SeqStack<Type>::full()const{return (top >= capacity);}// 判斷是否為空白棧template<class Type>bool SeqStack<Type>::empty()const{return (top == 0);}// 顯示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;}}// 入棧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;}}// 出棧template<class Type>bool SeqStack<Type>::pop(){if (empty()){cout << "the stack is empty,can not pop!" << endl;return false;}else{top--;return true;}}// 獲得棧頂元素template<class Type>void SeqStack<Type>::gettop(Type &x){x = base[top - 1];}// 求棧長度template<class Type>int SeqStack<Type>::length()const{return top;}// 清空棧template<class Type>void SeqStack<Type>::clear(){top = 0;}// 摧毀棧template<class Type>void SeqStack<Type>::destory(){delete[]base;base = NULL;capacity = top = 0;}// 退出系統template<class Type>void SeqStack<Type>::quit_system(Type &x){x = 0;}


主函數:



#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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.