1.关于单链表
特点: 数据元素的存储对应的是不连续的存储空间。 每个结点是由数据域和指针域组成。 逻辑上相邻的节点物理上不必相邻。
缺点: 比顺序存储结构的存储密度小 (每个节点都由数据域和指针域组成,所以相同空间内假设全存满的话顺序比链式存储更多)。 按照索引查找效率低下。
优点: 插入、删除灵活 有元素才会分配结点空间,不会有闲置的结点。
2. 单链表的源码解析
(1)定义一个List接口
public interface List {
public int size();
public Object get(int i);
public boolean isEmpty();
public boolean contains(Object e);
public int indexOf(Object e);
public void add(int i, Object e);
public void add(Object e);
public boolean addBefore(Object obj, Object e);
public boolean addAfter(Object obj, Object e);
public Object remove(int i);
public boolean remove(Object e);
public Object replace(int i, Object e);
public Iterator iterator();
}
(2)创建一个节点类Node
public class Node {
Object data;
Node next;
public Node() {
}
public Node(Object data) {
this.data = data;
}
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
@Override
public String toString() {
return "Node{" +
"data=" + data +
", next=" + next +
'}';
}
(3)定义一个SingleLinkedList,实现List接口
package com.yue.list2;
import com.yue.list.Iterator;
public class SingleLinkedList implements List {
private Node head = new Node();
private int size;
@Override
public int size() {
return size;
}
@Override
public Node get(int i) {
if (i < 0 || i > size) {
throw new RuntimeException("索引越界");
}
Node p = head;
for (int j = 0; j <= i; j++) {
p = p.next;
}
return p;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public boolean contains(Object e) {
return indexOf(e) >= 0;
}
@Override
public int indexOf(Object e) {
return 0;
}
@Override
public void add(int i, Object e) {
Node p = head;
if (i > 0) {
p = get(i - 1);
}
Node newNode = new Node(e);
newNode.next = p.next;
p.next = newNode;
size++;
}
@Override
public void add(Object e) {
add(size, e);
}
@Override
public boolean addBefore(Object obj, Object e) {
return false;
}
@Override
public boolean addAfter(Object obj, Object e) {
return false;
}
@Override
public Object remove(int i) {
Node p = head;
if (i > 0) {
p = get(i - 1);
}
Node currentNode = p.next;
p.next = p.next.next;
currentNode.next = null;
size--;
return null;
}
@Override
public boolean remove(Object e) {
return false;
}
@Override
public Object replace(int i, Object e) {
return null;
}
@Override
public Iterator iterator() {
return null;
}
public String toString() {
StringBuilder builder = new StringBuilder("[");
Node p = head;
for (int i = 0; i < size; i++) {
p = p.next;
builder.append(p.data + ",");
}
if (size != 0) {
builder.deleteCharAt(builder.length() - 1);
}
builder.append("]");
return builder.toString();
}
}
(4)测试类TestList
public class TestList {
public static void main(String[] args) {
List list = new SingleLinkedList();
list.add("11111");
list.add("aaaaa");
list.add("bbbbb");
list.add("33333");
list.add("22222");
list.add(3, "AAAAA");
System.out.println(list.get(0));
list.remove(2);
System.out.println(list.size());
System.out.println(list.get(0));
System.out.println(list.isEmpty());
System.out.println(list.contains("44444"));
System.out.println(list.indexOf("22222"));
System.out.println(list.toString());
}
}
运行结果
|