c 單鏈表基礎操作

2021-08-11 16:58:19 字數 2140 閱讀 2021

最近開始刷leetcode,感覺自己的基礎太差,基本的鍊錶部分的問題都得想很久,今天花了乙個晚上的時間將鍊錶的基礎操作都實現了一遍,記錄一下自己的收穫

最大的是收穫的一句話:鍊錶的操作使用替身

(替身其實就是類似指標的東西,但是我之前一直沒有想到這一點,導致思路堵塞)

舉個例子

//我要操作first,如遍歷等

listnode temp = first;//先建立乙個first的替身

temp = ***;//直接修改temp並不會改變first的值

//有兩種操作會改變first中的值

temp.val = ***;//直接改變first中對應節點的值,比如temp指向first[0],這一步下去first[0]的值就相應的改變

temp.next = ***;//改變這個節點,鏈結到其他節點,同樣first對應節點也被改變了

temp = temp.next;//我們經常會看到這個操作,意思是temp指向下一節點,也不會改變first的中任何值,相當於1=1

下面給我練習寫的鍊錶類,其中的方法沒有經過詳細測試,只是能跑而已

using system;

namespace leetcode

; linklist ll = new linklist(arr);

6);

ll.reversal();

ll.print();

}/// /// 鍊錶操作類

///

public class linklist

/// /// 順序列印鍊錶

///

public void print()

}/// /// 將陣列轉換成單鏈表

///

///

public linklist(int a)

}/// /// 求單鏈表的長度

///

public int length

return len;}}

/// /// 獲取鍊錶中指定下標的值

///

///

///

public int get(int index)

return -1;

}/// /// 求值為val的節點的下標,返回第乙個

///

///

///

public int locate(int val)

return -1;

}/// /// 在鍊錶的指定位置插入值為val的節點

///

///

///

public bool insert(int index,int val)

t = t.next;

count++;

}return false;

}/// /// 刪除下標為index的節點

///

///

///

public bool delete(int index)

t = t.next;

count++;

}return false;

}/// /// 判斷是否為空

///

///

public bool isempty()

/// /// 將鍊錶按照從小到大排序,值交換(氣泡排序)

///

public void sortvalue()

t2 = t2.next;

}t1 = t1.next;}}

/// /// 將鍊錶進行翻轉,遞迴實現,節點翻轉

///

public void reversal()

// 先回溯到最終值,反過來進行節點的翻轉

listnode reversal(listnode head)

listnode rehead = reversal(head.next);//rehead始終為最後的節點

head.next.next = head;

head.next = null;

return rehead;}}

public class listnode}}

}

單鏈表基礎操作C 實現

最近在複習資料結構,就把單鏈表的基礎操作過了一篇 node.h標頭檔案 templatestruct node node t nval 單鏈錶類檔案 include include node.h include using namespace std templateclass singlelink...

C單鏈表操作

今天面試給 宇龍酷派 鄙視了。我想說,其實鍊錶反轉我會!單鏈表 初始化 建立 顯示 刪除 插入特定位置 刪除特定位置 反轉操作。include include include include typedef struct student node 初始化 node initnode head nex...

C單鏈表操作

函式順序依次為單鏈表的建立 頭插和尾插法 初始化,判空,遍歷,求鍊錶長度,按值查詢,按位查詢,插入,刪除,銷毀操作 並且在主函式中舉例說了鍊錶的建立,遍歷,求長,刪除,插入操作 高階操作 c迴圈鍊錶 include include typedef int datatype 用datatype 替代 ...