数据结构 - 链栈的实现 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实现链栈
一.分析 栈是限定仅在表的一端进行插入或删除操作的线性表,对于栈来说,操作端称为栈顶,另一端则称为栈底,栈的修改是按照后进先出的原则进行的,因此又称为后进先出的线性表. 链栈是指采用链式存储结构实现的 ...
随机推荐
- MySQL实战45讲学习笔记:第十七讲
一 .引子 我在上一篇文章,为你讲解完 order by 语句的几种执行模式后,就想到了之前一个做英语学习 App 的朋友碰到过的一个性能问题.今天这篇文章,我就从这个性能问题说起,和你说说 MySQ ...
- [LeetCode] 421. Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] 264. Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- 仅逗oier们一笑(不定期更新中)(update.2019年12月8日)
CCF的正确解释: //部分来自:朝阳的二愣子的CSDN博客.ydclyq 的博客 .拱垲的博客.Randolph's Blog. 编译下列程序,会有意想不到的惊喜哦(注意打开声音): #includ ...
- java web开发入门十(idea创建maven SSM项目)基于intellig idea
一.搭建项目骨架 二.配置pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xm ...
- Nacos集成Spring Cloud Gateway 基础使用
项目结构 项目 端口 描述 nacos-provider 8000 服务 nacos-getway 8001 网关 nacos-provider项目依赖 <dependencies> &l ...
- LayUI-Table-添加禁止选中
LayUI这几年比较流行,里面的Table组件也比较强大,但是前面的CheckBox没有禁止选中功能,今天就来试试,看看能不能给添加一个禁止选中功能. Fork LayUI源码 LayUI项目地址 C ...
- Maven 教程(22)— Maven中 plugins 和 pluginManagement
原文地址https://blog.csdn.net/liupeifeng3514/article/details/80236827 plugins和pluginManagement的区别概述plugi ...
- wifi串口服务器
下面与大家分享上海卓岚无线wifi串口服务器ZLAN7104创建虚拟串口的设置使用心得 一.7104网线连接计算机,用ZLVircom即可搜索并配置 其中,串口设置需要匹配实际所接的串口设备,配置为相 ...
- CentOS7安装图形化界面方法
一.linux安装(root用户操作) 1. 安装vncserver; yum install tigervnc-server 2. 安装vncviewer; yum install vnc 3. 设 ...