栈 C++ 适用各种数据类型,,仿Java API

#ifndef STACK_HH
#define STACK_HH
#include<iostream>
#include<stdlib.h>
using namespace std;

template <typename type>
struct StackNode
{
    type value;
    StackNode *next;
    StackNode *previous;
};

template <typename type>
class Stack
{
private:
    int len;
    StackNode<type> *top;
    StackNode<type> *head;
    StackNode<type> *tail;
    bool isReverse;
public:
    Stack();
    ~Stack();
    bool isEmpty();
    bool push(type value);
    type pop();
    type getTop();
    bool reverse();
    bool isReversed();
    int getLength();
};

template <typename type>
Stack<type>::Stack()
{

    top = NULL;
    len = -1;
    isReverse = false;
}

template <typename type>
bool Stack<type>::isEmpty()
{
    if(this->len == -1 || this->top == NULL)
    {
        return true;
    }
    return false;
}

template <typename type>
bool Stack<type>::push(type value)
{
    StackNode<type> *vNode = new StackNode<type>();
    vNode->value = value;

    if(vNode == NULL)
    {
        return false;
    }

    if(len == -1 || this->top == NULL)
    {
        top = vNode;
        top->next = NULL;
        top->previous = NULL;
        this->head = vNode;
        this->tail = vNode;
        this->len++;
        return true;
    }
    //if the stack was reversed
    if(this->isReverse)
    {
        vNode->previous = this->tail;
        this->tail->next = vNode;
        this->tail = vNode;
        this->top = this->tail;
    }
    else
    {
        vNode->next = this->head;
        this->head ->previous = vNode;
        this->head = vNode;

        this->top = this->head;
    }
    this->len++;
    return true;
}

template <typename type>
type Stack<type>::pop()
{
    if(len == -1 || this->top == NULL)
    {
        return NULL;
    }
    type temp = this->top->value;
    StackNode<type> *nTemp;
    if(this->isReverse)
    {
        nTemp = this->top;
        this->tail = this->top->previous;
        this->top = this->tail;
        free(nTemp);
        nTemp = NULL;
    }
    else
    {
        nTemp = this->top;
        this->head = this->top->next;
        this->top = this->head;
        free(nTemp);
        nTemp = NULL;
    }
    this->len--;
    return temp;
}
template <typename type>
type Stack<type>::getTop()
{
	if(len == -1 || this->top == NULL)
	{
		return NULL;
	}
	return this->top->value;
}

template <typename type>
bool Stack<type>::reverse()
{
    try{
    if(this->len == -1)
    {
        return false;
    }
    if(this->isReverse)
    {
        this->top = this->head;
        this->isReverse = false;
    }
    else
    {
        this->top = this->tail;
        this->isReverse = true;
    }
    return true;
    }catch(...){
        return false;
    }
}

template <typename type>
bool Stack<type>::isReversed()
{
    return this->isReverse;
}

template <typename type>
int Stack<type>::getLength()
{
    return this->len+1;
}

template <typename type>
Stack<type>::~Stack()
{
    while(this->pop()) {}
}
#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值