One, the list with the head node and the current node two member variables, in the operation can be more convenient. And they're all one-way linked lists with short knots.
package struct;
Import Java.util.Scanner; * * Note: Here's two member variable permissions can not be private, * because private rights to the public access to the class * * class node{int data;//data domain Node next;//pointer domain local node (int da
TA) {this.data = data;
Public node () {}} public class Link {private node head;//header node private node current;//current node/* Construction Method * *
Public Link () {head = NULL;
current = head;
/* * Method: Add a node to the list * If the header node is empty, the list has not been created, then assign the new node to the head node and point the current node to the head node; * If the header node is not empty, create a new node, place it on the back end of the current node, and move the current index of the list back one bit.
*/public void Add (int data) {if (head = = null) {head = new Node (data);
current = head;
}else{current.next = new Node (data);
current = Current.next;
}/* * method: Traverse the list, that is, print the input list * If the header node is empty, print null; * If the header node is not empty, let the current index of the list move to the end of the node and start over from the starting point./public void print () {
if (head = = null) {SYSTEM.OUT.PRINTLN ("null");
Return
}else{current = head;
while (current!= null) {System.out.println (current.data);
current = Current.next; }}/* method: Get the length of the linked list *Fruit head node is empty, to return 0; * If the header node is not empty, move the current index to the end of the node, and begin the traversal statistic length/public int length () {int length = 0;
current = head;
while (current!= null) {length++;
current = Current.next;
return length;
/* * Method: Determines whether the linked list is empty * Returns True if the header node is empty, otherwise returns false.
*/Public Boolean isempty () {if (head = = null) {return true;
return false; } * * Method: Merge two orderly (small to large) linked list, after merging there is duplication and still orderly, and return the merged linked list * from the two linked list of the first node, the new linked list points to one of the small nodes/public link merge (link L1,lin
K L2) {link L = new link ();
Node n1 = L1.head;
Node n2 = L2.head; if (L1.isempty ()) {return l2;//if L1 is empty, returns L2}else if (L2.isempty ()) {return l1;//if L2 is empty, returns L1}//l1 the current index value is smaller than L2.
Point the new linked header node to the L1 current index if (N1.data < N2.data) {L.head =l.current = n1;
N1 = N1.next;
}ELSE{//L2 the current index value is less than or equal to L1, the new linked header node is pointed to the L2 current index l.head = L.current = n2;
N2 = N2.next; while (N1!=null && n2!=null) {//L1 The current index value is smaller than L2, the current index of the new list is pointed to L1 the current index if (N1.data < N2.data) {L.current.ne
XT = N1; L.current = L.curreNt.next;
N1 = N1.next;
}ELSE{//L2 if the current index value is less than or equal to L1, the current index of the new list is pointed to L2 the current index l.current.next = n2;
L.current = L.current.next;
N2 = N2.next;
}//The elements in L2 are already merged, continuing to merge elements in L1 while (N1!= null) {l.current.next = n1;
L.current = L.current.next;
N1 = N1.next;
}//This time the elements in L1 have been merged, continuing to merge the elements in L2 while (N2!= null) {l.current.next = N2;
L.current = L.current.next;
N2 = N2.next;
return l;
/* * Method: Returns the lead node's reverse link list */public link reverse () {Link link = new link ();
Link.head = new Node ();
Node old_head,new_head,tmp;
New_head = null;
Old_head = Head.next;
while (old_head!=null) {tmp = Old_head.next;
Old_head.next = new_head;//Reverses the direction of the list new_head = Old_head;
Old_head = tmp;
} Link.head.next = new_head;//will return the header node of the linked list to the new linked list node.
public static void Main (string[] args) {Link link = new link ();
Add data to the list Scanner scan = new Scanner (system.in);
System.out.println ("Please enter data ..."); for (int i = 0; i < 6; i++) {Link.add (Scan.nextint ());
Link L2 = new link ();
System.out.println ("The length of the linked list is:" +link.length ());
System.out.println ("There is an empty chain of tables merged:");
Link lin = link.merge (link, L2);
Lin.print ();
Link L1 = new link ();
L1.add (3);
L1.add (5);
L1.add (6);
L1.add (8);
SYSTEM.OUT.PRINTLN ("Two non-empty ordered list merges:");
Lin = Link.merge (LINK,L1);
Lin.print ();
Link ll = Lin.reverse ();
Ll.print (); }
}
the results of the output are:
Please enter data
... 1 2 3 4 5 6
The length of the list is: 6
There is an empty linked list merge: 1 2 3 4 5 6 two
non-empty ordered list merge:
1 2 3 3 4 5 5 6 6 8
linked list reversal:
8 6 6 5 5 4-3 3 2 1
Second, only the first point of the one-way linked list, in order to facilitate operation, each time to create a new node, instead of the linked list of the head node. Here is a list with no short nodes
package struct;
<pre name= "code" class= "java" >class node{
int data;//data domain
node next;//pointer domain public
node (int data) { C41/>this.data = data;
}
Public Node () {
}
}
/* * */public class Linklist {Private node head;//constructor public linklist () {head = null;} Inserts an element into the header of the list public void Insertfirst (int val) {Node NewLink = new Node (); newlink.data = Val;newlink.next = Head;head = NE Wlink;} Inserts an element to the tail of the list, public void Insertlast (int val) {Node NewLink = new Node (), Newlink.data = Val;if (IsEmpty ()) {head = NewLink; return;} Node current = GetCurrent (); current.next = NewLink;} Remove an element from the header node of the list public void Deletefirst () {Node temp = Head.next;head = temp;} Determine if the list is empty public boolean isempty () {return (head = = null);} Print List (elements from scratch node to start printing) public void display () {Node current = Head;if (IsEmpty ()) {System.out.println ("NULL"); While (the current!= null) {System.out.print (Current.data + ""), current = Current.next; System.out.println ();} Returns the current node of the list, public node GetCurrent () {Node present = new node (); Rent.next;} return current;} * * * Link list reversal * 1, the original head node of the next node in the temporary presence of temp; * 2, the original head node point to the new head node; * 3, the new head node jumps to the original head node; * 4, Original headThe node jumps to temp; * 5, and finally jumps the head node of the new list to the new head node. */public linklist Reverse () {linklist list = new linklist (); Node Temp,old_head,new_head;old_head = Head;new_head = null;while (old_head!= null) {temp = Old_head.next;old_head.next = New_head;new_head = Old_head;old_head = temp;} List.head = New_head;return list; Determine if an element exists in the list public boolean isexist (int val) {Node current = Head;while (current!= null) {if (Current.data = = val) {return T Rue;} current = Current.next;} return false;} public static void Main (string[] args) {linklist L1 = new linklist (); L1.insertlast (1); L1.insertlast (2); L1.insertlast (3) ; L1.insertlast (4); L1.insertlast (5); SYSTEM.OUT.PRINTLN ("List 1:"); L1.display (); linklist L2 = new linklist (); L2.insertlast (3); L2.insertlast (5); L2.insertlast (7); System.out.println ("List 2:"); L2.display (); linklist L = merge (L1,L2); System.out.println ("Two linked lists:"); L.display (); l = L.reverse (); SYSTEM.OUT.PRINTLN ("Linked list reversed:"); L.display ();} Merge two ordered singles list public static linklist merge (linklist l1,linklist L2) {linklist link = new linklist (); if (L1. IsEmpty ()) {//If L1 is empty, return L2return L2;} if (L2.isempty ()) {//If L2 is empty, return L1return L1;} Node Link1 = L1.head; Node link2 = L2.head; Node Linkhead = new node (); link.head = Linkhead;while (link1!=null && link2!=null) {if (Link1.data < Link2.data) {Linkhead.next = Link1;link1 = Link1.next;linkhead = Linkhead.next;} Else{linkhead.next = Link2;linkhead = Linkhead.next;link2 = Link2.next;} while (Link1!= null) {Linkhead.next = Link1;linkhead = Linkhead.next;link1 = Link1.next;} while (link2!= null) {Linkhead.next = Link2;linkhead = Linkhead.next;link2 = Link2.next;} Link.head = link.head.next;//The head node to remove return link;}
the results of the output are:
List 1:
1 2 3 4 5 List 2:3 5 + 7
List merged:
two 1 2 3 3 5 5 7
linked list after reversal:
7 5 5 4 3 3 2 1