The disadvantage of sequential storage of linear tables is that large amounts of data need to be moved when inserting and deleting, which is very time consuming. That should be another way of thinking, in the first element, you will know the position of the second element, in the second we find the third position, and so on. This way, whenever the next position is recorded, the location of the next node can be successfully found regardless of where the data exists.
Code implementation:
/*** * Single-linked list node class * @author link * */public class Node {public int inum;public double dnum;public node next; Points to the next node, public node (int i,double d) {iNum = I;dnum = D;} public void Show () {System.out.print ("{" +inum+ "," +dnum+ "}");}}
/*** * Single-Link list class * @author link * */public class linkedlist { private node first; //Declaration Node Object Public linkedlist () {first = null;} /* * Insert Method: Create a Node object, then let the next node of the node point to the first object, and then assign the object to the first object */public void insertfirst (int i,double d) {Node node = new node (i, d); node.next = first;first = node;} Public boolean isempty () {return first == null;} /* * Delete the first node: Let the next one become the first. */public node deletefirst () {node temp = First;first = first.next;return temp;} /* * Delete a specified node: Loop to determine the current node to find the corresponding node, if it is the first node, directly to the next node to become the first knot * Point, if it's not the first node, just let the previous point of the current node point to the next node of the current node. */public node delete (Int i) { Node current = first;//declares that the current node node pervious = first;//declares the previous nodes of the current junction while ( Current.inuM != i) {if (current.next == null) return null;else{pervious = current;current = current.next;}} if (Current == first) First = first.next;else{pervious.next = current.next;} Return current;} /* * looking for a node: loop the current node, if found then roll back the node, if not found, point to the next node to continue looking for */public node find (int i) { Node current = first;while (current.inum != i) {if (current.next == null) { Return null;} Else{current= current.next;}} Return current;} Public void display () {System.out.print ("List (first-->last):"); Node current = first;while (current!=null) {current.show (); current = current.next;} System.out.println ();}}
/*** * Test class * @author link * */public class Linklistapp {public static void main (string[] args) {linkedlist list = new link Edlist (), List.insertfirst (2.3), List.insertfirst (3.5), List.insertfirst ("12.2"), List.insertfirst (41, 5.5); List.insertfirst (2.8), List.insertfirst (2.2), List.insertfirst (6.9), List.display (); List.delete (89); List.display (); Node temp = list.find, if (temp! = null) {System.out.println ("found:{" +temp.inum+ "," +temp.dnum+ "}");} Else{system.out.println ("Can not find!");}}}
Operation Result:
List (First-->last): {67,6.9}{43,2.2}{21,2.8}{41,5.5}{89,12.2}{42,3.5}{12,2.3}
List (First-->last): {67,6.9}{43,2.2}{21,2.8}{41,5.5}{42,3.5}{12,2.3}
found:{41,5.5}
Briefly summarize the advantages and disadvantages of single-linked list structure and sequential storage structure: In the storage allocation mode, sequential storage structure stores the data elements of the linear table in succession, and the single-linked list uses a chain storage structure to store the elements of the linear table with a set of arbitrary storage units. In time performance, when looking for data operations, the sequential storage structure is O (1), the single-linked list is O (n), and when the insert and delete operations, the sequential storage structure requires an average of half the elements of the table to be moved, time is O (n), and the single-linked list is only O (1) when the pointer is In the space performance, the sequential storage structure needs to pre-allocate storage space, divide the big, waste, sub-small is prone to overflow, single-linked list does not need to allocate storage space, as long as there can be allocated, the number of elements is not limited.
Documenting the way I study data structures (eight)