Definition of stacks
Stack , also known as a stack, is a linear table with limited operations, and its limitation is that only one end of the table is allowed to insert and delete operations.
Stacks are implemented in two ways. One is sequential storage, and arrays are similar; one is chained and similar to a single-linked list.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/D0/wKiom1cJAafxAH8-AABXBkMBS7I061.png "title=" Qq20160409212003.png "alt=" Wkiom1cjaafxah8-aabxbkmbs7i061.png "/>
The following mainly in order to achieve its storage, and chain storage, compared to the following several advantages:
(1) to facilitate our management; An array is just one side of the operation.
(2) The efficiency of sequential storage is higher. If the use of chained storage, in the insertion and deletion of operations, the need to traverse the entire list, a waste of time, and one point is that the CPU cache utilization is low, each operation into the high-level cache load will add some unused space, not only a waste of time, but also easy to "pollute" memory.
2. The main function interface of the stack
void Push (const T & S);//Insert Data void Pop ();//delete data bool empty ();//Determine if empty size_t size ();//number of elements T & Top ();// Remove the last entry element without deleting void print ();//Output function
3. Definition of stacks
template <typename t>//is defined as a template class that implements different data storage class stack{ public: stack ()//constructor: _ptr (NULL), _top (0), _capacity (0) {}stack (const stack<t> & s)//copy construction {_capacity = s._capacity;_top = s._top;_ptr = new T[_capacity];for (size_t i = 0; i < _top; i++ //cannot use the memcpy function, (must consider a shallow copy of the //problem) { _ptr[i] = s._ptr[i];}} Stack <t> & operator= (const stack<t> & s)//overloading of the assignment operator {if (_capacity < s._capacity)//Determine if the capacity is sufficient { _caPacity = s._capacity; _ptr = new t[_capacity];} _top = s._top;for (size_t i = 0; i < _top; i++) { _ptr[i] = s._ptr[i];} Return *this;} ~stack ()//destructor {if (_ptr) { delete _ptr;}} protected:t * _ptr;//the number of data stored size_t _top;//the pointer that holds the data size_t _capacity ;//Capacity opened}
4. Implementation of the interface
void Push (const T & S)//Insert Data {_check_capacity ();//Use this function to determine if space _ptr[_top] = s;_top++;} void Pop ()//delete data {if (_top)//must guarantee that the number of data cannot be negative {_top--;}} bool Empty ()//empty {return _top==0;} size_t size ()//number of data {return _top;} T & Top ()//output last inserted data {if (!empty ()) {return _ptr[_top-1];}} void print ()//output {for (size_t i = _top; i > 0; i) {cout << _ptr[i-1] << "";} cout << Endl;}
5. Test results
void Teststack () {stack<int> s1;s1. Push (0); S1. Push (1); S1. Push (2); S1. Push (3); S1.print ();//Output Result 3 2 1 0s1. Pop (); S1.print ();//Output Result 2 1 0cout << s1.empty () << endl;//output 0cout << s1.size () << endl;//output 3c Out << S1. Top () << endl;//output 2}
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/CD/wKioL1cJB5mgyEuuAAAP5gBRHS8081.png "title=" Qq20160409214217.png "alt=" Wkiol1cjb5mgyeuuaaap5gbrhs8081.png "/>
This article is from the "Progressive Space" blog, make sure to keep this source http://10824050.blog.51cto.com/10814050/1762161
Basic operation of C + + implementation stack