/**
* 实现一个单链表: (1)查找--查找第index个节点;查找指定的元素 (2)插入--将指定的元素插入到第index个节点上
* (3)删除--将第index个节点删除
* 规律:删除和添加元素前务必保存两个元素的地址引用信息
*/
public class MyLinkedList<T> {
// 定义一个内部类来表示链表节点的数据结构
private class Node {
// 数据段
private T data;
// 地址段
private Node next;
// 节点的的初始化
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
// 记录链表结构的头结点地址引用
private Node head;
// 记录链表结构的尾节点地址引用
private Node tail;
// 记录链表结构中的节点数
private int size;
// 创建空链表
public MyLinkedList() {
this.head = null;
this.tail = null;
}
// 以一个指定元素来初始化该链表
public MyLinkedList(T data) {
head = new Node(data, null);
tail = head;
size++;
}
// 返回链表的长度
public int length() {
return size;
}
// 获取链表索引为index处的数据
public Node get(int index) {
// 参数容错
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("链表索引越界");
}
// 单链表遍历
Node current = head;
// 统计已经遍历的节点的个数
int count = 0;
while (null != current) {
if (count == index) {
return current;
}
count = count + 1;
current = current.next;
}
return null;
}
// 查找指定元素
public int get(T element) {
// 单链表遍历
Node current = head;
// 记录玩家已遍历的节点的个数
int count = 0;
while (current != null) {
if (current.data.equals(element)) {
return count;
}
count = count + 1;
current = current.next;
}
return -1;
}
// 向指定位置插入一个元素
public void insert(T element, int index) {
// 参数容错
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("链表索引越界");
}
if (size == 0) {
// 空链表情形
head = new Node(element, null);
tail = head;
} else {
// 非空链表情形
if (index == 0) {
// 从头部插入
Node old = head;
head = new Node(element, null);
head.next = old;
} else if (index == size) {
// 从尾部插入
Node old = tail;
tail = new Node(element, null);
old.next = tail;
} else {
// 从中部插入:遍历单链表
Node current = head;
// 记录玩家已经遍历的节点数
int count = 0;
while (current != null) {
// 保存插入节点的前一节点和后一节点的历史信息
if (count == index) {
Node pre = current;
Node next = current.next;
current = new Node(element, next);
pre.next = next;
}
count = count + 1;
current = current.next;
}
}
}
// 别忘了:size增1
size = size + 1;
}
// 头插法插入一个节点
public void addAtHead(T element){
if (head == null) {
// 空链表情形
head = new Node(element, null);
tail = head;
} else {
// 非空链表情形
// 保存原头结点及第一个节点的信息
Node pre = head;
Node next = head.next;
head = new Node(element, pre);
pre.next = next;
}
size = size + 1;
}
// 尾插法插入一个节点
public void addAtTail(T element){
if (head == null) {
// 空链表情形
head = new Node(element, null);
tail = head;
} else {
// 非空链表情形
// 保存尾结点及前一个节点的信息
Node pre = tail;
tail = new Node(element, null);
pre.next = tail;
}
size = size + 1;
}
// 删除指定索引位置的元素
public T remove(int index) {
// 参数容错
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("链表索引越界");
}
if (head == null) {
// 空链表
return null;
} else {
// 单链表的遍历
Node current = head;
// 记录玩家已经遍历的节点数
int count = 0;
while (current != null) {
if (count == index - 1) {
// 只需找到要删除的节点的前一节点就好了,保存要删除的节点和前一节点的信息
Node old = current.next;
Node pre = current;
current.next = null;
pre.next = old.next;
return old.data;
}
count = count + 1;
current = current.next;
}
return null;
}
}
// 删除指定元素
public Node remove(T element) {
if (head == null){
// 空链表情形
return null;
} else {
// 非空链表情形
// 单链表的遍历
Node current = head;
// 记录玩家已经遍历的节点数
int count = 0;
while (current != null) {
if (current.data.equals(element)) {
// 只需找到要删除的节点的前一节点就好了,保存要删除的节点和前一节点的信息
Node pre = this.get(count -1);
Node old = current;
current = null;
pre.next = old.next;
return old;
}
count = count + 1;
current = current.next;
}
}
return null;
}
* 实现一个单链表: (1)查找--查找第index个节点;查找指定的元素 (2)插入--将指定的元素插入到第index个节点上
* (3)删除--将第index个节点删除
* 规律:删除和添加元素前务必保存两个元素的地址引用信息
*/
public class MyLinkedList<T> {
// 定义一个内部类来表示链表节点的数据结构
private class Node {
// 数据段
private T data;
// 地址段
private Node next;
// 节点的的初始化
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
// 记录链表结构的头结点地址引用
private Node head;
// 记录链表结构的尾节点地址引用
private Node tail;
// 记录链表结构中的节点数
private int size;
// 创建空链表
public MyLinkedList() {
this.head = null;
this.tail = null;
}
// 以一个指定元素来初始化该链表
public MyLinkedList(T data) {
head = new Node(data, null);
tail = head;
size++;
}
// 返回链表的长度
public int length() {
return size;
}
// 获取链表索引为index处的数据
public Node get(int index) {
// 参数容错
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("链表索引越界");
}
// 单链表遍历
Node current = head;
// 统计已经遍历的节点的个数
int count = 0;
while (null != current) {
if (count == index) {
return current;
}
count = count + 1;
current = current.next;
}
return null;
}
// 查找指定元素
public int get(T element) {
// 单链表遍历
Node current = head;
// 记录玩家已遍历的节点的个数
int count = 0;
while (current != null) {
if (current.data.equals(element)) {
return count;
}
count = count + 1;
current = current.next;
}
return -1;
}
// 向指定位置插入一个元素
public void insert(T element, int index) {
// 参数容错
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("链表索引越界");
}
if (size == 0) {
// 空链表情形
head = new Node(element, null);
tail = head;
} else {
// 非空链表情形
if (index == 0) {
// 从头部插入
Node old = head;
head = new Node(element, null);
head.next = old;
} else if (index == size) {
// 从尾部插入
Node old = tail;
tail = new Node(element, null);
old.next = tail;
} else {
// 从中部插入:遍历单链表
Node current = head;
// 记录玩家已经遍历的节点数
int count = 0;
while (current != null) {
// 保存插入节点的前一节点和后一节点的历史信息
if (count == index) {
Node pre = current;
Node next = current.next;
current = new Node(element, next);
pre.next = next;
}
count = count + 1;
current = current.next;
}
}
}
// 别忘了:size增1
size = size + 1;
}
// 头插法插入一个节点
public void addAtHead(T element){
if (head == null) {
// 空链表情形
head = new Node(element, null);
tail = head;
} else {
// 非空链表情形
// 保存原头结点及第一个节点的信息
Node pre = head;
Node next = head.next;
head = new Node(element, pre);
pre.next = next;
}
size = size + 1;
}
// 尾插法插入一个节点
public void addAtTail(T element){
if (head == null) {
// 空链表情形
head = new Node(element, null);
tail = head;
} else {
// 非空链表情形
// 保存尾结点及前一个节点的信息
Node pre = tail;
tail = new Node(element, null);
pre.next = tail;
}
size = size + 1;
}
// 删除指定索引位置的元素
public T remove(int index) {
// 参数容错
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("链表索引越界");
}
if (head == null) {
// 空链表
return null;
} else {
// 单链表的遍历
Node current = head;
// 记录玩家已经遍历的节点数
int count = 0;
while (current != null) {
if (count == index - 1) {
// 只需找到要删除的节点的前一节点就好了,保存要删除的节点和前一节点的信息
Node old = current.next;
Node pre = current;
current.next = null;
pre.next = old.next;
return old.data;
}
count = count + 1;
current = current.next;
}
return null;
}
}
// 删除指定元素
public Node remove(T element) {
if (head == null){
// 空链表情形
return null;
} else {
// 非空链表情形
// 单链表的遍历
Node current = head;
// 记录玩家已经遍历的节点数
int count = 0;
while (current != null) {
if (current.data.equals(element)) {
// 只需找到要删除的节点的前一节点就好了,保存要删除的节点和前一节点的信息
Node pre = this.get(count -1);
Node old = current;
current = null;
pre.next = old.next;
return old;
}
count = count + 1;
current = current.next;
}
}
return null;
}