//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. [leetcode-513-Find Bottom Left Tree Value]

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input:  2 / \ 1 ...

  2. 【LeetCode】237. Delete Node in a Linked List

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  3. java怎么处理json数据

    json = new JSONObject(data); int which = json.optInt("which", -1); String label = json.opt ...

  4. laravel blade模板介绍

    第一步:首先写好模板 在sites下新建立一个app.blade.php文件 写入模板如: <!DOCTYPT html><html><head> <titl ...

  5. c# ActiveMQ 类

    using System;using System.Collections.Generic;using System.Text; using Apache.NMS;using Apache.NMS.A ...

  6. ubuntu主机名修改

    1.查看主机名 在Ubuntu系统中,快速查看主机名有多种方法: 其一,打开一个GNOME终端窗口,在命令提示符中可以看到主机名,主机名通常位于"@"符号后: 其二,在终端窗口中输 ...

  7. Android6.0 中appcompat_v7 报错

    更新了AndroidSDK以后 各种错误,新建一个项目会附赠一个appcompat_v7,你只要知道这个是一个兼容包就可以了,具体的特性可以看相关介绍,其实也没啥特别的就是为了兼容低版本的呗, 但是呢 ...

  8. 如何通过css设置表格居中

    CSS控制整个表格居中,不只是让表格里的文字居中,是整个表格居中1. 不用table的Align="center",要用CSS实现2. 不加<center></c ...

  9. 看懂 ,学会 .NET 事件的正确姿势

    一.事件的本质       举个例子你是个取向正常的青年男性,有个身材火辣,年轻貌美,腿长肤白的美女,冲你一笑,给你讲了一个ABCD羊吃草的故事.你有什么反应?可能你关注点在于颜值,身材,故事,故事含 ...

  10. python之基本内容

    这里提供在使用python进行开发中常使用到的方法技巧,如有不对欢迎批评指正. 要点:开发中类.变量特性查询,类型就是类,断言的使用,深浅复制判断等 python脚本文件是使用UTF-8编码的,所以在 ...