65.9K
CodeProject 正在变化。 阅读更多。
Home

使用模板实现堆栈

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (5投票s)

2001年12月19日

1分钟阅读

viewsIcon

102239

downloadIcon

964

通用 LIFO 堆栈实现

引言

栈是一种适配器,它提供受限制的容器功能:它提供栈顶元素的插入、删除和检查。栈是一种“后进先出”(LIFO)的数据结构:栈顶的元素是最近添加的元素。栈不允许遍历其元素。 始终记住一叠盘子来理解栈。

我决定在上周编写这个类,因为我需要它来完成一个小项目,但过了一段时间,我认为拥有一个通用的栈类会很有用,它可以在 C++ 中使用。

详细说明

我已经创建了两个类

  • CStackX:这是一个模板类,它使用默认大小为 10000 的 LIFO 实现。
  • ArrayOutofBounds:一个通用类,它将在遇到错误时打印异常。要使用此类,请参阅包含的演示。它是一个非常基本的演示,任何 C++ 初学者都应该能够理解。

堆栈在进行压栈操作后的示意图

0
1
2
3
4
5

pop() 将返回 5,因为它是在最后压入的元素,即,想象一下将一个盘子放在一叠盘子上,最后放上的盘子将被移除。 peek() 也会返回 5,因为它是在最后放上的盘子。

代码列表

/*Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*It is provided "as is" without express or implied warranty.
* kings_oz@yahoo.com
*/

// StackX.h: interface for the CStackX class.
//
/////////////////////////////////////////////

#ifndef _TREX_
#define _TREX_

#include <stdlib.h>
#include "ArrayBoundsEx.h"

#define ARRAY_MAX_SIZE 10000

template <class TRex>
class CStackX
{
public:
    CStackX();
    CStackX(long val);
    virtual ~CStackX();
    void push(TRex);
    TRex pop();
    TRex peek();
private:
    ArrayOutOfBounds *ex ;
    void setSize(long);
    long getSize();
    long arr_size;
    TRex *vect ;
    long top;
};

#endif

template <class TRex>
CStackX<TRex>::CStackX()
{
    //call super second constructor with value of 1000

    //create exception
    ex = new ArrayOutOfBounds();
    vect = new TRex[ARRAY_MAX_SIZE];
    top = -1;
}
template <class TRex>
CStackX<TRex>::CStackX(long val)
{
    ex = new ArrayOutOfBounds();
    setSize(val);
    vect = new TRex[arr_size];
    top = -1;
}
template <class TRex>
CStackX<TRex>::~CStackX()
{
    //delete array dynamically allocated
    delete ex;
    delete[] vect;
}
template <class TRex>
void CStackX<TRex>::push(TRex value)
{
    try
    {
        if(top != arr_size)
        {
            //top +=1;
            vect[++top] = value;
        }
        else
            throw ex;
    }
    catch(ArrayOutOfBounds * arrayExc)
    {
        arrayExc->printError("Max Value of Stack Reached oops.....\n");
        exit(1);
    }
}

template <class TRex>
TRex CStackX<TRex>::pop()
{
    try
    {
        if(top != -1)
            return vect[top--];
        else
            throw ex;
    }
    catch(ArrayOutOfBounds* exc)
    {
        exc->printError("End of Array  can't pop anymore ...\n");
        exit(1);
    }
}

template <class TRex>
TRex CStackX<TRex>::peek()
{
    try
    {
        if(top != -1)
            return vect[top];
        else
            throw ex;
    }
    catch(ArrayOutOfBounds* exc)
    {
        exc->printError("Peeking time closed nothing to see anymore...\n");
        exit(1);
    }
}

template <class TRex>
long CStackX<TRex>::getSize()
{
    return arr_size;
}

template <class TRex>
void CStackX<TRex>::setSize(long val)
{
    arr_size = val;
}

许可证

本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。

作者可能使用的许可证列表可以在此处找到。

© . All rights reserved.