美文网首页
[C++]模板类的使用(Stack)

[C++]模板类的使用(Stack)

作者: Luliang | 来源:发表于2019-08-26 14:29 被阅读0次

模板类的使用

// stcktp1.h -- modified Stack template
#ifndef STCKTP1_H_
#define STCKTP1_H_

template <class Type>
class Stack
{
private:
    enum { SIZE = 10 };   // default size
    int stacksize;
    Type * items;       // holds stack items
    int top;            // index for top stack item
public:

    explicit Stack(int ss = SIZE);
    Stack(const Stack & st);
    ~Stack() { delete[] items; }

    bool isempty() { return top == 0; }
    bool isfull() { return top == stacksize; }
    bool push(const Type & item);   // add item to stack
    bool pop(Type & item);          // pop top into item

    Stack & operator=(const Stack & st);
};

template <class Type>
Stack<Type>::Stack(int ss) : stacksize(ss), top(0)
{
    items = new Type[stacksize];
}

// 拷贝构造函数
template <class Type>
Stack<Type>::Stack(const Stack & st)
{
    stacksize = st.stacksize;
    top = st.top;
    items = new Type[stacksize];
    for (int i = 0; i < top; i++)
        items[i] = st.items[i];
}

template <class Type>
bool Stack<Type>::push(const Type & item)
{
    if (top < stacksize)
    {
        items[top++] = item;
        return true;
    }
    else
        return false;
}

template <class Type>
bool Stack<Type>::pop(Type & item)
{
    if (top > 0)
    {
        item = items[--top];
        return true;
    }
    else
        return false;
}

template <class Type>
Stack<Type> & Stack<Type>::operator=(const Stack<Type> & st)
{
    // 如果是自己本身,直接返回
    if (this == &st) {
        return *this;
    }

    // 删除以前的items
    delete[] items;
    stacksize = st.stacksize;
    top = st.top;
    items = new Type[stacksize];
    for (int i = 0; i < top; i++)
    {
        items[i] = st.items[i];
    }
    return *this;
}

#endif

测试类

// stkoptr1.cpp -- testing stack of pointers
#include <iostream>
#include <cstdlib>      // for rand(), srand()
#include <ctime>        // for time()
#include "../inc/stcktp1.h"

const int Num = 10;
int main()
{
    std::srand(std::time(0));   // randomize rand()
    std::cout << "Please enter stack size: ";
    int stacksize;
    std::cin >> stacksize;
    // create an empty stack with stacksize slots
    Stack<const char *> st(stacksize);

    // in basket
    const char * in[Num] = {
            " 1: Hank Gilgamesh", " 2: Kiki Ishtar",
            " 3: Betty Rocker", " 4: Ian Flagranti",
            " 5: Wolfgang Kibble", " 6: Portia Koop",
            " 7: Joy Almondo", " 8: Xaverie Paprika",
            " 9: Juan Moore", "10: Misha Mache"
    };
    // out basket
    const char * out[Num];

    int processed = 0;
    int nextin = 0;
    while (processed < Num)
    {
        if (st.isempty())
            st.push(in[nextin++]);
        else if (st.isfull())
            st.pop(out[processed++]);
        else if (std::rand() % 2 && nextin < Num) // 50-50 chance
            st.push(in[nextin++]);
        else
            st.pop(out[processed++]);
    }
    for (int i = 0; i < Num; i++)
        std::cout << out[i] << std::endl;

    std::cout << "Bye\n";
    return 0;
}

相关文章

  • [C++]模板类的使用(Stack)

    模板类的使用 测试类

  • 【数据结构】stack-数据结构

    1、stack stack 模板类的定义在头文件《stack》中。 stack 模板类需要两个模板参数,一个是元素...

  • Java设计模式之-模板方法模式(Template Method

    说到模(mú)板,很多人都接触过,C++和Java都有诸如List和Stack这样的模板类。但是说到模板方法,很多...

  • C++ 模版 学习总结

    C++ 模版 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据...

  • stack

    stack 构造函数 stack stkT;//stack采用模板类实现,stack对象的默认构造形式: stac...

  • GEEKBAND STL第一周

    关于模板库 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据成...

  • Geekband C++ 第五周

    概述 C++模板简介 函数模板 C++类模板 操作符重载 泛型编程 容器

  • C++类模板

    一、定义 C++ 类模板 定义一个 C++ 类模板和定义一个函数模板类似,可以指定一个或者多个模板参数标识符。在类...

  • Java泛型——学会编写自己的泛型

    自定义泛型其实就跟c++里面用模板定义很多类类似,比如:我么要写一个栈的类,那么我们完全可以用Stack来代...

  • 10-C++远征之模板篇-学习笔记

    C++远征之模板篇 将会学到的内容: 模板函数 & 模板类 -> 标准模板类 友元函数 & 友元类 静态数据成员 ...

网友评论

      本文标题:[C++]模板类的使用(Stack)

      本文链接:https://www.haomeiwen.com/subject/xggrectx.html