php基礎-13
阿新 • • 發佈:2020-07-26
新增尾結點
LNode LListAddEnd(LNode head, Data data) { LNode node = new LNode(), temp; node.data = data; //賦給結點資料 node.nextNode = null; //末尾元素,指向null if (head == null) { head = node; return head; } temp = head; while (temp.nextNode != null) { //查詢連結串列的末尾 temp = temp.nextNode; } temp.nextNode = node; //插入資料放在末尾 return head; }
新增首節點
LNode LListAddFirst(LNode head, Data data) {
LNode node = new LNode();
node.data = data;
if (head == null) {
head = node;
return head;
}
node.nextNode = head;
head = node; //新結點作為head
return head;
}
查詢結點
LNode LListFind(LNode head, int position) { if (position < 0) return null; LNode temp = head; int i = 0; while (temp != null) { temp = temp.nextNode; i++; if (i == position) return temp; } return null; }
插入結點
LNode LListInsert(LNode head, int position, Data data) { LNode node = new LNode(), temp = new LNode(); node.data = data; //position==0 if (position == 0) { node.nextNode = head; //新結點指向舊head head = node; //新結點作為head return head; } //position!=0 LNode preNode = LListFind(head, position - 1); //查詢插入位置的前一個結點 if (preNode != null) { node.nextNode = preNode.nextNode; //將前一個結點的指標給這個新的結點的指標 preNode.nextNode = node; // 將前一個結點指向新結點 return head; } else { System.out.println("插入位置非法"); return null; } }
刪除結點
LNode LListDelete(LNode head, int position) {
if (position == 0) {
return head.nextNode;
}
LNode preNode = LListFind(head, position - 1); //查找出前一個結點
preNode.nextNode = preNode.nextNode.nextNode; //前一個結點指向其下下個結點
return head;
}
計算連結串列長度
int LListLength(LNode head) {
LNode temp = head;
int i = 0;
while (temp != null) {
i++;
temp = temp.nextNode;
}
return i;
}
顯示所有結點
void LListShow(LNode head) {
LNode temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.nextNode;
}
}
整例
package LinkedList;
import java.util.Objects;
import java.util.Scanner;
/**
* Description
* Author cloudr
* Date 2020/7/25 18:56
* Version 1.0
**/
class Data {
String id;
String name;
int age;
public Data(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public Data() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Data data = (Data) o;
return age == data.age &&
Objects.equals(id, data.id) &&
Objects.equals(name, data.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age);
}
@Override
public String toString() {
return "Data{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
class LNode {
Data data;
LNode nextNode; //指向下一個結點
//新增尾結點
LNode LListAddEnd(LNode head, Data data) {
LNode node = new LNode(), temp;
node.data = data; //賦給結點資料
node.nextNode = null; //末尾元素,指向null
if (head == null) {
head = node;
return head;
}
temp = head;
while (temp.nextNode != null) { //查詢連結串列的末尾
temp = temp.nextNode;
}
temp.nextNode = node; //插入資料放在末尾
return head;
}
//新增首節點
LNode LListAddFirst(LNode head, Data data) {
LNode node = new LNode();
node.data = data;
if (head == null) {
head = node;
return head;
}
node.nextNode = head;
head = node; //新結點作為head
return head;
}
//查詢結點
LNode LListFind(LNode head, int position) {
if (position < 0)
return null;
LNode temp = head;
int i = 0;
while (temp != null) {
temp = temp.nextNode;
i++;
if (i == position)
return temp;
}
return null;
}
//插入結點
LNode LListInsert(LNode head, int position, Data data) {
LNode node = new LNode(), temp = new LNode();
node.data = data;
//position==0
if (position == 0) {
node.nextNode = head; //新結點指向舊head
head = node; //新結點作為head
return head;
}
//position!=0
LNode preNode = LListFind(head, position - 1); //查詢插入位置的前一個結點
if (preNode != null) {
node.nextNode = preNode.nextNode; //將前一個結點的指標給這個新的結點的指標
preNode.nextNode = node; // 將前一個結點指向新結點
return head;
} else {
System.out.println("插入位置非法");
return null;
}
}
//刪除結點
LNode LListDelete(LNode head, int position) {
if (position == 0) {
return head.nextNode;
}
LNode preNode = LListFind(head, position - 1); //查找出前一個結點
preNode.nextNode = preNode.nextNode.nextNode; //前一個結點指向其下下個結點
return head;
}
//計算連結串列長度
int LListLength(LNode head) {
LNode temp = head;
int i = 0;
while (temp != null) {
i++;
temp = temp.nextNode;
}
return i;
}
//顯示所有結點
void LListShow(LNode head) {
LNode temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.nextNode;
}
}
}
public class LinkedList {
public static void main(String[] args) {
System.out.println("連結串列測試 輸入結點資訊:(id name age) age為0截止輸入");
Scanner input = new Scanner(System.in);
LNode head = null, node = null;
LNode list = new LNode();
do {
Data data = new Data(input.next(), input.next(), input.nextInt());
if (data.age == 0)
break;
head = list.LListAddEnd(head, data);
} while (true);
System.out.println("--初始資料--");
list.LListShow(head);
head = list.LListAddFirst(head, new Data("6", "6", 6));
System.out.println(head.data);
System.out.println("--新增首節點--");
list.LListShow(head);
System.out.println("--查詢資料--");
LNode find = list.LListFind(head, 2);
System.out.println(find.data);
System.out.println("--插入結點--");
list.LListShow(head);
System.out.println("--連結串列長度--");
System.out.println(list.LListLength(head));
System.out.println("--刪除結點--");
list.LListShow(list.LListDelete(head, 3));
}
}