public class ChainTable {

private Node firstNode;//第一个节点

private Node lastNode;//最后一个节点

private int size;//链表中含有的元素个数

public int size(){//返回链表中含有的元素个数

return size;

}

//添加一个节点

public void add(Object obj){

if(size == 0){//当添加的节点为第一个节点的时候

Node node = new Node();

node.setObj(obj);

lastNode = node;

firstNode = node;

firstNode.setNextNode(lastNode);

lastNode.setNextNode(null);

size++;

}else{

Node node = new Node();

node.setObj(obj);

lastNode.setNextNode(node);

lastNode = node;

size++;

}

}

//添加一个链表

public void add(ChainTable table) throws Exception{

if(table != null){

for(int i=0;i<table.size();i++){

this.add(table.get(i));

}

}

}

//获取所在索引的节点中的值

public Object get(int index) throws Exception{

return getNode(index).getObj();

}

//获得对应索引的节点

private Node getNode(int index) throws Exception{

//让node 指向第一个节点

Node node = firstNode;

//保证index在正常区间内

if(index < 0 || index >= size){

throw new Exception();

}else{

for(int i=0;i<index;i++){

node = node.getNextNode();

}

}

return node;

}

//删除所在索引的值

public void remove(int index) throws Exception {

if(index < 0 | index >=size){

throw new Exception();

}

if(index == 0){

firstNode = firstNode.getNextNode();

}else if (index == (size -1)){

Node upNode = getNode(size -2);

lastNode = upNode;

}else{

Node upNode = getNode(index - 1);

Node node = upNode.getNextNode();

Node nextNode = node.getNextNode();

upNode.setNextNode(nextNode);

node = null;

}

size--;

}

//判断是否包含某个元素

public boolean contain(Object obj) throws Exception{

return indexOf(obj) >= 0;

}

//返回obj的索引  若没有此元素 则返回-1

public int indexOf(Object obj) throws Exception{

int index = -1;

if(obj == null){

for(int i=0;i<size - 1;i++){

if(get(i) == null){

index = i;

break;

}

}

}else{

for(int i=0;i<size -1;i++){

if(get(i).equals(obj)){

index = i;

break;

}

}

}

return index;

}

//内部类Node

public class Node{

private Object obj;//数据域

private Node nextNode;//指针域  指向下一个节点

public Node(){//构造方法

}

public Node(Object obj,Node nextNode){//构造方法

this.obj = obj;

this.nextNode = nextNode;

}

public Object getObj() {//获取数据

return obj;

}

public void setObj(Object obj) {//设置数据

this.obj = obj;

}

public Node getNextNode() {//获得下一个节点

return nextNode;

}

public void setNextNode(Node nextNode) {//设置下一个节点

this.nextNode = nextNode;

}

}

}

单向链表仿LinkedList的更多相关文章

  1. JS数据结构与算法--单向链表

    链表结构:链表中每个元素由一个存储元素本身的节点和一个指向下一元素的引用组成.如下所示(手画的,比较丑,懒得用工具画了,嘻嘻) 1.append方法,向链表末尾插入一个节点 2.insert(posi ...

  2. 数据结构(1) 第一天 算法时间复杂度、线性表介绍、动态数组搭建(仿Vector)、单向链表搭建、企业链表思路

    01 数据结构基本概念_大O表示法 无论n是多少都执行三个具体步骤 执行了12步 O(12)=>O(1) O(n) log 2 N = log c N / log c N (相当于两个对数进行了 ...

  3. Python 单向链表、双向链表

    用面向对象实现Linkedlist链表 单向链表实现append.iternodes 双向链表实现append.pop.insert.remove.iternodes 单向链表与双向链表 单向链表: ...

  4. [Java算法分析与设计]--单向链表(List)的实现和应用

    单向链表与顺序表的区别在于单向链表的底层数据结构是节点块,而顺序表的底层数据结构是数组.节点块中除了保存该节点对应的数据之外,还保存这下一个节点的对象地址.这样整个结构就像一条链子,称之为" ...

  5. 玩转C线性表和单向链表之Linux双向链表优化

    前言: 这次介绍基本数据结构的线性表和链表,并用C语言进行编写:建议最开始学数据结构时,用C语言:像栈和队列都可以用这两种数据结构来实现. 一.线性表基本介绍 1 概念: 线性表也就是关系户中最简单的 ...

  6. 数据结构与算法之链表(LinkedList)——简单实现

    这一定要mark一下.虽然链表的实现很简单,且本次只实现了一个方法.但关键的是例子:单向链表的反转.这是当年我去H公司面试时,面试官出的的题目,而当时竟然卡壳了.现在回想起来,还是自己的基本功不扎实, ...

  7. JAVA 单向链表

    package com.session.link; /** * 单向链表 */public class LinkedList<T> { private Node head;//指向链表头节 ...

  8. C语言实现简单的单向链表(创建、插入、删除)及等效STL实现代码

    实现个算法,懒得手写链表,于是用C++的forward_list,没有next()方法感觉很不好使,比如一个对单向链表的最简单功能要求: input: 1 2 5 3 4 output: 1-> ...

  9. JS实现单向链表、双向链表、循环链表

    https://cloud.tencent.com/developer/article/1114246 链表存储有序的元素的集合,但是和数组不同的是,链表中的元素在内存中的存储并不是连续的.每一个链表 ...

随机推荐

  1. UML和模式应用学习笔记-1(面向对象分析和设计)

    UML和模式应用学习笔记-1(面向对象分析和设计) 而只是对情节的记录:此处的用例场景为:游戏者请求掷骰子.系统展示结果:如果骰子的总点数是7,则游戏者赢得游戏,否则为输 (2)定义领域模型:在领域模 ...

  2. ios开发之路十一(ARC forbids explicit message send of 'autorelease'错误)

    在ios中经常会遇到:ARC forbids explicit message send of 'autorelease' 或“ARC forbids explicit message send of ...

  3. 也来“玩”Metro UI之磁贴

    也来“玩”Metro UI之磁贴 Win8出来已有一段时间了,个人是比较喜欢Metro UI的.一直以来想用Metro UI来做个自己的博客,只是都没有空闲~~今天心血来潮,突然想自己弄一个磁贴玩玩, ...

  4. 简明CSS属性:定位

    简明CSS属性:定位 第一话 定位 (Positioning) 关键词:position/z-index/top/bottom/right/left/clip POSITION 该属性用来决定元素在页 ...

  5. Your personal Mail Server iRedMail on ubuntu14.04 x64

    what we have? iRedMail -> http://iredmail.com Get the script over there.          http://www.ired ...

  6. 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载

    在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符. []运算符重载 +运算符重载 +=运算符重载 <<运算符重载 >>运算符重 ...

  7. c数据结构学习随笔

    #include <stdio.h> #include <stdlib.h> #include "PublicDS.h" #include<Windo ...

  8. 从Chrome源码看浏览器如何构建DOM树

    .aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } p { font-size: 1 ...

  9. C++ STD inner_product函数

    C++ STD函数   inner_product是c++标准库封装的一个函数. 函数原型: 函数1: inner_product(beg1, end1, beg2, init) 函数2: inner ...

  10. overflow属性及其在ios下卡顿问题解决

    overflow属性:http://www.w3school.com.cn/cssref/pr_pos_overflow.asp overflow:scroll/auto;在手机页面滑动不流畅问题: ...