單鏈表 增刪改查

2022-06-17 02:58:49 字數 3737 閱讀 2781

目錄基本面試題

class heronode 

}

class singlelinkedlist
public void add(heronode heronode)
public void addbyorder(heronode heronode) 

if (temp.next.no > heronode.no) else if (temp.next.no == heronode.no)

temp = temp.next;//後移

}//判斷flag的值

if (flag) else

}

注意

如果鍊錶中已經存在要插入的結點,則不能插入,並給出提示
public void update(heronode newheronode) 

//找到需要修改的結點,根據no編號

//定義遍歷指標

heronode temp = head;

while (temp != null && temp.no != newheronode.no)

if (temp == null) else

}

思路:找到待刪除結點的前乙個結點,temp指向要比較的結點的前乙個位置,所以比較時是,temp.next.no == delno

public void del(int no) 

//定義指標

heronode temp = head;

while (temp.next != null && temp.next.no != no)

if (temp.next == null) else

}

public void list() 

heronode temp = head.next;

while (temp != null)

}

public static int getlength(heronode head) 

int length = 0;

//定義乙個遍歷指標

heronode cur = head.next;

while (cur != null)

return length;

}

思路:

1. 先獲取鍊錶長度length

2. 得到length後,指標只需從第乙個結點向後移動 length-index 次

public static heronode getlastindexof(heronode head, int index) 

int length = getlength(head);

if (index <= 0 || index > length)

heronode cur = head;

for (int i = 0; i <= length - index; i++)

return cur;

}

遍曆法

思路:

1. 建立乙個新的頭節點

2. 遍歷原有鍊錶,將遍歷到的每個結點插入到新建立的結點的後面

public static void reverselist(heronode head) 

heronode reversehead = new heronode(0, "", "");

heronode cur = head.next;

//臨時儲存要插入反轉鍊錶的結點

heronode curnext;

//遍歷原來的鍊錶,每遍歷乙個結點,就將這個結點插入到新鍊錶的頭部

while (cur != null)

head.next = reversehead.next;

}

利用stack實現

思路:

1. 遍歷結點,並壓入棧中

2. 迴圈取出棧頂結點,並將其next指向新的棧頂結點

public static void reverse(heronode head) 

stackstack = new stack<>();

heronode cur = head.next;

//遍歷結點並壓入棧中

while (cur != null)

//先取出乙個結點,使next指向棧頂的結點

head.next = stack.pop();

head.next.next = stack.peek();

while (stack.size() > 0)

node.next = stack.peek();

}}

思路:

1. 同樣是借助棧,遍歷結點壓入棧

2. 取出棧頂結點並列印

public static void reverseprint(heronode head) 

stackstack = new stack<>();

heronode cur = head.next;

//將鍊錶所有結點壓入棧中,需要注意的是,jdk1.8,棧的最大深度為12000

while (cur != null)

//迴圈出棧,列印出棧的結點

while (stack.size() > 0)

}

思路:

建立乙個新的頭結點

從頭到尾遍歷兩個鍊錶,比較兩煉表中指標指向的結點大小,小的先插入新的鍊錶中,然後移動指標

取下乙個結點和另外乙個鍊錶中指標指向的結點比較

一旦有乙個鍊錶遍歷完成,則判斷哪個鍊錶不為空,就將新鍊錶的尾部指向不為空煉表中指標指向的下乙個結點

注意

​ 如果有乙個鍊錶為空,就將新煉表頭結點指向這個鍊錶的第乙個結點

​ 如果兩個鍊錶都為空,就直接指向null返回

public static heronode concatlist(heronode head1, heronode head2) 

heronode tail = newhead;

if (head1.next == null && head2.next != null)

if (head1.next != null && head2.next == null)

heronode cur1 = head1.next;

heronode cur2 = head2.next;

while (cur1 != null && cur2 != null) else

tail = tail.next;

}if (cur1 == null) else

return newhead;

}

單鏈表增刪改查

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

單鏈表增刪改查

單鏈表單鍊錶 linked list 由各個記憶體結構通過乙個 next 指標鏈結在一起組成,每乙個內 存結構都存在後繼記憶體結構 鏈尾除外 記憶體結構由資料域和 next 指標域組成。單鏈表實現圖示 解析 data 資料 next 指標,組成乙個單鏈表的記憶體結構 第乙個記憶體結構稱為 鏈頭,最後...

單鏈表的增刪改查

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