Single linked list
Little prince single linked list
One day, the little prince was fascinated by the game of queuing. There were 10 toys numbered 1-10 placed in order on the table. Now the little prince wants to place them according to his preferences. The little prince selects a beautiful toy from among them and puts it in the front of all the toys. It is known that he has selected a total of M times. Each time, he selects the toy with label X and puts it in the front to find the toy label after placement.
Given a set of inputs, M=8, a total of 8 times are arranged, and the sequence of these 8 times is 9, 3, 2, 5, 6, 8, 9, 8. Find the number sequence of the final toy.
Process:
1. Create initial linked list
2. Delete linked list
3. Realize the insertion operation of linked list
4. Output the ordered linked list
import java.util.Scanner; public class Single linked list { static class Node{ int data; Node next; Node(int data){ this.data = data; } }//Member class, table node static Node head = new Node(1);//Head node //Initialize linked list static void init() { Node x = head; for(int i = 1; i <= 10; i++) { x = (x.next = new Node(i)); // x.next = new Node(i); // x = x.next; x.next = null; } } //Delete node static void del(int x) { Node Befor = head;//Store a node precursor to facilitate the deletion of elements for(Node T = head.next; T != null; T = T.next) { if(T.data == x) {//Number of found to delete Node temp = T;//Temporary node save node Befor.next = T.next;//Delete node from linked list return;//End function after deletion } Befor = T; } } //Insert element (header) static void insert(int x) { Node temp = new Node(x); temp.next = head.next; head.next = temp; } //Display linked list static void show(int x) { //System.out.println("this is the" + x + "operation"); for(Node T = head.next; T != null; T = T.next) { System.out.print(T.data + " "); } System.out.println(); } public static void main(String[] args) { // TODO Auto-generated method stub int N; Scanner in = new Scanner(System.in); init(); //show(0); N = in.nextInt(); for(int i = 1; i <= N; i++) { int x = in.nextInt(); del(x); insert(x); show(i); } } }
Circular linked list
Joseph Ring problem
There are n people sitting around the round table. Now start counting from the person at a certain position k(1 ≤ k ≤ n), and the person who counts to m will stand out. The next person, that is, the person in the original position m+1, starts counting from 1 and then counts to M. Repeat until everyone stands up. Try to design a program to find out the sequence of these n people.
- Requirement 1: use circular linked list to solve the problem
- Circular list method: simulation method can be used
- Requirement 3: the definition and usage of circular linked list class can not be used
Analysis: it is almost the same as the previous one, except that the end node points to the head node to form a circular linked list.
That is, x.next = head.
import java.util.Scanner; public class Circular linked list { static class Node{ int val; Node next; Node(int val){ this.val = val; } } public static void main(String[] args) { int N, M, K; //n people count off from the k-th position and get out of the line when they count to m Scanner in = new Scanner(System.in); N = in.nextInt(); K = in.nextInt(); M = in.nextInt(); Node head = new Node(1);//The head nodes are listed separately to facilitate the formation of a circular linked list Node x = head; //Initialize circular linked list for(int i = 2; i <= N; i++) { x = (x.next = new Node(i)); x.next = head;//Form a circular linked list }//At this point, x points to the end node //Find the starting point of counting off for(int i = 1; i < K; i++) { x = x.next;//At this point, x points to the k-1 node } //Count off while(x != x.next) {//Stop when there is only one node left for(int i = 1; i < M; i++) { x = x.next; }//At this point, x points to the previous node to delete System.out.println(x.next.val + " "); x.next = x.next.next;//Delete node } //Output the last node value System.out.println(x.val); } }
Bidirectional linked list
Little prince double linked list
The same as the single linked list, it is realized with a two-way linked list.
The point is that the node has two pointer fields: next and before. Next points to the next node of the node, and before points to the previous node of the node. X.next Befor = x, which forms a two-way. In the little prince problem, when doing so, when deleting a node, you don't have to save the precursor node in a new node, and you can find it directly.
import java.util.Scanner; public class Bidirectional linked list { static class Node{ int data; Node next; Node befor; Node(int data){ this.data = data; } } static Node head = new Node(1);//Head node //Initialize linked list static void init() { Node x = head; for(int i = 1; i <= 10; i++) { x.next = new Node(i);//Establish a two-way linked list x.next.befor = x; x = x.next; } x.next = null; } //Delete node static void del(int x) { for(Node T = head.next; T != null; T = T.next) { if(T.data == x) {//Found deleted node T.befor.next = T.next;//Remove node x from the linked list T.next.befor = T.befor;//Although deleted, the left and right pointers of T also point to the previously pointed node return;//After deleting the node, end the function } } } //Insert node (head insert) static void insert(int x) { Node temp = new Node(x); temp.next = head.next; temp.next.befor = temp; head.next = temp; temp.befor = head;//The next before of the header node also points to the header node. In this question, you can write or not } //Display linked list static void show(int x) { //System.out.println("this is the" + x + "operation"); for(Node T = head.next; T != null; T = T.next) { System.out.print(T.data + " "); } System.out.println(); } public static void main(String[] args) { // TODO Auto-generated method stub int N; Scanner in = new Scanner(System.in); init(); //show(0); N = in.nextInt(); for(int i = 1; i <= N; i++) { int x = in.nextInt(); del(x); insert(x); show(x); } } }