public class ListNodeDemo {
public static void main(String[] args) {
ListLinkedList linkedList = new ListLinkedList();
linkedList.addNode(1);
linkedList.addNode(3);
linkedList.addNode(9);
System.out.println(" Insert the node by head insertion :");
linkedList.printList();
System.out.println("-----------------------");
//linkedList.deleteFirst();
//System.out.println(" Delete first element ");
//linkedList.printList();
Object obj = linkedList.findNode(3);
System.out.printf(" The lookup value is %d The node of :%d\n", 3, obj);
System.out.println(" Delete the node of the specified value :");
linkedList.removeNode(3);
linkedList.printList();
System.out.println(" Tail-insert element :");
linkedList.addNodeTwo(1111);
linkedList.printList();
}
}
class ListLinkedList {
/* Build an empty list , Head node */
ListNode listNode = new ListNode();
ListNode head = listNode.head;
/**
* Judge whether the list is empty
*
* @return
*/
public boolean isEmpty() {
return head == null;
}
/**
* Add node ( The first interpolation )
*
* @param data
*/
public void addNode(Object data) {
ListNode node = new ListNode(data);
node.next = head;
head = node;
}
/**
* Add a node ( Tail add )
* @param data
*/
public void addNodeTwo(Object data) {
ListNode temp = head;
ListNode node = new ListNode(data);
// When the list has no nodes
if (temp.next == null) {
node.next = head;
} else {
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}
}
/**
* Delete node ( Remove... From the head )
*/
public void deleteFirst() {
if (head == null) {
System.out.println(" Linked list is empty. , Cannot delete !");
return;
}
ListNode temp = head;
head = head.next;
}
/**
* Find the node of the specified value , And back to
*
* @param obj
* @return
*/
public Object findNode(Object obj) {
if (head == null) {
try {
throw new Exception(" Linked list is empty. !");
} catch (Exception e) {
e.printStackTrace();
}
}
ListNode cur = head;
while (cur != null) {
if (cur.value == obj) {
return cur.value;
}
cur = cur.next;
}
return null;
}
/**
* Remove the node of the specified value
*
* @param obj
*/
public void removeNode(Object obj) {
if (isEmpty()) {
System.out.println(" Linked list is empty. !");
return;
}
if (head.value == obj) {
head = head.next;
} else {
ListNode pre = head;
ListNode cur = head.next;
while (cur != null) {
if (cur.value == obj) {
pre.next = cur.next;
}
pre = cur;
cur = cur.next;
}
}
}
/**
* Print linked list information
*/
public void printList() {
if (head.next == null) {
System.out.println(" Linked list is empty. !");
return;
}
ListNode temp = head;
while (temp.next != null) {
System.out.print(temp.value + "->");
temp = temp.next;
}
System.out.println(temp.value);
}
}
/**
* Define the data structure of the linked list
*/
class ListNode {
public ListNode next = null;
public Object value;
public ListNode head = null;
public ListNode() {
this.value = null;
this.value = null;
}
public ListNode(Object x) {
this.value = x;
this.next = null;
}
}
Running results :
Insert the node by head insertion :
9->3->1
-----------------------
The lookup value is 3 The node of :3
Delete the node of the specified value :
9->1
Tail-insert element :
9->1->1111