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++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
随机推荐
- Python的迭代器与生成器
Python中的生成器和迭代器方便好用,但是平时对生成器和迭代器的特性掌握的不是很到位,今天将这方面的知识整理一下. 迭代器 为了更好的理解迭代器和生成,我们需要简单的回顾一下迭代器协议的概念. 迭代 ...
- Ext TabPanel tabbar添加按钮
tabPanel tabbar添加按钮 this.tabPanel = Ext.create('Ext.tab.Panel', { tabBar:{ items:[{ //组件靠右 xtype: 't ...
- poj1083,基本互斥问题
题意:南北两侧各有200个房间,两侧房间之间有一个走廊 现在需要把桌子从这400个房间之中搬进搬出,每一张桌子需要10分钟时间,如果走廊因为有桌子搬运而占用,则需等待,求共需多少时间(分钟)将桌子搬完 ...
- peoplesoft function PSTREENODE 通过 deptid 获得部门树 全路径 名称
create or replace function getUnitFullName(deptid in varchar) return varchar2 is r ); c int; n ); m ...
- SpringEL 表达式错误记录
原因暂时未知....
- Unity3D 物体移动到指定点
transform.position=Vector3.MoveTowards(transform.position , Target.position, speed * Time.deltaTime) ...
- Spring DelegatingFilterProxy解析
以前DelegatingFilterProxy是在需要使用spring security 的时候在xml中配置,如下: <filter> <filter-name>spring ...
- jmeter-Java关于MD5加密方法 以及16位32位互转
MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有MD5实现.将 ...
- 简单介绍Struts2
Struts2概述 Struts2虽然是Struts1的基础上发展起来的,但是实质上是以WebWork框架为核心,为传统的Struts1注入了WebWork的设计理念,统一了Struts1和WebWo ...
- ubuntu14.04_CUDA8.0_cudnn5.1_Tensorflow配置
深度学习框架tensorflow相比与caffe抽象层做的更好,即使用tensorflow的人不需要关心底层的实现,做底层实现的人不需要关心上层的模型和算法;caffe耦合比较紧凑,若想caffe用的 ...