1 Star 0 Fork 0

郑玉强/dataStructure

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
seq_stack.hpp 3.15 KB
一键复制 编辑 原始数据 按行查看 历史
#pragma once
#include <iostream>
#include "stack.h"
using namespace std;
//// 顺序栈模板类
//template <class TData> class SeqStack;
//
//// 重载<<
//template <typename TData> ostream& operator<<(ostream& os, const SeqStack<TData>& seq_stack);
template <class TData>
class SeqStack :public Stack<TData>
{
// 友元函数没有权限控制符:它不属于类的成员,因此无法受到 public、protected 或 private 的限制。
// 重载<<
/* <> 这对尖括号表明这是一个函数模板的友元声明,是模板参数推断。
* 因为 SeqStack 是一个类模板,友元函数也需要是模板函数,
* 而 <> 表明我们要匹配一个任意类型 TData 的模板函数。
*/
/*
* 当某个非成员函数或其他类的成员函数需要访问类的私有成员时。
*如果函数的功能比较独立,不适合作为类的成员函数,但需要访问类的私有数据时,可以将其声明为友元函数。
*/
friend ostream& operator<< <>(ostream& os, const SeqStack<TData>& seq_stack);
private:
TData* mem_data_;
int capacity_;
int top_; // 指向入栈的栈顶元素
public:
explicit SeqStack(int capacity = 20) :capacity_(capacity_), top_(-1)
{
// mem_data_ 指向的是这个动态数组的第一个元素,而不是数组本身。
// C++中没有数组类型的指针,数组名(或指针)指向的是数组的首个元素
this->mem_data_ = new TData[this->capacity_];
if (!this->mem_data_)
throw bad_alloc();
}
~SeqStack();
virtual bool push(const TData& data) override;
virtual bool pop(TData& data) override;
bool pop();
virtual bool top(TData& data) const override;
virtual bool isEmpty() const override;
virtual int length() const override;
bool isFull() const;
void clear();
};
// 重载<<是为了cout << object; 可以直接输出object的相关信息
template <typename TData>
ostream& operator<<(ostream& os, const SeqStack<TData>& seq_stack)
{
os << "栈中元素的个数:" << seq_stack.length() << endl;
// 从栈顶开始输出
for (int i = seq_stack.top_; i >= 0; i--)
{
os << seq_stack.mem_data_[i] << endl;
}
return os; // 为了支持链式编程 如cout << object; cout << endl; ===> cout << object << endl;
}
template<class TData>
inline SeqStack<TData>::~SeqStack()
{
delete[] this->mem_data_;
}
template<class TData>
inline bool SeqStack<TData>::push(const TData& data)
{
if (this->isFull())
return false;
this->top_++;
this->mem_data_[this->top_] = data;
return true;
}
template<class TData>
inline bool SeqStack<TData>::pop(TData& data)
{
if (this->length() == 0)
return false;
data = this->mem_data_[this->top_];
this->top_--;
return true;
}
template<class TData>
inline bool SeqStack<TData>::pop()
{
if (this->length() == 0)
return false;
this->top_--;
return true;
}
template<class TData>
inline bool SeqStack<TData>::top(TData& data) const
{
if (this->length() == 0)
return false;
data = this->mem_data_[this->top_];
return true;
}
template<class TData>
inline bool SeqStack<TData>::isEmpty() const
{
return this->top_ == -1;
}
template<class TData>
inline int SeqStack<TData>::length() const
{
return this->top_ + 1;
}
template<class TData>
inline bool SeqStack<TData>::isFull() const
{
return this->top_ == this->capacity_ - 1;
}
// 删除栈中的实体元素
template<class TData>
inline void SeqStack<TData>::clear()
{
int length = this->length();
if (length == 0)
return;
for (int i = 0; i < length; i++)
this->pop();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/zheng-yuqiang_lyg_cn/data-structure.git
git@gitee.com:zheng-yuqiang_lyg_cn/data-structure.git
zheng-yuqiang_lyg_cn
data-structure
dataStructure
dev01

搜索帮助