一、什么是單鏈表
單鏈表是鏈表的其中一種基本結構,在結點中資料域用來存盤資料元素,指標域用于指向下一個具有相同結構的結點,
因為只有一個指標結點,稱為單鏈表,
二、單鏈表的結構

三、單鏈表的基本操作
1.創建類和構造方法
class Node{
public int val;
public Node next;
public Node(){
}
public Node(int val){
this.val = val;
}
}
2.建立單鏈表
public class MyLinkedList {
public Node head;
public void createLinkedList(){
this.head = new Node(2);
Node node2 = new Node(3);
Node node3 = new Node(4);
Node node4 = new Node(5);
Node node5 = new Node(6);
head.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = null;
}
}
3.遍歷單鏈表
public void traverse(){
Node cur = head;
while(cur != null){
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
4.找到單鏈表的最后一個結點
public Node findLastNode(){
Node cur = head;
if(cur == null){
return null;
}
while(cur.next != null){
cur = cur.next;
}
return cur;
}
5.找到倒數第二個結點`
public Node findLastTwoNode(){
Node cur = head;
if(cur == null){
return null;
}
if(cur.next == null){
return null;
}
while(cur.next.next != null){
cur = cur.next;
}
return cur;
}
6.求鏈表的長度
public int size(){
Node cur = head;
int count = 0;
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
7.求鏈表的第N個結點
public Node findN(int n){
if(head == null){
System.out.println("單鏈表為空!");
return null;
}
if(n <= 0){
System.out.println("n不合法!");
}
if(n > size()){
System.out.println("n不合法!");
}
int count = 1;
Node cur = head;
while(count != n){
cur = cur.next;
count++;
}
return cur;
}
8.查找關鍵字key是否在單鏈表中
public boolean findKey(int key){
Node cur = head;
while(cur != null){
if(cur.val == key){
return true;
}
cur = cur.next;
}
return false;
}
9.頭插
public void addFirst(int data){
Node node = new Node(data);
node.next = head;
head = node;
}
10.尾插
public void addLast(int data){
Node node = new Node(data);
Node cur = head;
if(cur == null){
head = node;
}
while(cur.next != null){
cur = cur.next;
}
cur.next = node;
}
11.任意位置的插入
public void addIndex(int index,int data){
if(index < 0 || index > size()){
System.out.println("插入位置不合法!");
}
//頭插
if(index == 0){
addFirst(data);
return;
}
//尾插
if(index == size()){
addLast(data);
return;
}
//中間插
Node node = new Node(data);
Node cur = head;
int count = 0;
while(count != index-1){
count++;
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
}
12.找到關鍵字為key的前一結點
public Node searchPre(int key){
Node cur = head;
while(cur.next != null){
if(cur.next.val == key){
return cur;
}
cur = cur.next;
}
return null;
}
13.洗掉關鍵字第一次為key的結點
public void deleteKey(int key){
if(head == null){
return;
}
if(head.val == key){
head = head.next;
return;
}
Node prev = searchPre(key);
if(prev == null){
System.out.println("不存在關鍵字為key的結點");
}else{
prev.next = prev.next.next;
}
}
14.洗掉index位置的結點
public void delete(int index){
if(index < 0 || index >= size()){
System.out.println("洗掉位置不合法!");
}
if(index == 0){
head = head.next;
return;
}
Node cur = head;
int count = 0;
while(count != index-1){
cur = cur.next;
count++;
}
cur.next = cur.next.next;
}
15.清空單鏈表
public void clear(){
head = null;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/243879.html
標籤:其他
上一篇:溫度指示報警電路設計報告
