鏈表:
是一種物理存儲結構上非連續存儲結構,數據元素的邏輯順序是通過鏈表中的引用鏈接次序實現的 。
實際中鏈表的結構非常多樣,以下情況組合起來就有8種鏈表結構:
單向、雙向
帶頭、不帶頭
循環、非循環
對這六種排列組合即可得到八種鏈表的名稱;
我們今天實現的是 無頭單向鏈表
```java
// 1、無頭單向非循環鏈表實現
public class SingleLinkedList {
//頭插法
public void addFirst(int data);
//尾插法
public void addLast(int data);
//任意位置插入,第一個數據節點爲0號下標
public boolean addIndex(int index,int data);
//查找是否包含關鍵字key是否在單鏈表當中
public boolean contains(int key);
//刪除第一次出現關鍵字爲key的節點
public void remove(int key);
//刪除所有值爲key的節點
public void removeAllKey(int key);
//得到單鏈表的長度
public int size();
public void display();
public void clear(); }
`
```java
```java
class LinkedNode{
public int data;
public LinkedNode next = null;
public LinkedNode(int data) { //構造方法
this.data = data;
}
}
public class LinkedList { //頭插
private LinkedNode head = null;
//創建第一個結點
public void addFirst(int elem) {
LinkedNode node = new LinkedNode(elem);
if (head == null) { //空鏈表
this.head = node;
return;
} else {
node.next = head;
this.head = node;
return;
}
}
//顯示鏈表
public void display() {
LinkedNode prv;
if (this.head == null) {
System.out.println("空鏈表!");
return;
}
System.out.print("[");
for (prv = this.head; prv != null; prv = prv.next) {
if (prv.next != null) {
System.out.print(prv.data + ",");
} else System.out.print(prv.data);
}
System.out.print("]");
}
//尾插
public void addLast(int elem) {
LinkedNode node = new LinkedNode(elem);
if (this.head == null) { //空鏈表
this.head = node;
return;
}
LinkedNode cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
//任意添加結點
public void addIndex(int index, int elem) {
LinkedNode node = new LinkedNode(elem);
int len = size();
if (index < 0 || index > len) {
return;
}
if (index == 0) { //頭插
addFirst(elem);
return;
}
if (index == len) {//尾插
addLast(elem);
return;
}
//中間插入
LinkedNode cur = this.head;
for (int i = 0; i < index - 1; i++) {
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
return;
}
public int size() {
int count = 0;
LinkedNode cur = this.head;
while (cur.next != null) {
cur = cur.next;
count++;
}
return count;
}
//判斷元素是否存在
public boolean contains(int toFind) {
// 直接遍歷鏈表, 依次比較每個元素就行了
if(this.head==null){
return false;
}
LinkedNode cur=this.head;
for(cur=this.head;cur!=null;cur=cur.next){
if(cur.data==toFind){
return true;
}
}
return false;
}
//刪除指定值
public void remove(int toRemove) {
if(this.head==null){ //空鏈表情況
System.out.println("空鏈表!");
return ;
}
//頭刪除
if(toRemove==this.head.data){
this.head=head.next;
return;
}
//別的地方刪除,先找前面結點
LinkedNode prv=getPlace(toRemove);
prv.next=prv.next.next;
}
private LinkedNode getPlace(int toRemove){
LinkedNode cur=this.head;
for(;cur!=null;cur=cur.next){
if(cur.next.data==toRemove){
return cur;
}
}
return null;
}
//刪除所有key的值
public void removeAllKey(int elem){
if(this.head==null){
return;
}
LinkedNode prev=head;
LinkedNode cur=head.next;
while(cur!=null){
if(cur.data==elem){
prev.next=cur.next;
cur=prev.next;
}
else {
prev=cur;
cur=cur.next;
}
}
if(this.head.data==elem){
this.head=this.head.next;
}
}
public void clear(){
this.head=null;
}
}
``