LinkStack
//Node.h
template<typename ElemType>
struct Node
{
ElemType data;
Node<ElemType> *next;
Node();
Node(ElemType item,Node<ElemType> * link=NULL);
};
template<typename ElemType>
Node<ElemType>::Node()
{
next=NULL;
}
template<typename ElemType>
Node<ElemType>::Node(ElemType item,Node<ElemType> *link)
{
data=item;
next=link;
}
//LinkStack.h
template<typename ElemType>
class LinkStack
{
protected:
Node<ElemType> *top;
int count;
public:
LinkStack();
virtual ~LinkStack();
int Length() const;
bool Empty() const;
void Clear();
void Traverse(void (*visit)(const ElemType &)) const;
bool Push(const ElemType &e);
bool Top(ElemType &e) const;
bool Pop(ElemType &e);
LinkStack(const LinkStack<ElemType> ©);
LinkStack<ElemType> &operator=(const LinkStack<ElemType> ©);
};
template<typename ElemType>
LinkStack<ElemType>::LinkStack()
{
top=NULL;
count=;
}
template<typename ElemType>
LinkStack<ElemType>::~LinkStack()
{
Clear();
}
template<typename ElemType>
int LinkStack<ElemType>::Length() const
{
return count;
}
template<typename ElemType>
bool LinkStack<ElemType>::Empty() const
{
return top==NULL;
}
template<typename ElemType>
void LinkStack<ElemType>::Clear()
{
ElemType tmpElem;
while(!Empty())
Pop(tmpElem);
}
template<typename ElemType>
void LinkStack<ElemType>::Traverse(void (*visit)(const ElemType &))const
{
Node<ElemType> *tmpPtr;
LinkStack<ElemType> tmpS;
for(tmpPtr=top;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
tmpS.Push(tmpPtr->data);
for(tmpPtr=tmpS.top;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
(*visit)(tmpPtr->data);
}
template<typename ElemType>
bool LinkStack<ElemType>::Push(const ElemType &e)
{
Node<ElemType> *newTop=new Node<ElemType>(e,top);
if(newTop==NULL)//内存耗尽
return false;
else
{
top=newTop;
count++;
return true;
} }
template<typename ElemType>
bool LinkStack<ElemType>::Top(ElemType &e)const
{
if(Empty())
return false;
else
{
e=top->data;
return true;
} }
template<typename ElemType>
bool LinkStack<ElemType>::Pop(ElemType &e)
{
if(Empty())
return false;
else
{
Node<ElemType> *old_top=top;
top=old_top->next;
e=old_top->data;
count--;
delete old_top;
return true;
} }
template<typename ElemType>
LinkStack<ElemType>::LinkStack(const LinkStack<ElemType> ©)
{
if(Empty())
{
top=NULL;
count=;
}
else
{
top=new Node<ElemType>(copy.top->data);
count=copy.count;
node<ElemType> *buttomPtr=top;
for(Node<ElemType> *tmpPtr=copy.top->next;tmPtr!=NULL;tmpPtr=tmpPtr->next)
{
buttomPtr->next=new Node<ElemType> (tmpPtr->data);
buttomPtr=buttomPtr->next;
} }
}
template<typename ElemType>
LinkStack<ElemType> &LinkStack<ElemType>::operator=(const LinkStack<ElemType> ©)
{
if(©!=this)
{
if(copy.Empty())
{
top=NULL;
count=;
}
else
{
Clear();
top=new Node<ElemType>(copy.top->data);
count=copy.count;
Node<ElemType> *buttomTop=top;
for(Node<ElemType> *tmpPtr=copy.top->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
{
buttomTop->next=new node<ElemType>(tmpPtr->data);
buttomTop=buttomTop->next;
} }
} }
LinkStack的更多相关文章
- 栈的的链式实例LinkStack实现
1.#include <stdio.h>#include <malloc.h>#include "LinkList.h"typedef struct _ta ...
- LinkStack(链栈)
链栈即链式栈,也就是说我们不用再考虑空间的大小,可随心所欲的进行数据的插入/删除了.和顺序栈一样,仍然要保持其stack的特性,只在一端进行插入和删除,后进先出. (2018-02-14 代码更新) ...
- 数据结构(c语言第2版)-----了解链表,栈,队列,串
关于链表我觉得这都是最基本的东西,但是不常见,在实际的应用中很少的使用,了解它会用就OK,不需要研究的那么深,除非做那种内存压缩,存储方面工作. C语言中动态申请空间 malloc() q=(dlin ...
- Java链栈
package com.lxm.customDataStructure; public class LinkStack<T>{ class Node<T>{ T data; N ...
- 数据结构图文解析之:栈的简介及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- C#栈
线性表.栈和队列这三种数据结构的数据元素以及数据元素间的逻辑关系完全相同,差别是线性表的操作不受限制,而栈和队列的操作受到限制.栈的操作只能在表的一端进行, 队列的插入操作在表的一端进行而其它操作在表 ...
- C#实现堆栈
堆栈(Stack)是一种特殊的线性表,是一种操作只允许在尾端进行插入或删除等操作的线性表.表尾允许进行插入删除操作,称为栈顶(Top),另一端是固定的,称为栈底(Bottom).栈的操作使按照先进后出 ...
- 实现十进制无符号整数m到十六进制数的转换功能
/*利用顺序栈结构,编写算法函数void Dto16(unsigned int m)实现十进制无符号整数m到十六进制数的转换功能.*//******************************** ...
- 用JS描述的数据结构及算法表示——栈和队列(基础版)
前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
随机推荐
- jquery(select)下拉框 选取选中的值
var get_date_type=$("#date_type").find("option:selected").val(); var get_date_ty ...
- memcached使用文档
使用memcached进行内存缓存 通常的网页缓存方式有动态缓存和静态缓存等几种,在ASP.NET中已经可以实现对页面局部进行缓 存,而使用memcached的缓存比ASP.NET的局部缓存更加灵活, ...
- 手机cpu结构,arm
问题描述 今天测试人员测试集成版本时除了一个bug:关于华为 Mate 8手机Android 6.0系统运行刚刚提测的版本时,出现闪退的bug,而小米 4 手机Android 6.0系统却没有出现任何 ...
- Oracle的用户、角色以及权限相关操作
1.创建用户create user KD identified by 123456;2.授予连接数据库的权限grant connect to KD;3.将Scott用户的emp表授权给KD可以查询gr ...
- 没有在xml中引入 相关的配置文件
错误信息如下 严重: Servlet.service() for servlet AutoReplyServlet threw exception org.apache.ibatis.except ...
- ASP.NET Core 四种释放 IDisposable 对象的方法
本文翻译自<Four ways to dispose IDisposables in ASP.NET Core>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! IDispos ...
- AJAX学习笔记(一)基础知识
一.HTTP协议 1.HTTP: 计算机通过网络进行通讯的规则,用于浏览器向服务器发送请求. 2.HTTP是一种无状态的协议,无状态是指服务器端不保留任何连接相关的信息,浏览器客户端向服务器发送请求, ...
- Unity3D中使用BMFont制作图片字体 (NGUI版)
[旧博客转移 - 发布于2015年9月10日 16:07] 有时美术会出这种图片格式的文字,NGUI提供了UIFont来支持BMFont导出的图片字体 BMFont原理其实很简单,首先会把文字小图拼成 ...
- P3390 【模板】矩阵快速幂
题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格式: 输出A^k ...
- MyBatis源码解析【6】SqlSession运行
前言 这个分类比较连续,如果这里看不懂,或者第一次看,请回顾之前的博客 http://www.cnblogs.com/linkstar/category/1027239.html 经过之前的学习我们知 ...