//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> &copy);
LinkStack<ElemType> &operator=(const LinkStack<ElemType> &copy);
};
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> &copy)
{
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> &copy)
{
if(&copy!=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的更多相关文章

  1. 栈的的链式实例LinkStack实现

    1.#include <stdio.h>#include <malloc.h>#include "LinkList.h"typedef struct _ta ...

  2. LinkStack(链栈)

    链栈即链式栈,也就是说我们不用再考虑空间的大小,可随心所欲的进行数据的插入/删除了.和顺序栈一样,仍然要保持其stack的特性,只在一端进行插入和删除,后进先出. (2018-02-14 代码更新) ...

  3. 数据结构(c语言第2版)-----了解链表,栈,队列,串

    关于链表我觉得这都是最基本的东西,但是不常见,在实际的应用中很少的使用,了解它会用就OK,不需要研究的那么深,除非做那种内存压缩,存储方面工作. C语言中动态申请空间 malloc() q=(dlin ...

  4. Java链栈

    package com.lxm.customDataStructure; public class LinkStack<T>{ class Node<T>{ T data; N ...

  5. 数据结构图文解析之:栈的简介及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  6. C#栈

    线性表.栈和队列这三种数据结构的数据元素以及数据元素间的逻辑关系完全相同,差别是线性表的操作不受限制,而栈和队列的操作受到限制.栈的操作只能在表的一端进行, 队列的插入操作在表的一端进行而其它操作在表 ...

  7. C#实现堆栈

    堆栈(Stack)是一种特殊的线性表,是一种操作只允许在尾端进行插入或删除等操作的线性表.表尾允许进行插入删除操作,称为栈顶(Top),另一端是固定的,称为栈底(Bottom).栈的操作使按照先进后出 ...

  8. 实现十进制无符号整数m到十六进制数的转换功能

    /*利用顺序栈结构,编写算法函数void Dto16(unsigned int m)实现十进制无符号整数m到十六进制数的转换功能.*//******************************** ...

  9. 用JS描述的数据结构及算法表示——栈和队列(基础版)

    前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...

随机推荐

  1. ajax请求返回数据,模板中的数据处理

    /*ajax请求返回数据,模板中的数据处理*/ function QueryGameAsset(){ var new_start_time=$('#new_start_time').val();//开 ...

  2. 用超链接a来提交form表单

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  3. juggle dsl语法介绍及codegen浅析

    juggle语法规范如下: 类型: bool -> in cpp bool int -> in cpp int64 float -> in cpp double string -&g ...

  4. MySQL 5.7 在windows下修改max_allowed_packet变量

    (一)执行sql遇到的错误如下: ### Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (387 ...

  5. python 三级菜单 while循环三次,湖北省市-县-街道的选择,3个while的循环 -day2

    python编写一个三级while的循环菜单 1.定义字典,字典里面嵌套字典,内嵌字典的值为列表. 思路: 湖北省的市:字典中的定义3个字典,用于存储{序列-键:市名} shiqu_dir = {} ...

  6. mongodb远程连接配置

    mongodb远程连接配置如下: 1.修改配置文件mongodb.conf 命令:vim /etc/mongodb.conf 把 bind_ip=127.0.0.1 这一行注释掉或者是修改成 bind ...

  7. Android 测试 Appium、Robotium、monkey等框架或者工具对比

    1. Appium测试 (功能测试,用户接受度测试,黑盒测试) - Rating: 8 Appium测试相当于黑盒测试.只是测试UI逻辑正确性.所以Appium测试框架提供的方法有限.获取一个Appi ...

  8. Chrome浏览器扩展开发系列之七:override页面

    Chrome浏览器通常提供了一些默认页面,如标签管理器页面chrome://bookmarks.浏览历史记录页面chrome://history或新建Tab页面chrome://newtab等. Ch ...

  9. CentOS7 yum安装zabbix3.2.6

    前言: 本人小白,在一个多月前通过面试进入公司,在进入公司的第一天,老板把我叫到他办公室,坐下来慢慢喝茶,吹牛,给我吹他们以前做的软件,经营的产品,还装作一副什么都告诉我的样子,其实这都是套路,我早已 ...

  10. P3390 【模板】矩阵快速幂

    题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格式: 输出A^k ...