C++用模板实现栈

#ifndef _CStack  
#define _CStack  
#include<iostream.h>  
template<class T>
class CStack{
    // LIFO 对象  
public:
    CStack(int MaxStackSize = 10);
    ~CStack() { delete[] stack; }
    bool IsEmpty() const { return top == -1; }
    bool IsFull() const { return top == MaxTop; }
    T Top() const;
    CStack<T>& Add(const T& x);
    CStack<T>& Delete(T& x);
    friend ostream& operator<<(ostream& out, const CStack<T>& w);

private:
    int top; // 栈顶  
    int MaxTop; // 最大的栈顶值  
    T *stack; // 堆栈元素数组  
};

template<class T>
CStack<T>::CStack(int MaxStackSize)
{// CStack 类构造函数  
    MaxTop = MaxStackSize - 1;
    stack = new T[MaxStackSize];
    top = -1;
}
template<class T>
T CStack<T>::Top() const
{// 返回栈顶元素  
    if (!IsEmpty())
        return stack[top];
}
template<class T>
CStack<T>& CStack<T>::Add(const T& x)
{ //添加元素x  
    if (!IsFull())
    {
        stack[++top] = x;
        return *this;
    }
}

template<class T>
CStack<T>& CStack<T>::Delete(T& x)
{// 删除栈顶元素,并将其送入x  
    if (!IsEmpty())
    {
        x = stack[top--];
        return *this;
    }
}
template <class T>
ostream& operator<<(ostream& out, const CStack<T>& w)
{
    for (int i = w.top; i > -1; i--)
        out << w.stack[i] << " ";
    return out;

}


/*采用链表来定义栈*/
template<class T>class LinkedStack;
template <class T>
class Node{
    friend LinkedStack<T>;
private:
    T data;
    Node<T> *link;
};
template<class T>
class LinkedStack {
public:
    LinkedStack() { top = 0; }
    ~LinkedStack();
    bool IsEmpty() const { return top == 0; }
    bool IsFull() const;
    T Top() const;
    LinkedStack<T>& Add(const T& x);
    LinkedStack<T>& Delete(T& x);
private:
    Node<T> *top; // 指向栈顶节点  
};

template<class T>
LinkedStack<T>:: ~LinkedStack()
{
    // 析构函数  
    Node<T> *next;
    while (top) {
        next = top->link;
        delete top;
        top = next;
    }
}
template<class T>
bool LinkedStack<T>::IsFull() const
{// 堆栈是否满?  
    try {
        Node<T> *p = new Node<T>;
        delete p;
        return false;
    }
    catch (...) { return true; }
}
template<class T>
LinkedStack<T>& LinkedStack<T>::Add(const T& x)
{// 添加元素x  
    Node<T> *p = new Node<T>;
    p->data = x;
    p->link = top;
    top = p;
    return *this;
}
template<class T>
T LinkedStack<T>::Top() const
{// 返回栈顶元素  
    if (!IsEmpty())
        return top->data;
}
template<class T>
LinkedStack<T>& LinkedStack<T>::Delete(T& x)
{// 删除栈顶元素,并将其送入x  
    if (!IsEmpty())
    {

        x = top->data;
        Node<T> *p = top;
        top = top->link;
        delete p;
        return *this;
    }
}
#endif  
//在主函数的调用如下:
[cpp] view plain copy
#include "Stack.h"  
void main()
{
    CStack<int> S(20);
    for (int i = 0; i < 20; i++)
        S.Add(i);
    cout << "栈顶元素为:" << S.Top() << endl;
    cout << "所有栈元素为:" << S << endl;
}

转自:http://blog.csdn.net/xiaoding133/article/details/6897101

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于类模板实现类的示例代码: ```c++ template <typename T> class Stack { private: T* stackArray; // 存储元素的数组 int top; // 顶元素的下标 int capacity; // 的容量 public: // 构造函数,创建一个 Stack(int capacity = 10) { this->capacity = capacity; stackArray = new T[capacity]; top = -1; } // 析构函数,释放内存 ~Stack() { delete[] stackArray; } // 判断是否为空 bool isEmpty() { return top == -1; } // 判断是否已满 bool isFull() { return top == capacity - 1; } // 入操作 void push(T element) { if (isFull()) { cout << "Stack overflow!" << endl; return; } stackArray[++top] = element; } // 出操作 T pop() { if (isEmpty()) { cout << "Stack underflow!" << endl; return T(); } return stackArray[top--]; } // 获取顶元素 T getTop() { if (isEmpty()) { cout << "Stack is empty!" << endl; return T(); } return stackArray[top]; } // 获取的元素个数 int size() { return top + 1; } }; ``` 这个类模板实现一个基本的数据结构,其中元素类型为模板参数T,可以根据实际需要进行实例化。使用示例: ```c++ Stack<int> s(5); // 创建一个容量为5的整型 s.push(1); s.push(2); s.push(3); cout << s.pop() << endl; // 输出3 cout << s.getTop() << endl; // 输出2 cout << s.size() << endl; // 输出2 ``` 注意,这里的容量是在构造函数中指定的,如果不指定则默认为10。在入操作中,如果已满则会输出一条错误信息并返回。在出操作中,如果为空则同样会输出一条错误信息并返回一个默认值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值