c++實現棧

來源:互聯網
上載者:User

標籤:元素   public   線性表   

棧的概念

棧是資料結構中一種特殊的線性表,它的基本特性是“先入後出,後入先出”。如:


650) this.width=650;" title="圖片1.png" alt="wKiom1cJJX6QZftdAAAd2u7FNBg489.png" src="http://s5.51cto.com/wyfs02/M02/7E/D1/wKiom1cJJX6QZftdAAAd2u7FNBg489.png" />

棧的基本操作

棧有基本操作如下:

void Push(const T& d);//入棧
 void Pop();//出棧
 T& Top();//返回棧頂元素
 bool Empty();//判斷是否為空白棧
 size_t Size();//棧中元素個數


棧的實現

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;
  _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>::Push(const T& d)
{
 _CheckCapacity();
 _a[_top++] = d;
}

template<class T>
void Stack<T>::Pop()
{
 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;
}

運行結果:

650) this.width=650;" title="圖片2.png" src="http://s3.51cto.com/wyfs02/M01/7E/CF/wKioL1cJL-DCe_dBAAA71QV3epI560.png" alt="wKioL1cJL-DCe_dBAAA71QV3epI560.png" />

c++實現棧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.