Data Structures In C++

栈的应用

自定义一个栈(In C++)

在头文件 my_stack.h 中自定义栈。

  1. 自定义栈的类的数据成员和公有函数:
#pragma once
#include <iostream>
using namespace std;

template <typename Type>
class Stack
{
private:
    Type *stk; //起始地址
    int MAXN;  //最大容量
    int top;   //指向栈顶元素(下标)
public:
    Stack(int size); //初始化
    ~Stack();
    bool empty() const;   //判断空
    bool full() const;    //判断满
    bool push(Type x);    //入栈
    bool pop();           //出栈
    Type get_top() const; //取栈顶元素
    int size() const;     //栈大小
    int Max() const;      //最大容量
};
  1. 函数的定义:
template <typename Type>
Stack<Type>::Stack(int size)
{
    MAXN = size;
    stk = new Type[MAXN];
    top = -1;
} //栈初始化

template <typename Type>
Stack<Type>::~Stack()
{
    delete stk;
} //析构函数定义

template <typename Type>
bool Stack<Type>::empty() const
{
    if (top == -1)
    {
        return true;
        cout << "underflow" << endl;
    }
    return false;
}

template <typename Type>
bool Stack<Type>::full() const
{
    if (top == MAXN - 1)
    {
        return true;
        cout << "overflow" << endl;
    }
    return false;
}

template <typename Type>
bool Stack<Type>::push(Type x)
{
    if (full())
    {
        return false;
        cout << "overflow" << endl;
    }
    stk[++top] = x;
    return true;
}

template <typename Type>
bool Stack<Type>::pop()
{
    if (empty())
    {
        return false;
        cout << "underflow" << endl;
    }
    top--;
    return true;
}

template <typename Type>
Type Stack<Type>::get_top() const
{
    if (empty())
    {
        return false;
        cout << "underflow" << endl;
    }
    return stk[top];
}

template <typename Type>
int Stack<Type>::size() const
{
    return top + 1;
}

template <typename Type>
int Stack<Type>::Max() const
{
    return this ->MAXN;
}

在源文件my_stack.cpp中创建一个栈对象,实现栈的基本操作。

  1. 创建对象,实现栈的基本操作。
#include <iostream>
#include "my_stack.h"
using namespace std;

int main()
{
    int n, x, ele, i; //最大长度;元素个数;元素;输入元素循环次数
enter:
    cout << "enter max length of stack :" << endl;
    cin >> n;
    cout << "--------------------------------" << endl;
    cout << "enter numbers of elements:" << endl;
    cin >> x;
    cout << "--------------------------------" << endl;
    if (x >= n)
    {
        cout << "failed! numbers of elements must less than max length!" << endl
             << "please enter again!" << endl;
        goto enter;
    }
    /*创建栈对象*/
    Stack<int> s(n);
    cout << "The max length is : " << s.Max() << endl;
    cout << "--------------------------------" << endl;
    cout << "enter your elements:" << endl;
    /* 循环元素入栈 */
    for (i = 0; i < x; i++)
    {
        cin >> ele;
        s.push(ele);
    }
    /* 元素出栈 */
    cout << "--------------------------------" << endl;
    cout << "stack size:" << s.size() << endl;
    cout << "output elements:" << endl;

    while (s.empty() != true)
    {
        cout << s.get_top() << endl;    //取栈顶元素
        s.pop();
    }

    cout << "stack size:" << s.size() << endl;
    cout << "--------------------------------" << endl;

    return 0;
}
  1. 用模板类的时候注意编写的格式。
    模板类外函数定义:
    template <typename 类型参数>
    函数类型 类名<类型参数>::成员函数名(形参表)
    {
    ……
    }

  2. 运行结果。
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值