"Data Structure" in C + + writing stacks and basic operations (including stack, stack, get stack top, destroy, empty, etc.)

Source: Internet
Author: User

"Data Structure" in C + + writing stacks and basic operations (including into the stack, out of the stack, get stack top, destroy, empty, etc.)//header file #ifndef _seq_stack_#define _seq_stack_#include <iostream> Using namespace Std;template <class type>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//Determines whether NULL {return (top = = 0);} BOOL Full () const//determines if the {return (top >= capacity) has been filled;} void push (const Type &AMP;X)//Enter stack {if (full ()) {cout << "stack is filled, cannot be inserted. "<< Endl;return;} base[top++] = x;} void pop ()//out stack {top--;} BOOL GetTop (Type &x) const//Get stack top {if (top = = 0) Return false;x = Base[top-1];return true;} int length () const//Seek size {return top;} void clear ()//clear {top = 0;} void Destory ()//Destroy {Delete[]base;base = null;capacity = top = 0;} void Show () const//Display {if (empty () = = 1) {cout << "stack is empty" << endl;reTurn;} for (int i=top-1; I >=0; i--) {cout << Base[i]<<endl;}} private:enum {init_size = 8}; Type *base;int capacity;int top;}; #endif//Main function # include "SeqStack.h" void Main () {seqstack<int> mystack;int select = 1;int item;while (select) {cout << "**************************************" << endl;cout << "* [1] show [2] push *" &lt ;< endl;cout << "* [3] pop [4] length *" << endl;cout << "* [5] clear [6] D Estory * "<< endl;cout <<" ************************************** "<< endl;cout <<" Please select:> "; c In >> select;switch (select) {case 1:mystack.show (); Break;case 2:cout << "Enter the value to be inserted (-1 end):>" while (CIN >> Item, Item! =-1) {Mystack.push (item);} Break;case 3:mystack.pop (); Break;case 4:cout << "size:" << mystack.length () <<endl;break;case 5: Mystack.clear (); break;case 6:mystack.destory (); break;default:break;}} return;} 

"Data Structure" in C + + writing stacks and basic operations (including stack, stack, get stack top, destroy, empty, etc.)

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.