//【數據結構】用C++編寫棧及基本操作(包括入棧,出棧,獲得棧頂,摧毀,清空等等) //頭文件 #ifndef _SEQ_STACK_ #define _SEQ_STACK_ #includeusing namespace std; template class SeqStack { public: SeqStack(size_t sz=INIT_SIZE) { capacity = sz > INIT_SIZE ? sz : INIT_SIZE; base = new Type[capacity]; top = 0; } ~SeqStack() { destory(); } public: bool empty() const //判斷是否為空 { return(top == 0); } bool full()const //判斷是否已滿 { return(top >= capacity); } void push(const Type &x) //進棧 { if (full() ) { cout << "棧已滿,不能插入。" << endl; return; } base[top++] = x; } void pop() //出棧 { top--; } bool getTop(Type &x) const //獲得棧頂 { if (top == 0) return false; x = base[top - 1]; return true; } int length() const //求大小 { return top; } void clear() //清除 { top = 0; } void destory() //摧毀 { delete[]base; base = NULL; capacity = top = 0; } void show() const //顯示 { if (empty() == 1) { cout << "棧為空" << endl; return; } for (int i=top-1; i >=0; i--) { cout << base[i]< mystack; int select = 1; int Item; while (select) { cout << "**************************************" << endl; cout << "* [1] show [2] push *" << endl; cout << "* [3] pop [4] length *" << endl; cout << "* [5] clear [6] destory *" << endl; cout << "**************************************" << endl; cout << "請選擇:>"; cin >> select; switch (select) { case 1: mystack.show(); break; case 2: cout << "請輸入要插入的值(-1結束):>"; while (cin >> Item, Item != -1) { mystack.push(Item); } break; case 3: mystack.pop(); break; case 4: cout << "大小為:" << mystack.length()<