链栈封装 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++的更多相关文章

  1. 数据结构 - 链栈的实行(C语言)

    数据结构-链栈的实现 1 链栈的定义 现在来看看栈的链式存储结构,简称为链栈. 想想看栈只是栈顶来做插入和删除操作,栈顶放在链表的头部还是尾部呢?由于单链表有头指针,而栈顶指针也是必须的,那干吗不让它 ...

  2. 数据结构——链栈(link stack)

    /* linkStack.c */ /* 链栈 */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h ...

  3. C语言数据结构链栈(创建、入栈、出栈、取栈顶元素、遍历链栈中的元素)

    /**创建链栈*创建一个top指针代表head指针*采用链式存储结构*采用头插法创建链表*操作 创建 出栈 入栈 取栈顶元素*创建数据域的结构体*创建数据域的名称指针*使用随机函数对数据域的编号进行赋 ...

  4. C#数据结构-链栈

    上一篇我们通过数组结构实现了栈结构(准确的说是栈的顺序存储结构),现在我们通过链(单链)存储栈,也就是链栈. 通常对于正向单链表来说,是从头节点开始,在链的尾部附加节点,前一个节点的指针指向附加节点: ...

  5. 算法与数据结构(二) 栈与队列的线性和链式表示(Swift版)

    数据结构中的栈与队列还是经常使用的,栈与队列其实就是线性表的一种应用.因为线性队列分为顺序存储和链式存储,所以栈可以分为链栈和顺序栈,队列也可分为顺序队列和链队列.本篇博客其实就是<数据结构之线 ...

  6. 【Java】 大话数据结构(6) 栈的顺序与链式存储

    本文根据<大话数据结构>一书,实现了Java版的栈的顺序存储结构.两栈共享空间.栈的链式存储机构. 栈:限定仅在表尾进行插入和删除操作的线性表. 栈的插入(进栈)和删除(出栈)操作如下图所 ...

  7. 【C#】【数据结构】006-栈:链栈

    C#数据结构:链栈 1.自定义链栈结构: 链栈节点类 using System.Collections; using System.Collections.Generic; using UnityEn ...

  8. java与数据结构(6)---java实现链栈

    栈之链式存储结构链栈 链栈 栈的链式存储结构成为链栈.链栈是没有头结点,头结点就是栈顶指针top. 代码结构 package list; public interface Stackable;公共接口 ...

  9. 数据结构——Java实现链栈

    一.分析 栈是限定仅在表的一端进行插入或删除操作的线性表,对于栈来说,操作端称为栈顶,另一端则称为栈底,栈的修改是按照后进先出的原则进行的,因此又称为后进先出的线性表. 链栈是指采用链式存储结构实现的 ...

随机推荐

  1. MySQL实战45讲学习笔记:第十讲

    一 .本节内容概要 前面我们介绍过索引,你已经知道了在 MySQL 中一张表其实是可以支持多个索引的.但是,你写 SQL 语句的时候,并没有主动指定使用哪个索引.也就是说,使用哪个索引是由MySQL ...

  2. Note | PyTorch官方教程学习笔记

    目录 1. 快速入门PYTORCH 1.1. 什么是PyTorch 1.1.1. 基础概念 1.1.2. 与NumPy之间的桥梁 1.2. Autograd: Automatic Differenti ...

  3. IntelliJ IDEA最新版2019年注册码,可激活至2099年 激活 破解

    IntelliJ IDEA最新版2019年注册码,可激活至2099年 激活 破解 特别说明:图中的IDEA是2017版本,不过方法对2019版本的IDEA同样适用! 最近笔者测试了好多破解Idea的方 ...

  4. 利用Jenkins打包并远程部署NodeJS应用

    本文Jenkins版本2.190.2,为19年11月最新 1.安装Jenkins.Git和NodeJS Jenkins安装参考:https://www.cnblogs.com/zhi-leaf/p/1 ...

  5. pg_sql常用查询语句整理

    #pg_sql之增删改查 #修改: inset into table_name (id, name, age, address ) select replace(old_id,old_id,new_i ...

  6. Pandas中查看列中数据的种类及个数

    Pandas中查看列中数据的种类及个数 读取数据 import pandas as pd import numpy as np filepath = 'your_file_path.csv' data ...

  7. cv2.putText,cv2.rectangle方法

    常用方法:cv2.putText(img,xy,(x1,y1), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), thickness=2)img:要作用的图片xy:显示 ...

  8. js中的方法如何传入多个参数

    js中的方法如何传入多个参数 $(function () { let parameter1 = 1; let parameter2 = 'Hello World'; let parameter3 = ...

  9. pod的yaml例子

    apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selecto ...

  10. 一段简单的顶部JS广告

    一段简单的顶部JS广告 <SCRIPT LANGUAGE="JavaScript"> ; ; images = new Array; images[] = new Im ...