資料結構與演算法--單鏈表(Single Linked List)
阿新 • • 發佈:2021-02-06
技術標籤:java資料結構與演算法資料結構演算法java單鏈表連結串列
此文章僅作為自己學習過程中的記錄和總結,同時會有意地去用英文來做筆記,一些術語的英譯不太準確,內容如有錯漏也請多指教,謝謝!
一、概述
-單鏈表的基本組成結構:
- Node:自定義的結點結構。
- (Node) head:指向單鏈表頭結點的“頭指標”。
-自定義結點的基本組成結構:
- 資料域:存放具有實際意義的資料。
- “指標”域(next):存放一個指向下一結點的“指標”。
-內容:
- 構造方法建立陣列迴圈佇列。
- add()【向單鏈表中新增結點】
- addByOrder()【向單鏈表中按順序(預設升序)新增結點】
- update()【更新單鏈表中已存在的某個結點的資訊】
- delete()【刪除單鏈表中已存在的的某個結點】
- show()【打印出單鏈表中所有元素的內容】
二、程式碼實現
- Custom Node(自定義結點結構)
/*
HeroNode
Zzay
2021/01/17
*/
package com.zzay.linkedlist.impl;
/**
* Define the node of the linked list.
*
* @author Zzay
* @version 2021/01/17
*/
public class HeroNode {
private int no;
private String name;
private String nickname;
protected HeroNode next;
public HeroNode(int hNo, String hName, String hNickname) {
no = hNo;
name = hName;
nickname = hNickname;
next = null;
}
@Override
public String toString() {
return "HeroNode [" +
"number=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
']';
}
public int getNo() {
return no;
}
public String getName() {
return name;
}
public String getNickname() {
return nickname;
}
public void setName(String name) {
this.name = name;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
- Attributes and constructor
/*
SingleLinkedList
Zzay
2021/01/17
*/
package com.zzay.linkedlist.impl;
/**
* The implementations of a linked list.
*
* @author Zzay
* @version 2021/01/17
*/
public class SingleLinkedList {
// The head node of the linked list. It does not store any practical data.
private HeroNode head = new HeroNode(0, "", "");
/**
* Instantiate a single linked list without giving any nodes.
*/
public SingleLinkedList() {
}
/**
* Instantiate a single linked list with a given head node.
*
* @param head The given head node
*/
public SingleLinkedList(HeroNode head) {
this.head = head;
}
}
- Methods
public void add(HeroNode node) {
HeroNode temp = head;
while (temp.next != null) { // When the list gets to the end.
temp = temp.next;
}
temp.next = node;
}
/**
* Add new node into the list by the ascending order of attribute "no".
*/
public void addByOrder(HeroNode node) {
// Whether the index has already existed.
boolean existed = false;
HeroNode temp = head;
while (true) {
if (temp.next == null) { // When the list gets to the end.
break;
}
if (temp.next.getNo() > node.getNo()) { // When we find the target place.
break;
} else if (temp.next.getNo() == node.getNo()) { // Already occupied by someone.
existed = true;
break;
}
temp = temp.next;
}
if (existed) {
System.out.printf("There's already a hero with the no. of %d, cannot add into it...", node.getNo());
} else {
node.next = temp.next;
temp.next = node;
}
}
/**
* Update a node's information, according to the new node's "no".
*
* @param newNode The new node that will replace the previous node
*/
public void update(HeroNode newNode) {
if (head.next == null) {
System.out.println("The list is empty...");
return;
}
/* Method 1: With the help of assistance variable.
This method replaces the information of the previous node by directly changing them.
這種方法通過輔助變數來實現,原結點的資訊被直接修改。
*/
HeroNode temp = head.next;
boolean findNo = false;
while (true) {
if (temp == null) { // When the list gets to the end.
break;
}
if (temp.getNo() == newNode.getNo()) { // When we find the target node.
findNo = true;
break;
}
temp = temp.next;
}
if (findNo) {
temp.setName(newNode.getName());
temp.setNickname(newNode.getNickname());
} else {
System.out.printf("There's no hero with No %d in the list...\n", newNode.getNo());
}
/* Method 2: By using multiple "next".
This method replaces the information of the previous node by letting the
pre-node of it point to the new node, and the new node points to the next-node.
這種方法通過多次呼叫next域來實現隔代操作,原結點資訊的替換並非被修改,而是先前結點指向了新的結點。
HeroNode temp = head;
while (true) {
if (temp.next.getNo() == newNode.getNo()) {
newNode.next = temp.next.next;
temp.next = newNode;
return;
}
if (temp.next.next == null) {
break;
}
temp = temp.next;
}
System.out.println("There's no hero with such No...");
*/
}
/**
* Delete an existing node from the list according to the given "no".
*
* @param no The given attribute to be compared with so that we can find the target node
*/
public void delete(int no) {
if (head.next == null) {
System.out.println("The list is empty, cannot delete nodes...");
return;
}
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) { // When the list gets to the end.
break;
}
if (temp.next.getNo() == no) { // When we find the target node.
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else {
System.out.printf("There's no hero with No %d in the list...\n", no);
}
}
/**
* Print all the nodes in the linked list.
*/
public void show() {
if (head.next == null) { // If the list is empty.
System.out.println("The list is empty...");
return;
}
HeroNode temp = head.next;
do {
System.out.println(temp);
temp = temp.next;
} while (temp != null);
}
- Test
/*
SingleLinkedListDemo
Zzay
2021/01/17
*/
package com.zzay.linkedlist.demo;
import com.zzay.linkedlist.impl.HeroNode;
import com.zzay.linkedlist.impl.SingleLinkedList;
/**
* CONTENTS:
* (1) Add nodes.
* (2) Add nodes by order (ascending).
* (3) Update a node's information.
* (4) Delete a node from the single linked list.
* (5) Show the contents of the single linked list.
*
* @author Zzay
* @version 2021/01/17
*/
public class SingleLinkedListDemo {
public static void main(String[] args) {
SingleLinkedList singleLinkedList = new SingleLinkedList();
/* Add nodes into the single linked list. */
// singleLinkedList.add(new HeroNode(1, "Jay", "Yellow Chocolate"));
// singleLinkedList.add(new HeroNode(2, "Kobe Bryant", "Black Mamba"));
// singleLinkedList.add(new HeroNode(3, "Dirk Nowitzki", "German Race Car"));
// singleLinkedList.add(new HeroNode(4, "Paul Pierce", "Truth"));
/* Add nodes into the single linked list by order (ascending). */
singleLinkedList.addByOrder(new HeroNode(1, "Jay", "Yellow Chocolate"));
singleLinkedList.addByOrder(new HeroNode(4, "Paul Pierce", "Truth"));
singleLinkedList.addByOrder(new HeroNode(2, "Kobe Bryant", "Black Mamba"));
singleLinkedList.addByOrder(new HeroNode(3, "Dirk Nowitzki", "German Race Car"));
/* Update a node's information. */
singleLinkedList.update(new HeroNode(4, "Vince Carter", "Half man Half amazing"));
// singleLinkedList.update(new HeroNode(5, "James Harden", "The beard"));
/* Delete a node from the single linked list according to the attribute "no". */
// singleLinkedList.delete(2);
// singleLinkedList.delete(5);
/* Show the contents of the single linked list. */
singleLinkedList.show();
}
}