Second, the list (Linked List)
Linked list node structure is stored in the chain (node) stored in a node comprising a data field (data storage) and a next field (storing a next node pointer), each node linked list need not be continuous, it can be divided to take the lead and do not take the lead node node. Node contains only the next header field.
1, one-way linked list
1.1 inserted in order
-
Create a node class, including data and next, next target point to the next node
class PersonNode { public int number; public String name; public String nickName; public PersonNode next; public PersonNode(int number, String name, String nickName) { this.number = number; this.name = name; this.nickName = nickName; } @Override public String toString() { return "PersonNode{" + "number=" + number + ", name='" + name + '\'' + ", nickName='" + nickName + '\'' + '}'; } }
-
Creating a one-way linked list class, which includes the method of the CRUD
-
Create head Node, the first node in the list node points to the first class next
-
Implementation of CRUD
-
Add a node in the linked list
Determining the position of the node to be inserted into a loop through the node position of the node.
S->next=P->next P->next=S
- To delete a node in the linked list
Determining deleted location, to loop through the locations of the nodes on a node
P->next=p->next->next
-
Traverse the list
PersonNode temp = headNode; while (true) { if (temp.next == null) { break; } temp = temp.next; System.out.println(temp); }
-
Modifying information in a linked list of nodes
You sure you want to modify the position of the nodes, the nodes to traverse to find, modify the information that node.
2, doubly linked list
Doubly linked list pointer is added to the pre-way linked list based on the thus supports bidirectional CRUD.
public int number; public String name; public String nickName; public PersonNode next; public PersonNode pre;
- Doubly linked list traversal
Doubly linked list traversal the same way, can be traversed in two directions.
- Add a doubly linked list
1, added to the end
C is added at the end of the doubly linked list of nodes (target Node B)
B->next=C C->pre=B
2, add between two nodes
D is added between the nodes B and C (the program to locate the node D),
Sequence: first get the predecessor and successor node D, then to get a successor node of the B and C precursors.
D->pre=B D->next=C C->pre=D B->next=D
- Doubly linked list changes
Modify the same with a single list
- Doubly linked list of deleted
1, trailing delete list (target Node P (B))
2, deleted between two nodes (target node S (D))
3, the one-way circular list (without a head node)
Circular list is one way end to end, the entire single-chain to form a ring. A typical application of a one-way circular list when Joseph problems
Joseph Problem Description:
Joseph problem is a very famous interesting question, namely the n people sit in a circle, clockwise by one of their number to begin. * Then the first person Countin, the number of people out to m. Now request is the last person out of the number. * Given two int n and m, the number of representatives of the game. Please return to the last man out of the number.
problem analysis:
Joseph problems can be divided into two simple steps
Boy node class structure
private int number;
private Boy next;
- Way to build a circular list
First and defines two pointer variables cur, first represents the first node, cur point to the current node
构造第一个节点first first = boy;//第一个节点为first first.setNext(first);//仅有一个节点时,first指向first,构成闭环 cur = first;//辅助变量指向first 构造其它节点 cur.setNext(boy);//将新添加的节点设置为当前节点的后继节点 boy.setNext(first);//新添加的节点与first构成闭环 cur=boy;//将当前节点设置为新添加的节点
- Exit circular list according to the rules agreed upon, until only one node
Two auxiliary variables defined first and helper nodes, each node point first round to begin, helper points to the last node, when the first and helper point to the same node, a node of the loop remaining list
构造函数 moveCycle(int no,int number,int sum) 1、确定从第几个节点开始 for(int i=0;i<no-1;i++){ first=first.getNext(); helper=helper.getNext(); } 2、循环,找出节点,直至仅剩一个节点 while (true){ if(first==helper){ System.out.printf("最后一个节点时%d\n",first.getNumber()); break; } //开始,每number下,出圈一次 for(int j=0;j<number-1;j++){ first=first.getNext(); helper=helper.getNext(); } System.out.printf("%d出圈\n",first.getNumber()); first=first.getNext(); helper.setNext(first); }