数据结构 - 顺序栈的实现 C++

顺序栈封装 C++

使用C++对顺序栈进行了简单的封装,实现了栈的基本操作
封装方法: pop(),top(),size(),empty(),push()

代码已经过测试

#pragma once
#include <iostream>
#include <algorithm>
using namespace std;
template<class T> class Stack {
public:
        Stack();       //构造函数
   void pop();         //弹出头元素
   void push(T value); //入栈
   bool empty();       //判断是否为空栈
   int  size();        //返回栈的大小
     T  top();         //获取首元素

private:
    T arr[100];
    int head;
    int tail;
    int len;
};

template<class T>
inline Stack<T>::Stack()
{
    this->head = 0;
    this->tail = 0;
    this->len = 0;
}

template<class T>
inline void Stack<T>::pop()
{
    this->head--;
    this->len--;
}

template<class T>
inline void Stack<T>::push(T value)
{
    head++;
    arr[head] = value;
    this->len++;
}

template<class T>
inline bool Stack<T>::empty()
{
    if(this->len == 0)
    return true;
    else return false;
}

template<class T>
inline int Stack<T>::size()
{
    return this->len;
}

template<class T>
inline T Stack<T>::top()
{
    return T(arr[head]);
}

如果大家有什么疑问的话可以加qq向我提出哦,欢迎各位大佬指出问题。
如果你觉得对你有所帮助的话就给我点个赞,点燃我下次写文章的动力吧 ^_^ !

转载于:https://www.cnblogs.com/wlw-x/p/11609672.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值