單鏈表增刪改查

2021-10-19 07:53:59 字數 1807 閱讀 3112

單鏈表單鍊錶 [linked list]:由各個記憶體結構通過乙個 next 指標鏈結在一起組成,每乙個內

存結構都存在後繼記憶體結構(鏈尾除外),記憶體結構由資料域和 next 指標域組成。

單鏈表實現圖示:

解析:data 資料 + next 指標,組成乙個單鏈表的記憶體結構 ;

第乙個記憶體結構稱為 鏈頭,最後乙個記憶體結構稱為 鏈尾;

鏈尾的 next 指標設定為 null [指向空];

單鏈表的遍歷方向單一(只能從鏈頭一直遍歷到鏈尾)

public

class

node

public

node

(object data)

}

//頭插法

public

void

addfirst

(object o)

else

}

為了便於理解,畫了張圖

//尾插法

public

void

addtail

(object o)

else

last.next = node2;

}}

// 在任意位置插入節點 在index的後面插入,前提是要插入的那個節點是有值的,否則插入不了。

public

void

addindex

(int index, object o)

int location =0;

node cur = head;

node temp = null;

while

(cur != null)

location++

; cur = cur.next;

}}

public

intgetsize()

public

void

delete

(int index)

//當前位置

int position =0;

//當前節點

node cur = head;

//前置節點

node pre = null;

//知道當前節點出現空時,停止迴圈。

while

(cur != null)

pre = cur;

cur = cur.next;

position++;}

}

public

void

updata

(int index,object o)

int position =0;

node cur = head;

node temp = null;

while

(cur != null)

cur = cur.next;

position++;}

}

public

void

show()

else

}}

單鏈表增刪改查

include include include include using namespace std struct node node int x,node next null 帶參初始化 建立煉表頭結點,新增引用因為要改變指標的位址指向 void createlink node head 新增鍊...

單鏈表 增刪改查

目錄基本面試題 class heronode class singlelinkedlistpublic void add heronode heronode public void addbyorder heronode heronode if temp.next.no heronode.no el...

單鏈表的增刪改查

include include include typedef int data t typedef struct node 定義結點 node typedef struct list 定義表結構 llt 增刪改查函式宣告 intcreate list llt pplist 建立鍊錶 intdest...