C++数据结构之栈模版实现

栈中的元素遵守“先进后出”的原则(LIFO,Last In First Out)

  只能在栈顶进行插入和删除操作

  压栈(或推入、进栈)即push,将数据放入栈顶并将栈顶指针加一

  出栈(或弹出)即pop,将数据从栈顶删除并将栈顶指针减一

  栈的基本操作有:pop,push,判断空,获取栈顶元素,求栈大小

 

定义:

template <typename T>

class ArrayStack {

private:

T *array;

int top;

int capacity;

public:

ArrayStack(const int m_size = 100):capacity(m_size), top(0){this->array = new T[capacity];}

ArrayStack(const T &data, const int m_size):capacity(m_size), top(1){this->array = new T[capacity], this->array[0] = data;}

ArrayStack(const ArrayStack<T> &stack);

~ArrayStack();

void clear();

bool isEmpty();

bool isFull();

int length();

void show();

bool GetTopNode(T &data);

bool GetBottonNode(T &data);

bool GetPosNode(T &data, int pos);

bool SetTopNode(const T &data);

bool SetBottonNode(const T &data);

bool SetPosNode(const T &data, int pos);

bool Push(const T &data);

bool Pop(T &data);

bool DelTopNode();

bool IncreaseStack(int len);

/*

function:find the position of node which equal to data in the list

data:the data which compared

return value:the position which equal to data;

this function need edit accoding the T

*/

int GetFirstNodePos(T &data);

/*

function:find the position of node which equal to data in the list

data:the data which compared

pos:the array which storage the position

return value:the num which equal to data;

this function need edit accoding the T

*/

int GetNodePos(T &data,int *pos);

};

成员函数:

复制构造函数

template <typename T>

ArrayStack<T>::ArrayStack(const ArrayStack<T> &stack)

{

this->top = stack.top;

this->capacity = stack.capacity;

this->array = new T[this->capacity];

ULOGW("arraystack is ", sizeof(T[this->capacity]), "this->capacity is ", this->capacity*sizeof(T));

memcpy(this->array, stack.array, stack.top*sizeof(T));

}

析构函数

template <typename T>

ArrayStack<T>::~ArrayStack()

{

delete [] this->array;

}

 

清除函数

template <typename T>

void ArrayStack<T>::clear()

{

this->top = 0;

}

判断是否为空

template <typename T>

bool ArrayStack<T>::isEmpty()

{

if(this->top == 0)

return true;

return false;

}

判断是否为满

template <typename T>

bool ArrayStack<T>::isFull()

{

if((this->top+1) >= this->capacity)

return true;

return false;

}

返回

template <typename T>

int ArrayStack<T>::length()

{

return this->top;

}

遍历函数

template <typename T>

void ArrayStack<T>::show()

{

std::cout<<"The sum of data in the stack is: "<<this->top<<" max size is: "<<this->capacity<<std::endl;

std::cout<<"detail:";

for(int i=0; i<this->top; i++){

std::cout<<this->array[i]<<" ";

}

std::cout<<std::endl;

}

 

获取栈顶栈底及pos处元素

template <typename T>

bool ArrayStack<T>::GetTopNode(T &data)

{

return this->GetPosNode(data, this->top-1);

}

 

template <typename T>

bool ArrayStack<T>::GetBottonNode(T &data)

{

return this->GetPosNode(data, 0);

}

 

 

template <typename T>

bool ArrayStack<T>::GetPosNode(T &data, int pos)

{

if(this->top == 0){

ULOGW("empty array");

return false;

}

if((pos<0) || (pos>=this->top)){

ULOGW("invalid pos or pos ",pos ,"th is beyond the max array index");

return false;

}

data = this->array[pos];

return true;

}

修改栈顶栈底及pos处元素

template <typename T>

bool ArrayStack<T>::SetTopNode(const T &data)

{

return this->SetPosNode(data, this->top-1);

}

 

template <typename T>

bool ArrayStack<T>::SetBottonNode(const T &data)

{

return this->SetPosNode(data, 0);

}

 

template <typename T>

bool ArrayStack<T>::SetPosNode(const T &data, int pos)

{

if(this->top == 0){

ULOGW("empty array");

return false;

}

if((pos<0) || (pos>=this->top)){

ULOGW("invalid pos or pos ",pos ,"th is not exist");

return false;

}

this->array[pos] = data;

return true;

}

压栈

template <typename T>

bool ArrayStack<T>::Push(const T &data)

{

if((this->top+1) >= this->capacity){

ULOGW("full stack");

return false;

}

this->array[this->top] = data;

this->top++;

return true;

}

出栈

template <typename T>

bool ArrayStack<T>::Pop(T &data)

{

if(this->top == 0){

ULOGW("empty stack");

return false;

}

this->top--;

data = this->array[this->top];

return true;

}

删除栈顶元素

template <typename T>

bool ArrayStack<T>::DelTopNode()

{

if(this->top == 0){

ULOGW("empty stack");

return false;

}

this->top--;

return false;

}

手动增加栈内存

template <typename T>

bool ArrayStack<T>::IncreaseStack(int len)

{

T *array_ = new T[this->capacity+len]; //未检测分配内存是否成功

memcpy(array_, this->array, this->top*sizeof(T));

this->capacity += len;

delete [] this->array;

this->array = array_;

return true;

}

源码下载:https://download.csdn.net/download/zhouchao_0321/10693600

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值