An example of Java simulating ordered linked list data structure

Keywords: Java less Mobile

Ordered list:

Sort by key. When the chain head is deleted, the minimum (/ maximum) value is deleted. When inserting, the insertion position is searched.

When inserting, you need to compare O(N), average O(N/2), and delete the minimum (/ maximum) data in the chain head with an efficiency of O(1),

If an application needs frequent access to (insert / find / delete) the smallest (/ largest) data item, then ordered list is a good choice

Priority queues can be implemented by using ordered linked lists

Insertion sort of ordered list:

For an unordered array, use the ordered list to sort. The time level of comparison is O(N^2)

The replication time level is O(2*N), because the number of replications is less, the first time the data is put into the linked list and moved N times, then copied from the linked list to the array, again N times

Every time a new chain node is inserted, it does not need to copy the mobile data, but only needs to change the chain domain of one or two chain nodes

import java.util.Arrays;
import java.util.Random;
 
/**
 * Ordered list insert and sort the array
 * @author stone
 */
public class LinkedListInsertSort<T extends Comparable<T>> {
 
 private Link<T> first; //First node
 public LinkedListInsertSort() {
 
 }
 
 public boolean isEmpty() {
 return first == null;
 }
 
 public void sortList(T[] ary) {
 if (ary == null) {
 return;
 }
 //Insert array elements into the linked list to sort in an ordered linked list
 for (T data : ary) {
 insert(data);
 }
 //
 
 }
 
 public void insert(T data) {//Insert to chain head to sort from small to large
 Link<T> newLink = new Link<T>(data);
 Link<T> current = first, previous = null;
 while (current != null && data.compareTo(current.data) > 0) {
 previous = current;
 current = current.next;
 }
 if (previous == null) {
 first = newLink;
 } else {
 previous.next = newLink;
 }
 newLink.next = current;
 }
 
 public Link<T> deleteFirst() {//Delete chain head
 Link<T> temp = first;
 first = first.next; //Change the first node to the next node
 return temp;
 }
 
 public Link<T> find(T t) {
 Link<T> find = first;
 while (find != null) {
 if (!find.data.equals(t)) {
 find = find.next;
 } else {
 break;
 }
 }
 return find;
 }
 
 public Link<T> delete(T t) {
 if (isEmpty()) {
 return null;
 } else {
 if (first.data.equals(t)) {
 Link<T> temp = first;
 first = first.next; //Change the first node to the next node
 return temp;
 }
 }
 Link<T> p = first;
 Link<T> q = first;
 while (!p.data.equals(t)) {
 if (p.next == null) {//It means we haven't found the end of the chain
 return null;
 } else {
 q = p;
 p = p.next;
 }
 }
 
 q.next = p.next;
 return p;
 }
 
 public void displayList() {//ergodic
 System.out.println("List (first-->last):");
 Link<T> current = first;
 while (current != null) {
 current.displayLink();
 current = current.next;
 }
 }
 
 public void displayListReverse() {//Reverse order traversal
 Link<T> p = first, q = first.next, t;
 while (q != null) {//Pointer reverse, traversal data order backward
 t = q.next; //no3
 if (p == first) {//When it is the original header, the. next of the header should be empty
 p.next = null;
 }
 q.next = p;// no3 -> no1 pointer reverse
 p = q; //start is reverse
 q = t; //no3 start
 }
 //In the if in the above loop, set first.next to null. When q is null and no loop is executed, p is the original most and one data item. After inversion, p is assigned to first
 first = p; 
 displayList();
 }
 
 class Link<T> {//Chain node
 T data; //Data domain
 Link<T> next; //Subsequent pointer, node chain domain
 Link(T data) {
 this.data = data;
 }
 void displayLink() {
 System.out.println("the data is " + data.toString());
 }
 }
 
 public static void main(String[] args) {
 LinkedListInsertSort<Integer> list = new LinkedListInsertSort<Integer>();
 Random random = new Random();
 int len = 5;
 Integer[] ary = new Integer[len];
 for (int i = 0; i < len; i++) {
 ary[i] = random.nextInt(1000);
 }
 System.out.println("----Before sorting----");
 System.out.println(Arrays.toString(ary));
 System.out.println("----After list sorting----");
 list.sortList(ary);
 list.displayList();
 }

Printing

----Before sorting----
[595, 725, 310, 702, 444]
----After list sorting----
List (first-->last):
the data is 310
the data is 444
the data is 595
the data is 702
the data is 725

Single chain table reverse order:

public class SingleLinkedListReverse {
 
 public static void main(String[] args) {
 Node head = new Node(0);
 Node temp = null;
 Node cur = null;
 
 for (int i = 1; i <= 10; i++) {
 temp = new Node(i);
 if (i == 1) {
 head.setNext(temp);
 } else {
 cur.setNext(temp);
 }
 cur = temp;
 }//10.next = null;
 
 Node h = head;
 while (h != null) {
 System.out.print(h.getData() + "	");
 h = h.getNext();
 }
 System.out.println();
 
 //Reverse 1
// h = Node.reverse1(head);
// while (h != null) {
// System.out.print(h.getData() + "	");
// h = h.getNext();
// }
 
 //Reverse 2
 h = Node.reverse1(head);
 while (h != null) {
 System.out.print(h.getData() + "	");
 h = h.getNext();
 }
 
 
 }
}
 
/*
 * Each node in a single chain table has attributes that point to the next node
 */
class Node {
 Object data;//Data object
 Node next; //Next node
 
 Node(Object d) {
 this.data = d;
 }
 Node(Object d, Node n) {
 this.data = d;
 this.next = n;
 }
 public Object getData() {
 return data;
 }
 public void setData(Object data) {
 this.data = data;
 }
 public Node getNext() {
 return next;
 }
 public void setNext(Node next) {
 this.next = next;
 }
 //Method 1 head is reset
 static Node reverse1(Node head) {
 
 Node p = null; //New head after reversal
 Node q = head;
 //Rotation result: 01212334,..... 10 null
 while (head.next != null) {
 p = head.next; //Replace the first with the second, when p represents the next in the original sequence header
 head.next = p.next; //Replace the second with the third
 p.next = q; //The next one that has already reached the first position will become the first one
 q = p; //New first to become current first
 }
 return p;
 
 }
 //Method 2 head is not reset
 static Node reverse2(Node head) {
 //After pointing the pointer of the intermediate node to the previous node, you can still traverse the linked list backward
 Node p1 = head, p2 = head.next, p3; //Before, during and after
 //Rotation result: 012,; 123,; 234,; 345,; 456..... 9 ﹣ 10 ﹣ null
 while (p2 != null) {
 p3 = p2.next; 
 p2.next = p1; //Point back change point forward
 p1 = p2; //2, 3 move forward
 p2 = p3;
 }
 head.next = null;//The head does not change. When the output is 0, ask 0.next to be 1
 return p1;
 }
}


Posted by jeffshead on Thu, 14 Nov 2019 06:51:59 -0800