数据结构学习:c++实现栈的链式存储结构

上一篇博客介绍了如何使用c++实现顺序栈,可以查看下面的链接:

https://blog.csdn.net/weixin_42119041/article/details/102405438

和线性表一样,栈也存在链式存储结构,简称为:链栈;下面我们就看链栈是如何使用c++实现的(保证结果可复现):

代码块包括所示的三个部分:
1、首先是头文件部分(声明和定义链栈类):
#ifndef _LINKSTACK_H
#define _LINKSTACK_H
#include <iostream>
using namespace std;

template <class ElemType>
struct Node {
	ElemType data;            //数据域
	Node *next;               //指针域
};

template <class ElemType>
class LinkStack
{
public:
	LinkStack();                       //构造函数
	virtual ~LinkStack();              //析构函数
	void push(ElemType &e);            //进栈操作
	void pop();                        //出栈操作
	bool isEmpty();                    //判断栈是否为空栈
	int getSize();                     //获取栈的大小
	void display();                    //从栈顶至栈底输出栈的内容
	ElemType &getTop();                //获取栈顶元素
	
	int size;                     //栈的大小
	Node<ElemType> *top;          //栈顶指针
};
#endif
2、其次是函数定义部分:
#include "LinkStack.h"

//构造函数

template <class ElemType>
LinkStack<ElemType>::LinkStack(){
	top = NULL;
	this->size = 0;
}

//析构函数

template <class ElemType>
LinkStack<ElemType>::~LinkStack(){
}

//进栈操作

template <class ElemType>
void LinkStack<ElemType>::push(ElemType &e) {
	Node<ElemType> *newPtr = new Node<ElemType>;
	newPtr->data = e;
	newPtr->next = top;
	top = newPtr;
	this->size++;
}

//出栈操作

template <class ElemType>
void LinkStack<ElemType>::pop() {
	Node<ElemType> *p = top;
	top = top->next;
	delete p;
	this->size--;
}

//判断栈是否为空栈

template<class ElemType>
bool LinkStack<ElemType>::isEmpty(){
	if ( !top )
		return true;
	else
		return false;
}

//获得栈的大小

template <class ElemType>
int LinkStack<ElemType>::getSize() {
	return this->size;
}

//从栈顶至栈底输出栈

template <class ElemType>
void LinkStack<ElemType>::display() {
	if (!top) {
		cout << "stack is empty " << endl;
	    return;
	}
	Node<ElemType> *ptr = top;
	while (ptr) {
		cout << ptr->data << " ";
		ptr = ptr->next;
	}
}

//查看栈顶元素
template <class ElemType>
ElemType &LinkStack<ElemType>::getTop() {
	return top->data;
}
3、最后是测试函数(main函数):
#include "LinkStack.h"
#include "LinkStack.cpp"
#include <iostream>

int main()
{
	LinkStack<int> ls;
	cout << "********************" << endl;
	cout << "未插入元素的栈";
	cout << "是否为空栈:" << boolalpha << ls.isEmpty() << endl;
	cout << "元素数为: " << ls.getSize() << endl;
	ls.display();
	cout << "********************" << endl;
	for (int i = 0; i < 10; ++i) {
		ls.push(i);
	}
	cout << "插入元素后:";
	cout << "是否为空栈:" << boolalpha << ls.isEmpty() << endl;
	cout << "元素数为: " << ls.getSize() << endl;
	cout << "栈顶元素是:" << ls.getTop() << endl;
	ls.display();
	cout << endl;
	cout << "********************" << endl;
	ls.pop();
	cout << "弹出元素后的栈";
	cout << "是否为空栈:" << boolalpha << ls.isEmpty() << endl;
	cout << "元素数为: " << ls.getSize() << endl;
	cout << "弹出后栈顶的栈顶元素是:" << ls.getTop() << endl;
	ls.display();
	cout << endl;
	system("pause");
	return 0;
}
4、测试结果为:

在这里插入图片描述

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值