一、链表的基本结构
链表有很多种,比如单链表,双链表等等,链表是最基本的数据结构,其存储的你原理图如下图所示
上面展示的是一个单链表的存储原理图,简单易懂,head为头节点,他不存放任何的数据,只是充当一个指向链表中真正存放数据的第一个节点的作用,
而每个节点中都有一个next引用,指向下一个节点,就这样一节一节往下面记录,直到最后一个节点,其中的next指向null。目前实现单向链表。
二、简单代码实现
public class SimpleLink { public int iData; public double dData; public SimpleLink next; public SimpleLink(int iData, double dData) { this.iData = iData; this.dData = dData; } public void displayLink(){ System.out.print("{" + iData + ", " + dData + "}"); } } class SimpleLinkList{ private SimpleLink first; public SimpleLinkList(){ first = null; } public boolean isEmpty(){ return (first == null); } public void insertFirst(int id, double dd){ SimpleLink newSimpleLink = new SimpleLink(id,dd); newSimpleLink.next = first; first = newSimpleLink; System.out.println("Insert " + id + ", " + dd + " to LinkList "); } public SimpleLink deleteFirst(){ SimpleLink simpleLink = first; first = first.next; return simpleLink; } public void displayList(){ System.out.println("List (first-->last): "); SimpleLink current = first; System.out.println("List struct display:"+JSON.toJSONString(first)); while(current != null){ current.displayLink(); current = current.next; } System.out.println(" "); } }
编写测试类
public class SimpleLinkApp { public static void main(String[] args) { SimpleLinkList theList = new SimpleLinkList(); theList.insertFirst(22, 2.99); theList.displayList(); theList.insertFirst(44, 4.99); theList.displayList(); theList.insertFirst(66, 6.99); theList.displayList(); theList.insertFirst(88, 8.99); theList.displayList(); theList.insertFirst(99, 9.99); theList.displayList(); System.out.println("-----------------------delete-----------------------------------"); while(!theList.isEmpty()){ SimpleLink aLink = theList.deleteFirst(); System.out.println("Deleted!"); aLink.displayLink(); System.out.println(" "); theList.displayList(); } } }
输出结果
Insert 22, 2.99 to LinkList List (first-->last): List struct display:{"dData":2.99,"iData":22} {22, 2.99} Insert 44, 4.99 to LinkList List (first-->last): List struct display:{"dData":4.99,"iData":44,"next":{"dData":2.99,"iData":22}} {44, 4.99}{22, 2.99} Insert 66, 6.99 to LinkList List (first-->last): List struct display:{"dData":6.99,"iData":66,"next":{"dData":4.99,"iData":44,"next":{"dData":2.99,"iData":22}}} {66, 6.99}{44, 4.99}{22, 2.99} Insert 88, 8.99 to LinkList List (first-->last): List struct display:{"dData":8.99,"iData":88,"next":{"dData":6.99,"iData":66,"next":{"dData":4.99,"iData":44,"next":{"dData":2.99,"iData":22}}}} {88, 8.99}{66, 6.99}{44, 4.99}{22, 2.99} Insert 99, 9.99 to LinkList List (first-->last): List struct display:{"dData":9.99,"iData":99,"next":{"dData":8.99,"iData":88,"next":{"dData":6.99,"iData":66,"next":{"dData":4.99,"iData":44,"next":{"dData":2.99,"iData":22}}}}} {99, 9.99}{88, 8.99}{66, 6.99}{44, 4.99}{22, 2.99} -----------------------delete----------------------------------- Deleted! {99, 9.99} List (first-->last): List struct display:{"dData":8.99,"iData":88,"next":{"dData":6.99,"iData":66,"next":{"dData":4.99,"iData":44,"next":{"dData":2.99,"iData":22}}}} {88, 8.99}{66, 6.99}{44, 4.99}{22, 2.99} Deleted! {88, 8.99} List (first-->last): List struct display:{"dData":6.99,"iData":66,"next":{"dData":4.99,"iData":44,"next":{"dData":2.99,"iData":22}}} {66, 6.99}{44, 4.99}{22, 2.99} Deleted! {66, 6.99} List (first-->last): List struct display:{"dData":4.99,"iData":44,"next":{"dData":2.99,"iData":22}} {44, 4.99}{22, 2.99} Deleted! {44, 4.99} List (first-->last): List struct display:{"dData":2.99,"iData":22} {22, 2.99} Deleted! {22, 2.99} List (first-->last): List struct display:null
春风如贵客,一到便繁华