List of Links: Linked list header + node
The chain header typically stores only the reference of the next node
?? Node: Save Data + reference to next node
List Header code:
Package?com.xingej.algorithm.datastructure.linkedlist.singlelinkedlist;/**?*? Declare the head of a linked list??? * From the use of the linked list head, Lenovo to Hadoop,spark,netty in the context of the context?*?? *[email protected]?erjun?2017 December 8? Morning 8:45:08?*/public?class? Linklist? {???? Private? Node?first;???? Public? Linklist ()? {???????? When you start a linked list, the pointer reference is empty???????? First?=?null,????}???? Public?void?insertfirst (int?idata,?double?ddata)? {???????? ? Create a new chain node???????? Node?newlink?=?new? Node (idata,?ddata);???????? Update the newly created chain node next reference, which is to assign a header reference to it???????? Newlink.next?=?first;???????? Update the reference to the list header, which is the reference to the newly created chain node, assigned to the header reference???????? First?=?newlink,????}???? Public?boolean?isempty ()? {???????? Return?null?==?first,????}???? Public? Node?deletefirst ()? {???????? ? The first element to be deleted???????? Node?temp?=?first;???????? A linked list, in this data structure, removes the element and only needs to update the pointer reference to???????? Update the reference to the header element???????? First?=?first.next;???????? Return?temp,????}???? Public?void?displaylist ()? {???????? System.out.println ("List?" ( First--->last):???????? Node?current?=?first;???????? While? (null?! =?current)? {???????????? Current.dispLaylink ();???????????? ? Gets the reference to the next element, in C, called the pointer???????????? Current?=?current.next,????????}???????? System.out.println ();????}}
Node Code:
Package?com.xingej.algorithm.datastructure.linkedlist.singlelinkedlist;/**?*? Declare a node? *?? *[email protected]?erjun?2017 December 8? Morning 8:41:50?*/public?class? Node? {???? Private?int?idata;???? Private?double?ddata;???? ? Oh, otherwise the object cannot access the???? Public? Node?next;???? Public? Node (int?idata,?double?ddata)? {???????? This.ddata?=?ddata;???????? This.idata?=?idata,????}???? Public?void?displaylink ()? {???????? System.out.println ("{"? +?idata?+? ",?"?) +?ddata?+ "}");????}}
Simple test Cases:
Package?com.xingej.algorithm.datastructure.linkedlist.singlelinkedlist;import?org.junit.test;/**?*? single-linked list test? *[email protected]?erjun?2017 December 8? Morning 9:00:54?*/public?class? Linklisttest? {[email protected]???? Public?void?test ()? {???????? Linklist?list?=?new? Linklist ();???????? ? It is clear that each time the first element of the linked list is inserted???????? List.insertfirst (32,?8);???????? List.insertfirst (3,?3);???????? List.insertfirst (2,?6);???????? List.insertfirst (42,?8);???????? List.insertfirst (26,?9);???????? List.displaylist ();????}}
Single linked list of Java data structures