本教程的内容基本来自于《Java数据结构与算法》
单向链表是最基本的数据结构之一,链表中的基本单位是节点,节点包含数据和其他节点的指针。不同于数组的是,链表中的数据存储地址在物理上不是连续的,而是使用指针来指向下一节点。
每个节点有数据区(data)和指向下一节点的指针(next),当next指针指向null时,表示链表的尾节点。
单个节点定义
class Link{
public int iData; //节点保存的整型数据
public double dData; //节点保存的双精度浮点型数据
public Link next; //节点所指向的下一个节点
//构造函数
public Link(int id, double dd)
{
this.iData = id;
this.dData = dd;
}
//打印节点数据,以便查看
public void displayLink()
{
System.out.print("{"+iData+","+dData+"} ");
}
}
链表的定义
1.首先定义第一个节点 first,开始没有元素,所有将first指向null
class LinkList{
private Link first; //定义first节点
public LinkList()
{
this.first = null; //first不指向任何节点,表示空链表
}
2.判断链表是否为空
public boolean isEmpty()
{
return (this.first==null);
}
3.从头部插入新节点
插入前
插入后
从图中看到分为两步
- 先将新节点的next指向first指向的头结点
- 再将first指向新节点即可
public void insertFirst(int id, double dd)
{
Link newLink = new Link(id, dd); //创建节点对象
newLink.next = this.first; //newLink.next指向原来first指向的位置
this.first = newLink; //first指向newLink
}
4.从头部删除节点
删除前
删除后
删除节点比较简单,将first指向头结点中next指向的下一节点即可
public Link deleteFirst()
{
if(isEmpty())
{
System.out.println("LinkList is empty, there is no Link to delet!!");
return null;
}
else
{
Link temp = this.first; //暂存头结点
this.first = this.first.next; //first指向头结点的下一个节点
return temp;
}
}
5.查找节点
节点一个一个的遍历即可
public Link find(int key)
{
Link current = this.first;
while(current!=null)
{
if(current.iData==key)
{
return current;
}
else
current = current.next;
}
return null;
}
6.删除指定节点
相当于第4点与第5点的结合,先找到该节点,然后将节点删除
在遍历节点的时候,记住当前节点与前一个节点,找到后只需将前一节点中next指向当前节点中next指向的下一个节点即可
public Link delete(int key)
{
Link current = this.first;
Link previous = this.first;
while(current!=null)
{
if(current.iData==key)
{
if (current==first)
first = first.next;
else
previous.next = current.next;
return current;
}
else
{
previous = current;
current = current.next;
}
}
return null;
}
7.打印链表
public void dispalyList()
{
System.out.print("List (First-->Last): ");
Link current = this.first; //从头节点开始
while (current != null) //判断节点是否为空
{
current.displayLink(); //打印节点
current = current.next; //移动至下一节点
}
System.out.println("");
}
最后测试一下
public class LinkListApp {
public static void main(String[] args)
{
LinkList theList = new LinkList(); //新建链表
//插入节点
theList.insertFirst(22, 2.49);
theList.insertFirst(44, 4.49);
theList.insertFirst(66, 6.49);
theList.insertFirst(88, 8.49);
//打印链表
theList.dispalyList();
//查找节点
Link fLink = theList.find(2);
if (fLink!=null)
{
System.out.print("find the key ");
fLink.displayLink();
System.out.println("");
}
else
System.out.println("key is not exist");
//删除节点
Link dLink = theList.delete(88);
if (dLink!=null)
{
System.out.print("delete the key ");
dLink.displayLink();
System.out.println("");
}
else
System.out.println("delete failed, key is not exist!!");
System.out.print("after delete: ");
theList.dispalyList();
//从头部删除节点
while(!theList.isEmpty())
{
Link aLink = theList.deleteFirst();
System.out.print("Deleted ");
aLink.displayLink();
System.out.println("");
}
theList.dispalyList();
}
}
github完整代码
https://github.com/gamersover/data_structure_java/blob/master/LinkList/LinkListApp.java