Basic operation of C + + implementation stack

Source: Internet
Author: User
Tags shallow copy


    1. 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

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.