C + + implementation stack

Source: Internet
Author: User

The concept of stacks

Stack is a special kind of linear table in data structure, its basic characteristic is "first-in, after-first-out". Such as:


650) this.width=650; "title=" Picture 1.png "alt=" Wkiom1cjjx6qzftdaaad2u7fnbg489.png "src=" http://s5.51cto.com/wyfs02/M02 /7e/d1/wkiom1cjjx6qzftdaaad2u7fnbg489.png "/>

Basic operation of the stack

The stack has the following basic operations:

void Push (const t& D);//Into the stack
void Pop ();//OUT Stack
t& top ();//return top element of stack
bool Empty ();//Determine if it is an empty stack
size_t Size ();//The number of elements in the stack


Implementation of the Stack

Stack.hpp


#pragma once
#include <iostream>

using namespace Std;

Template<class T>
class Stack
{
Public:
 stack ();
 ~stack ();
 stack (const stack<t>& s);
 Stack<T>& operator= (stack<t> s);
 void Push (const t& D);
 void Pop ();
 T& Top ();
 bool Empty ();
 size_t size ();
Protected:
 void _checkcapacity ();
Protected:
 t* _a;
 size_t _top;
 size_t _capacity;
};
Template<class t>
void stack<t>::_checkcapacity ()
{
 if (_a = = NULL)
 {
  _capacity = 3;
  _a = new T[_capacity];
  return;
 }
 if (_top = = _capacity)
 {
  _capacity + = 3;
  t* tmp = new t[_capacity];
  for (size_t i = 0; i < _top; i++)
  {
   tmp[i] = _a[i];
  }
  delete[] _a;
&NBSP;&NBSP;_A = tmp;
 }
}

Template<class t>
Stack<t>::stack ()
: _a (0)
, _top (0)
, _capacity (0)
{}

Template<class t>
Stack<t>::~stack ()
{
if (_a)
{
Delete[] _a;
}
}
Template<class t>
Stack<t>::stack (const stack<t>& s)
: _top (S._top)
, _capacity (s._capacity)
{
_a = new T[_capacity];
for (size_t i = 0; i < _top; i++)
{
_a[i] = S._a[i];
}

}

Template<class t>
stack<t>& stack<t>::operator= (stack<t> s)
{
Swap (S._A,_A);
_top = S._top;
_capacity = s._capacity;
return *this;
}
Template<class t>
stack<t>& stack<t>::operator= (const stack<t>& s)
//{
if (This! = &s)
// {
Delete[] _a;
_capacity = s._capacity;
_top = S._top;
_a= New t[_capacity];
for (size_t i = 0; i < _top; i++)
//  {
_a[i] = S._a[i];
//  }
// }
return *this;
//}

Template<class t>
void Stack<t>::P ush (const t& D)
{
_checkcapacity ();
_a[_top++] = D;
}

Template<class t>
void Stack<t>::P op ()
{
if (_top)
{
--_top;
}
}

Template<class t>
t& Stack<t>::top ()
{
return _a[_top-1];
}
Template<class t>
BOOL Stack<t>::empty ()
{
if (_top = = 0)
return true;
Else
return false;
}

Template<class t>
size_t stack<t>::size ()
{
return _top;
}


Test.cpp

#include "Stack.h"
#include <string>
int main ()
{
Stack<string> s;
S.push ("1111111111111");
S.push ("2222222222222");
S.push ("3333333333333");
S.push ("4444444444444");
S.push ("5555555555555");
stack<string> S1 (s);
Stack<string> S2;
S2 = s;
while (!S2. Empty ())
{
cout << S2. Top () << "";
S2. Pop ();
}
GetChar ();
return 0;
}

Operation Result:

650) this.width=650; "title=" Picture 2.png "src=" Http://s3.51cto.com/wyfs02/M01/7E/CF/wKioL1cJL-DCe_ Dbaaa71qv3epi560.png "alt=" Wkiol1cjl-dce_dbaaa71qv3epi560.png "/>

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.