巩固数据结构 单链表java实现 单链表除了表尾 每个几点都有一个后继 结点有数据和后继指针组成  通过构建表头和表尾(尾部追加需要)两个特殊几点 实现单链表的一些操作,代码如下

package com.shine.test.datastruct;

/**
* 简易链表
*
* @author SHINE
*
* @param <E>
*/
public class LinkList<E> { private Node head, tail;
private int size = 0; protected class Node<E> {
E data;
Node next;
} public LinkList() {
head = new Node();
head.next = null;
tail = new Node<E>();
} public void insert(E e, int index) {
Node newNode = new Node<E>();
newNode.data = e; Node before = head;
Node temp = head.next;
while (index >= 0) {
if (index == 0) {
before.next = newNode;
newNode.next = temp;
size++;
break;
}
before = temp;
temp = temp.next;
index--;
}
} public void add(E e) {
Node node = new Node();
node.data = e;
node.next = null;
if (head.next == null) {
head.next = node;
}
tail.next = node;
tail = node;
size++;
} public E get(int index) {
if (index > size - 1) {
return null;
}
Node temp = head.next;
while (index >= 0) {
if (index == 0) {
return (E) temp.data; }
temp = temp.next;
index--;
}
return null;
} public E getFirst() {
return get(0);
} public E getLast() {
return get(size - 1);
} public void remove(E e) {
Node before = head;
Node temp = head.next;
while (temp != null) {
if (temp.data.equals(e)) {
before.next = temp.next;
size--;
break;
}
before = temp;
temp = temp.next;
}
} @Override
public String toString() {
StringBuffer sb = new StringBuffer();
Node temp = head.next;
while (temp != null) {
sb.append(temp.data + "->");
temp = temp.next;
}
return sb.toString();
} public int getLength() {
return size;
}
}

  

数据结构——单链表java简易实现的更多相关文章

  1. python实现数据结构单链表

    #python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...

  2. C语言数据结构-单链表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作

    1.数据结构-单链表的实现-C语言 typedef struct LNode { int data; struct LNode* next; } LNode,*LinkList; //这两者等价.Li ...

  3. Java数据结构--单链表

    #java学习经验总结------单链表的建立与结点的增删 在该链表结点有data数据,并且还有cpu,分给cpu随机的时间片,根据时间片大小进行结点data的排序 链表结点的建立 class Lin ...

  4. JAVA数据结构——单链表

    链表:一. 顺序存储结构虽然是一种很有用的存储结构,但是他有如下几点局限性:1. 因为创造线性表的时候已经固定了空间,所以当需要扩充空间时,就需要重新创建一个地址连续的更大的存储空间.并把原有的数据元 ...

  5. java数据结构——单链表、双端链表、双向链表(Linked List)

    1.继续学习单链表,终于摆脱数组的魔爪了,单链表分为数据域(前突)和引用域(指针域)(后继),还有一个头结点(就好比一辆火车,我们只关心火车头,不关心其它车厢,只需知晓车头顺藤摸瓜即可),头结点没有前 ...

  6. 单链表Java实现

    近期在复习基本数据结构,本文是单链表的Java实现,包含对单链表的实现插入删除查找遍历等.最后还实现了单链表的逆置. 实现了多项式相加,多项式相乘. 原文章及完整源码在这里 http://binhua ...

  7. C# 数据结构--单链表

    什么是单链表 这两天看到很多有关单链表的面试题,对单链表都不知道是啥的我.经过学习和整理来分享一下啥是单链表和单链表的一些基本使用方法.最后看些网上有关单链表的面试题代码实例. 啥是单链表? 单链表是 ...

  8. 数据结构-------单链表(C++)

    相关信息: /** * @subject 数据结构 实验2 * @author 信管1142班 201411671210 赖俊杰 * @project 单链表 * @time 2015年10月29日1 ...

  9. 单链表---java实现

    单链表优点:1.不需要预先给出元素个数. 2.单链表插入删除时不需要移动数据元素. 单链表缺点:1.每个节点有指针,空间利用率低. 2.单链表不支持随机读取数据. Node.java package ...

随机推荐

  1. CF319E Ping-Pong 线段树 + vector + 思维

    Code: #include<bits/stdc++.h> #define N 3000009 #define maxn 3000009 #define ll long long #def ...

  2. [基准测试]----lmbench

    引言 要评价一个系统的性能,通常有不同的指标,相应的会有不同的测试方法和测试工具,一般来说为了确保测试结果的公平和权威性,会选用比较成熟的商业测试软件.但在特定情形下,只是想要简单比较不同系统或比较一 ...

  3. 爬虫系列(二) Chrome抓包分析

    在这篇文章中,我们将尝试使用直观的网页分析工具(Chrome 开发者工具)对网页进行抓包分析,更加深入的了解网络爬虫的本质与内涵 1.测试环境 浏览器:Chrome 浏览器 浏览器版本:67.0.33 ...

  4. GitLab权限介绍

    访问权限 - Visibility Level 这个是在建立项目时就需要选定的,主要用于决定哪些人可以访问此项目,包含3种 Private - 私有,只有属于该项目成员才有原先查看 Internal ...

  5. 防火墙对FTP的影响

    http://www.5iadmin.com/post/919.html FTP 常用命令 http://www.cnblogs.com/emanlee/archive/2012/07/09/2583 ...

  6. 通过winrm使用powershell远程管理服务器

    原文地址 在Linux中,我们可以使用安全的SSH方便的进行远程管理.但在Windows下,除了不安全的Telnet以外,从Windows Server 2008开始提供了另外一种命令行原创管理方式, ...

  7. 手把手实现Java权限(1)-Shiro介绍

    功能介绍 Authentication :身份认证/登录.验证用户是不是拥有对应的身份:  Authorization :授权,即权限验证.验证某个已认证的用户是否拥有某个权限:即推断用  户能否做事 ...

  8. NEFU 115

    刚开始,做了水题 #include <iostream> #include <cstdio> #include <algorithm> using namespac ...

  9. 关系型数据库与HBase的数据储存方式差别

    现在Bigtable型(列族)数据库应用越来越广,功能也非常强大. 可是非常多人还是把它当做关系型数据库在使用,用原来关系型数据库的思维建表.存储.查询. 本文以hbase举例讲述数据模式的变化. 传 ...

  10. Flex布局 Flexbox属性具体解释

    原文:A Visual Guide to CSS3 Flexbox Properties Flex布局官方称为CSS Flexble Box布局模型是CSS3为了提高元素在容器中的对齐.方向.顺序,甚 ...