采用链式存储结构,用一组任意的存储单元存在线性表元素,不需要分配存储空间,元素个数不受限制;
单链表结构和顺序存储结构的对比:
1) 时间复杂度:查找(顺序结构O(1),单链表O(n))插入和删除(顺序结构O(n),单链表O(1))
2) 若线性表频繁的进行查找,很少进行插入和删除操作,采用顺序存储结构;
若需要频繁的插入和删除,宜采用单链表结构;
public class SingleList {
public static void main(String[] args) {
LinkList list = new LinkList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add(1, "ddd");
System.out.println(list);
list.remove(2);
System.out.println(list);
System.out.println(list.get(1));
System.out.println(list.set(1, "eee"));
list.delete("eee");
System.out.println(list);
}
}
class Node<T> {
protected Node<T> next;// 指针域
public T data;// 数据域
public Node(T data) {
this.data = data;
}
// 显示此节点
public void display() {
System.out.println(data + " ");
}
}
class LinkList<T> {
public Node<T> first;// 定义一个头结点
private int pos = 0;// 节点的位置
// 初始化方法
public LinkList() {
this.first = null;
}
// 初始化方法
public LinkList(Node<T> first) {
this.first = first;
}
// 链表是否为空
public boolean isEmpty() {
return this.first == null;
}
// 返回单链表的长度
public int length() {
int i = 0;
Node<T> current = this.first;
while (current != null) {
i++;
current = current.next;
}
return i;
}
// 返回序号为index的对象 若单链表为空或序号错误则返回null
public T get(int index) {
int j = 0;
Node<T> current = this.first;
while (current != null && j < index) {
j++;
current = current.next;
}
if (current != null) {
return (T) current.data;
}
return null;
}
// 设置序号为index的对象为element 操作成功则返回该对象,失败则返回null
public T set(int index, T element) {
if (this.first != null && index > 0) {
Node<T> current = this.first;
while (current != null && pos < index) {
pos++;
current = current.next;
}
if (current != null) {
T old = (T) current.data;
current.data = element;
pos = 0;
return old;
}
}
return null;
}
// 在index后插入对象element 成功则返回true
public boolean add(int index, T element) {
if (element == null) {
return false;
}
if (this.first == null || index <= 0) {// 头插入
Node<T> current = new Node<T>(element);
current.next = this.first;
this.first = current;
} else {
Node<T> previous = this.first;
while (previous.next != null && pos < index) {
pos++;
previous = previous.next;
}
Node<T> current = new Node<T>(element);
current.next = previous.next;
previous.next = current;
pos = 0;
}
return true;
}
// 在单链表最后插入对象
public boolean add(T element) {
return add(Integer.MAX_VALUE, element);
}
// 移除序号为index的对象,若是操作成功则返回true;
public T remove(int index) {
T old = null;
if (this.first != null && index >= 0) {
if (index == 0) {// 头删除
old = (T) this.first.data;
this.first = this.first.next;
} else {
Node<T> current = this.first;
while (current.next != null && pos < index - 1) {
pos++;
current = current.next;
}
if (current.next != null) {
old = (T) current.next.data;
current.next = current.next.next;
}
}
}
return old;
}
// 清空单链表
public void clear() {
this.first = null;
}
public String toString() {
String str = "(";
Node<T> current = this.first;
while (current != null) {
str += current.data.toString();
current = current.next;
if (current != null) {
str += ",";
}
}
return str + ")";
}
// 根据节点的data 删除节点 (仅仅删除第一个)
public T delete(T data) {
Node<T> old = null;
Node<T> current = this.first;
while (!(current.data.equals(data))) {
if (current == null) {
return null;
}
old = current;
current = current.next;
}
if (current == first) {
this.first = first.next;
return (T) current;
} else {
old.next = current.next;
return (T) current;
}
}
}