链表是一种物理存储单元上非连续、逻辑上连续的线性存储结构。链表由一系列结点组成每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。与链表相关的操作包括:add(向链表中插入元素)、isEmpty(判断链表是否为空)、updata(更新链表中的元素)、getSize(获取链表的长度)、contains(判断链表中是否存在这个元素)、remove(删除链表中的结点)。下面是使用JAVA实现的单链表。
public class SingleList<T>{
final private Node head;
private int length;
private Node last;
//定义Node,每个Node对象就是一个节点
class Node{
T data;
Node next;
//构造器
public Node(T data){
this.data = data;
}
}
//创建一个空链表
public SingleList() {
//先初始化一个头结点,头结点不要移动,不存放具体的数据
//泛型参数是不能new出对象的,只能通过强转Object对象实现
head = new Node((T)new Object());
length = 0;
}
//添加节点到单向链表中
/*
思路,当不考虑顺序时
1.找到当前链表的最后节点
2.找到最后一个节点的next指向新节点
*/
public void add(T t) {
Node newnode = new Node(t);//插入到末尾
if(last != null)
last.next = newnode;
else
head.next = newnode;
last = newnode;
length++;
}
public void add(T t,int index){ //插入范围在0-length
Node node = new Node(t);
//使用一个指针遍历链表,找到节点
Node pointer = GetElem(index);
node.next = pointer.next; //插入节点
pointer.next = node;
length++;
}
//判断链表是否为空
public boolean isEmpty() {
if (last == null)
return true;
return false;
}
//更新链表中的节点
public void update(int index, T t) {
Node node = GetElem(index);
node.data = t;
}
//得到链表的长度
public int getSize() {
return length;
}
public boolean contains(T t){
return contains(head.next,t);
}
private boolean contains(Node node,T t){
while(node != null) {
if(t.equals(node.data))
return true;
else {
node = node.next;
}
}
return false;
}
//根据下标删除结点
public void remove_index(int index){
if(!isEmpty()){
Node node = GetElem(index);
node.next = node.next.next;
}
}
//根据元素值删除结点
public void remove(T t){
remove(head.next,t);
}
private void remove(Node node,T t){
Node ahead = head;
while(node != null) {
if(t.equals(node.data)){
ahead.next = node.next;
if(node == last) last = ahead;
return;
}
else {
ahead = node;
node = node.next;
}
}
}
//尾插法插入元素
public void addLast(T t){
add(t);
}
//检查索引是否越界
private void check(int index){
if(index>length || index<0) //下标从0开始
throw new IndexOutOfBoundsException("索引越界!");
}
//获取第index个元素
public Node GetElem(int index){
check(index);
Node node = head;
for(int i=0;i<index;i++)
node = node.next;
return node;
}
//头插法插入元素
public void addFirst(T t){
Node newNode = new Node(t);
newNode.next = head.next;
head.next = newNode;
length++;
}
//按位查找 平均:O(n) 最坏:O(n) 最好:O(1)
public Node put(int index){
if(isEmpty())
return null;
return GetElem(index+1);
}
public String toString(){
StringBuilder sb = new StringBuilder();
Node pointer = head.next;
while (pointer != null) {
if(pointer == last)
sb.append(pointer.data);
else
sb.append(pointer.data+",");
//指针移动到下一个节点
pointer = pointer.next;
}
return sb.toString();
}
}