In this paper, according to the "Big talk Data Structure" a book, the implementation of the Java version of the single-linked list .
The linear table abstract data type definition in the book is as follows (page 45th):
Implementation program:
Package linklist;/** * Description: * 1. There is no linear table length in the big talk data structure, but it refers to the data field that can be stored in the head node. * The linear table length of this program is stored in the count variable, the linear table length can make the program more convenient. * 2. In the program, the first position represents the I node, and the head node belongs to the No. 0 node * 3. Because the list is generic, the whole table is created using integers (random integers do elements), so there are some types of conversion * 4.Java programs usually start with lowercase, but in accordance with the book, the method in the program takes the beginning of capital. * * NOTE: * 1.count to add or subtract elements when adding one or minus a must not forget * 2. Empty the linear table to each element is null * * @author Yongh * */public class Linklist<e> {private No De<e> Head; Header node private int count; Linear table Length ("Liar" stored in head.data)/** * Node */private class node<e>{e data; Node<e> next;public Node (E data,node<e> next) {This.data=data;this.next=next;}} /** * Initialization of the linear table */public linklist () {head=new node<e> (null, NULL); Not head=null;count=0;} /** * Determine if the linear table is empty */public boolean IsEmpty () {if (count==0) {System.out.println ("Table is empty! "); return true;} else {System.out.println ("The table is not empty! "); return false;} return count==0;} /** * Empty linear table (self-written) */public void Clearlist () {node<e> node;while (count!=0) {node=head.next;head.next=node.next; node=null;count--;} System.out.println ("The linear table has been emptied!" ");} /** * Empty linear table (rewrite in book) */public void ClearList2 () {node<e> q,p;q=head.next;while (q!=null) {p=q.next;q=null;q=p;count--;} Head.next=null; System.out.println ("The linear table has been emptied!" ");} /** * Get the first node (including the No. 0 node, head node) * Get the node value only needs GetNode (i). data, no longer write methods */public node<e> getnode (int i) {if (i<0| | I>count) {throw new RuntimeException ("Element location error! ");} else if (i==0) {return head; }else {node<e> node=head.next;for (int k=1;k<i;k++) {node=node.next;} return node;}} /** * Get Data for node I (including head node) */public E GetData (int i) {return getnode (i). data;} /** * Find element, 0 for find failed */public int Locateelem (e e) {node<e> node;node=head.next;if (node.data==e) return 1;for (int k=1; k<count;k++) {node=node.next;if (node.data==e) return k+1;} System.out.println ("Find failed! "); return 0;} /** * The first place inserts a new element */public void Listinsert (int i,e E) {if (i<1| | i>count+1) {throw new RuntimeException ("wrong insertion position! ");} else {node<e> newnode=new node<e> (e,null); Newnode.next=getnode (i-1). Next; Because the GetNode () method contains a node, you do not have to Judge GetNode (I-1) alone. next=newnode;count++;System.out.println ("Insert succeeded! ");}} /** * Removes the I-position element and returns its value */public E listdelete (int i) {if (i<1| | I>count) throw new RuntimeException ("Delete location Error! "); Node<e> Node=getnode (i); E E=node.data; GetNode (i-1). next=node.next;node=null;count--; System.out.println ("Delete succeeded!"); return e;} /** * Gets the linear table length */public int listlength () {return count;} /** * Whole table creation, head interpolation method */public linklist<integer> createlisthead (int n) {linklist<integer> list1=new LinkList< Integer> (); node<integer> node,lastnode;for (int i=0;i<n;i++) {int data= (int) (Math.random () *100); Generates a random number within 100 node=new node<integer> (data, NULL); Node.next= (LINKLIST<E>. node<integer>) List1.head.next; List1.head.next= (LINKLIST<INTEGER>. node<integer>) node; list1.count++; }return List1;} /** * Full table creation, tail interpolation */public linklist<integer> createlisttail (int n) {linklist<integer> list2=new LinkList< Integer> (); Node<integer> node,lastnode;lastnode= (LINKLIST<E>. Node<integer>) list2.head;for (int i=0;i<n;i++) {int data= (int) (Math.random () *100); Generates a random number within 100 node=new node<integer> (data, null); lastnode.next=node;lastnode=node;list2.count++; }return List2; }}
Test code:
The basic data types and reference types each write a test code.
Package linklist;/** * Basic data Type Test */public class LinkListTest1 {public static void main (string[] args) {linklist<integer& Gt Nums = new linklist<integer> (); nums. IsEmpty (); System.out.println ("—————————— insert 1 to 5 and read the contents ——————————"); for (int i = 1; I <= 5; i++) nums. Listinsert (i, 2*i); nums. IsEmpty (); int num;for (int i = 1; I <= 5; i++) {num = Nums. GetData (i); System.out.println (the value of "the" + i + "position is:" + num);} System.out.println ("—————————— find whether 0, 2, 10 are —————————— in the table"); System.out.print ("0 Position:"); System.out.println (nums. Locateelem (0)); System.out.print ("2 position:"); System.out.println (nums. Locateelem (2)); System.out.print ("10 Position:"); System.out.println (nums. Locateelem (10)); System.out.println ("—————————— Delete 2, ——————————"); num = Nums. Listdelete (1); System.out.println ("deleted:" + num); num = Nums. Listdelete (4); System.out.println ("deleted:" + num); SYSTEM.OUT.PRINTLN ("Current table length:" + nums. Listlength ()); for (int i = 1; I <= nums. Listlength (); i++) {num = Nums. GetData (i); System.out.println (the value of "the" + i + "position is:" + num);} Nums. Clearlist (); nums. IsEmpty ();}}
the table is empty! —————————— Insert 1 to 5 and read the contents —————————— Insert successfully! Insert Success! Insert Success! Insert Success! Insert Success! The table is not empty! The value of the 1th position is:2 The value of the 2nd positionis: 4 The value of the 3rdposition is: 6 The value of the 4th positionis: 8 The value of the 5th position is:ten—————————— find 0, 2, 10 whether in the table —————————— 0 Location: Find failed! 02 Location:110 Location:5—————————— Delete 2,—————————— Delete succeeded ! deleted:2 Delete succeeded ! deleted:3 The value of the 1th positionis: 4 The value of the 2ndposition is: 6 The value of the 3rdposition is: 8 linear table has been emptied! The table is empty!
LinkListTest1
Package Linklist;public class LinkListTest2 {public static void main (string[] args) {linklist<student> s Tudents =new linklist<student> (); Students. IsEmpty (); System.out.println ("—————————— insert 1 to 5 and read Content ——————————"); Student[] stus= {new Student ("Small A", one-to-one), new Student ("Small B", "New" ("Small C"), New Student ("small d", +), new S Tudent ("Small e", 151)}; for (int i=1;i<=5;i++) students. Listinsert (i, stus[i-1]); Students. IsEmpty (); Student Stu; for (int i=1;i<=5;i++) {stu=students. GetData (i); System.out.println ("+i+" Position: "+stu.name"); } System.out.println ("—————————— Find out if small a, small e, Dragons are —————————— in the table"); System.out.print ("Small A's position:"); Stu=stus[0]; SYSTEM.OUT.PRINTLN (students. Locateelem (Stu)); System.out.print ("Small E's position:"); STU=STUS[4]; SYSTEM.OUT.PRINTLN (students. Locateelem (Stu)); System.out.print ("The location of the Dragons:"); Stu=new Student ("Drake", 11); SYSTEM.OUT.PRINTLN (students. Locateelem (Stu)); System.out.println ("—————————— Delete small E, small b ——————————"); Stu=students. Listdelete (2); System.out.println ("deleted:" +stu.name); Stu=students. Listdelete (4); System.out.println ("deleted:" +stu.name); SYSTEM.OUT.PRINTLN ("Current table length:" +students. Listlength ()); for (int i=1;i<=students. Listlength (); i++) {stu=students. GetData (i); System.out.println ("+i+" Position: "+stu.name"); } students. Clearlist (); Students. IsEmpty (); }} class Student{public Student (String name, Int. age) {This.name=name; This.age=age; } String name; int age;}
the table is empty! —————————— Insert 1 to 5 and read the contents —————————— Insert successfully! Insert Success! Insert Success! Insert Success! Insert Success! The table is not empty! The 1th position is: Small a 2nd position: Small B 3rd position: Small C 4th position is: Small d 5th position: Small e —————————— Find small A, small e, Little dragons in the table —————————— small a position:1 small e position: 5 Dragon's Location: Find failed! 0—————————— Delete small E, small b —————————— Delete success ! deleted: Small b delete succeeded ! deleted: small e current table length:3 The 1th position is: Small a 2nd position: small C 3rd position: Small D linear table has been emptied! The table is empty!
LinkListTest2
"Java" Big Talk Data Structure (2) a single linked list of linear tables