巩固数据结构 单链表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;
}
}

  

最新文章

  1. php程序员绝不能违背的安全铁则
  2. 在jsp页面解析json的2种方法
  3. 把谷歌等webkit内核浏览器变为输入文本编辑器的方法
  4. psy 2
  5. thinkphp5.0 架构
  6. 所有事件event集锦
  7. An overnight dance in discotheque
  8. 如何线上部署node.js项目
  9. CentOS下添加Root权限用户(超级用户)方法
  10. EmberJS 为什么我偏爱 Ember.js 胜过 Angular 和 React.js
  11. Eclipse ee项目 Java Resources文件报错解决方法
  12. 省市区三级联动——思路、demo、示例
  13. 第32章:MongoDB-索引--Capped固定集合
  14. Hadoop基础-MapReduce的Join操作
  15. 超人前传第一至十季/全集Smallville迅雷下载
  16. 使用FIO测试磁盘iops
  17. 【教程】【FLEX】#006 控件位置的拖动
  18. windows下thrift的使用(python)
  19. Socket编程(一):建立与客户端的连接并接受数据
  20. resnet densenet

热门文章

  1. 【转载】Java IO 转换流 字节转字符流
  2. SPLAY or SPALY ?
  3. appium的截图
  4. 深度遍历DFS---树
  5. Lua的热更新学习笔记_01
  6. Ubuntu安装RTX2080显卡驱动
  7. JavaScript获取日期方法
  8. shell脚本编程及bash特性
  9. 从命令行配置 Windows 防火墙
  10. isap算法模板poj 1273gap+弧优化 最大流