单链表基础操作的Java实现

链表是很多的数据结构的基础,比如说:队列,栈,二叉树,优先级队列等等,而链表也是很多公司面试和笔试的常考题。
链表的基本操作包括:判断是否为空,头部插入,尾部插入,根据key值查找,根据key值删除,遍历链表。
当然稍微复杂一点的操作还包括:链表的逆序,链表的排序等等。
在链表中,包含两个类:Node(节点)

package com.qiao.lb;
public class Node {
public long data;
public Node next;
public Node(long data){
    this.data=data;
}
public void displayLink(){
    System.out.print("{"+data+"},");
}
}

当然这个Node类可以根据需要变得很复杂。
LinkList类:
LinkList类中只包含一个数据项,即对链表中第一个链节点的引用,叫做first,它是唯一的链表需要维护的永久信息,用以定位所有其他的链节点。从first出发,沿着链表通过每个链节点的next字段,就可以找到其他的链节点:

class LinkList{
 private Link first;
 public void LinkList(){
    first=null;//表示目前为止链表中没有数据项,如果有,first字段中应该存有对第一个链接点的引用值
 }
}

在LinkList类中包括对链表的一系列操作,包括增删改查等等。
其中最核心的是insertFirst()方法和insertLast()方法,其方法中对链表的操作思路在其他的方法会用到。
不多说了,把全部的代码粘贴如下(具体的操作大家可以参考我的资源《Java数据结构和算法》这本书):

package com.qiao.lb;

public class LianBiao {
//最好不要用尾插法,因为每次尾插法都需要遍历整个链表
private Node first;
public LianBiao(){
    first=null;
}
public boolean isEmpty(){
    return first==null;
}
public void insertFirst(long data){
    Node newNode=new Node(data);
    newNode.next=first;
    first=newNode;
}
public void insertLast(long data){
    Node newNode=new Node(data);
    Node current=first;
    if(current==null){
        first=newNode;
    }else{
        while(current.next!=null){
            current=current.next;
        }
        current.next=newNode;
    }

}
public Node deleteFirst(){
    Node temp=first;
    if(!isEmpty()){
        first=first.next;
    }
    return temp;
}
public void displayList(){
    Node current=first;
    while(current!=null){
        current.displayLink();
        current=current.next;
    }
    System.out.println();
}
public Node find(long key){//找到第一个与key值相同的节点
    Node current=first;
    while(current!=null){
        long data=current.data;
        if(key==data){
            return current;
        }else{
            current=current.next;
        }
    }
    return current;
}
public Node delete(long key){
    Node current=first;//表示当前节点,同时也表示要删除的节点
    Node prior=current;
    while(current!=null){
        long data=current.data;
        if(data==key){
            Node temp=current;
            prior.next=current.next;
            return current;
        }else{
            prior=current;
            current=current.next;
        }

    }
    return current;
}
}

下面是测试代码:

package com.qiao.lb;

public class LinkListTest {
public static void main(String[] args) {
LianBiao biao=new LianBiao();
biao.insertLast(9);
biao.insertLast(8);
biao.insertLast(7);
biao.insertLast(6);
biao.insertLast(5);
biao.insertLast(4);
biao.insertLast(3);
biao.insertLast(2);
biao.displayList();
System.out.println("------------------------------------");
biao.delete(10);
biao.displayList();
System.out.println("------------------------------------");
biao.deleteFirst();
biao.displayList();
System.out.println("------------------------------------");
Node node=biao.find(5);
//  System.out.println(node.data);
System.out.println("------------------------------------");
biao.insertLast(10);
biao.displayList();
}
}

你可能感兴趣的:(数据结构和算法)