数据结构 - 链栈的实现 C++
链栈封装 C++
使用C++对链栈进行了简单的封装,实现了栈的基本操作
封装方法: pop(),top(),size(),empty(),push()
代码已经过测试
#pragma once
#include <iostream>
#include <algorithm>
using namespace std;
template<class T>class LinkNode{
public:
T data;
LinkNode *pnext;
LinkNode *ppre;
};
template<class T>class Stack {
public:
Stack(); //构造函数
void pop(); //弹出头元素
void push(T value); //入栈
bool empty(); //判断是否为空栈
int size(); //返回栈的大小
T top(); //获取首元素
private:
LinkNode<T> *head;
LinkNode<T> *tail;
int len;
};
template<class T>
inline Stack<T>::Stack()
{
this->head = nullptr;
this->tail = nullptr;
this->len = 0;
}
template<class T>
inline void Stack<T>::pop()
{
if (this->head == this->tail)
{
this->head = this->tail = nullptr;
this->len = 0;
return;
}
LinkNode<T> *p = this->tail;
this->tail = p->ppre;
this->tail->pnext = nullptr;
free(p);
p = nullptr;
this->len--;
}
template<class T>
inline void Stack<T>::push(T value)
{
LinkNode<T> *newnode = new LinkNode<T>;
newnode->data = value;
if (this->len == 0)
{
this->head = newnode;
this->tail = newnode;
this->len++;
}
else {
tail->pnext = newnode;
newnode->ppre = tail;
this->tail = newnode;
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(this->tail->data);
}
如果大家有什么疑问的话可以加qq向我提出哦,欢迎各位大佬指出问题。
如果你觉得对你有所帮助的话就给我点个赞,点燃我下次写文章的动力吧 _ !
数据结构 - 链栈的实现 C++的更多相关文章
- 数据结构 - 链栈的实行(C语言)
数据结构-链栈的实现 1 链栈的定义 现在来看看栈的链式存储结构,简称为链栈. 想想看栈只是栈顶来做插入和删除操作,栈顶放在链表的头部还是尾部呢?由于单链表有头指针,而栈顶指针也是必须的,那干吗不让它 ...
- 数据结构——链栈(link stack)
/* linkStack.c */ /* 链栈 */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h ...
- C语言数据结构链栈(创建、入栈、出栈、取栈顶元素、遍历链栈中的元素)
/**创建链栈*创建一个top指针代表head指针*采用链式存储结构*采用头插法创建链表*操作 创建 出栈 入栈 取栈顶元素*创建数据域的结构体*创建数据域的名称指针*使用随机函数对数据域的编号进行赋 ...
- C#数据结构-链栈
上一篇我们通过数组结构实现了栈结构(准确的说是栈的顺序存储结构),现在我们通过链(单链)存储栈,也就是链栈. 通常对于正向单链表来说,是从头节点开始,在链的尾部附加节点,前一个节点的指针指向附加节点: ...
- 算法与数据结构(二) 栈与队列的线性和链式表示(Swift版)
数据结构中的栈与队列还是经常使用的,栈与队列其实就是线性表的一种应用.因为线性队列分为顺序存储和链式存储,所以栈可以分为链栈和顺序栈,队列也可分为顺序队列和链队列.本篇博客其实就是<数据结构之线 ...
- 【Java】 大话数据结构(6) 栈的顺序与链式存储
本文根据<大话数据结构>一书,实现了Java版的栈的顺序存储结构.两栈共享空间.栈的链式存储机构. 栈:限定仅在表尾进行插入和删除操作的线性表. 栈的插入(进栈)和删除(出栈)操作如下图所 ...
- 【C#】【数据结构】006-栈:链栈
C#数据结构:链栈 1.自定义链栈结构: 链栈节点类 using System.Collections; using System.Collections.Generic; using UnityEn ...
- java与数据结构(6)---java实现链栈
栈之链式存储结构链栈 链栈 栈的链式存储结构成为链栈.链栈是没有头结点,头结点就是栈顶指针top. 代码结构 package list; public interface Stackable;公共接口 ...
- 数据结构——Java实现链栈
一.分析 栈是限定仅在表的一端进行插入或删除操作的线性表,对于栈来说,操作端称为栈顶,另一端则称为栈底,栈的修改是按照后进先出的原则进行的,因此又称为后进先出的线性表. 链栈是指采用链式存储结构实现的 ...
随机推荐
- [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- OBS录制全屏游戏的方法(超好录屏)
新版Windows设置 详见 https://github.com/obsproject/obs-studio/wiki/Laptop-Troubleshooting 新版的Windows 10: l ...
- 509道Java面试题解析:2020年最新Java面试题
<Java面试全解析>是我在 GitChat 发布的一门电子书,全书总共有 15 万字和 505 道 Java 面试题解析,目前来说应该是最实用和最全的 Java 面试题解析了. 我本人是 ...
- 问题查询-tomcat内存泄露
1.报警信息 内容: 微信服务器向公众号推送消息或事件后,开发者5秒内没有返回 次数: 5分钟 239次 错误样例: [OpenID=o][Stamp=1562718361][3rdUrl=url][ ...
- Qt Quick 事件处理之鼠标、键盘、定时
一.鼠标事件 MouseArea 对象可以附加到一个 item 上供 item 处理鼠标事件,它本身是一个不可见的 item .在其内部,可以直接引用它所附着的对象的属性和方法.你可以将 MouseA ...
- vertica单节点故障恢复 Startup Failed, ASR Required
测试环境的vertica是单节点的,无法做到故障自动恢复,需要手工处理.案例如下: 发现5433端口连接不上,vertica挂了,手工运行admintools,重新启动vertica,仍然失败,提示: ...
- centos7.x下环境搭建(五)—nginx搭建https服务
https证书获取 十大免费SSL证书 https://blog.csdn.net/ithomer/article/details/78075006 如果我们用的是阿里云或腾讯云,他们都提供了免费版的 ...
- jmeter 如何获取一小时之前的时间戳
正确答案: ${__intSum(${__time(/1000,)},-3600,)} 如果还要显示毫秒 ${__longSum(${__time},-3600000,)}