日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#数据结构-单链表

發布時間:2023/12/1 C# 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#数据结构-单链表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?? 理論基礎:

??? 鏈表是用一組任意的存儲單元來存儲線性表中的數據元素

??? 如果結點的引用域只存儲該結點直接后繼結點的存儲地址,則該鏈表叫單鏈表(Singly Linked List)。

????單鏈表由頭引用H唯一確定。頭引用指向單鏈表的第一個結點,也就是把單鏈表第一個結點的地址放在H中。

?? C#實現:

??? 1接口

???????? 引用線性表的接口IListDS<T>

????2實現

???????? 首先,必須定義一個單鏈表的節點類。

Code
?1?public?class?Node<T>
?2????{
?3????????private?T?data;????????//數據域
?4????????private?Node<T>?next;??//引用域
?5
?6????????
?7????????public?Node(T?val)
?8????????{
?9????????????data?=?val;
10????????????next?=?null;
11????????}

12
13????????public?Node()
14????????{
15????????????data?=?default(T);
16????????????next?=?null;
17????????}

18
19????????public?T?Data
20????????{
21????????????get
22????????????{
23????????????????return?data;
24????????????}

25????????????set
26????????????{
27????????????????data?=?value;
28????????????}

29????????}

30????????public?Node<T>?Next
31????????{
32????????????get
33????????????{
34????????????????return?next;
35????????????}

36????????????set
37????????????{
38????????????????next?=?value;
39????????????}

40????????}

41
42
43????}

???? 實現主體類

???? Append,Insert,InsertBack三個方法實質上都是插入操作,可以考慮用overload或者override來實現,有興趣的朋友試試。

Code
??1public?class?LinkList<T>?:?IListDS<T>
??2????{
??3????????private?Node<T>?head;
??4????????public?Node<T>?Head
??5????????{
??6????????????get
??7????????????{
??8????????????????return?head;
??9????????????}

?10????????????set
?11????????????{
?12????????????????head?=?value;
?13????????????}

?14????????}

?15????????public?LinkList()
?16????????{
?17????????????head?=?null;
?18????????}

?19
?20????????/**////?<summary>
?21????????///?獲取長度
?22????????///?</summary>
?23????????///?<returns></returns>

?24????????public?int?GetLength()
?25????????{
?26????????????Node<T>?p?=?head;
?27????????????int?len?=?0;
?28????????????while?(p?!=?null)
?29????????????{
?30????????????????++len;
?31????????????????p?=?p.Next;
?32????????????}

?33????????????return?len;
?34????????}

?35
?36????????/**////?<summary>
?37????????///?清空操作
?38????????///?</summary>

?39????????public?void?Clear()
?40????????{
?41????????????head?=?null;
?42????????}

?43
?44????????/**////?<summary>
?45????????///?判斷線性表是否為空
?46????????///?</summary>
?47????????///?<returns></returns>

?48????????public?bool?IsEmpty()
?49????????{
?50????????????if?(head?==?null)
?51????????????{
?52????????????????return?true;
?53????????????}

?54????????????else
?55????????????{
?56????????????????return?false;
?57????????????}

?58????????}

?59
?60????????/**////?<summary>
?61????????///?附加操作,線性表未滿,將值為item的新元素添加到末尾
?62????????///?</summary>
?63????????///?<param?name="item"></param>

?64????????public?void?Append(T?item)
?65????????{
?66????????????Node<T>?newNode?=?new?Node<T>(item);??//根據元素創建新的節點
?67????????????Node<T>?node?=?new?Node<T>();
?68
?69????????????if?(head?==?null)
?70????????????{
?71????????????????head?=?newNode;
?72????????????????return;
?73????????????}

?74????????????node?=?head;
?75????????????while?(node.Next?!=?null)
?76????????????{
?77????????????????node?=?node.Next;
?78????????????}

?79????????????node.Next?=?newNode;
?80
?81????????}

?82
?83????????/**////?<summary>
?84????????///?尋找節點
?85????????///?</summary>
?86????????///?<param?name="i"></param>
?87????????///?<returns></returns>

?88????????public?Node<T>?FindNode(int?i)
?89????????{
?90????????????if?(IsEmpty())
?91????????????{
?92????????????????Console.Write("List?is?empty");
?93????????????????return?null;
?94????????????}

?95????????????if?(i?<?1)
?96????????????{
?97????????????????Console.Write("Index?is?error");
?98????????????????return?null;
?99????????????}

100????????????Node<T>?current?=?head;
101????????????int?j?=?1;
102
103????????????while?(current.Next?!=?null?&&?j?<?i)
104????????????{
105????????????????++j;
106????????????????current?=?current.Next;
107????????????}

108????????????return?current;
109????????}

110
111????????/**////?<summary>
112????????///?插入操作,在第i個節點前面插入item
113????????///?</summary>
114????????///?<param?name="item"></param>
115????????///?<param?name="i"></param>

116????????public?void?Insert(T?item,?int?i)
117????????{
118????????????Node<T>?newNode?=?new?Node<T>(item);
119????????????Node<T>?node?=?new?Node<T>();
120????????????Node<T>?current?=?FindNode(i);
121????????????if?(current?!=?null)
122????????????{
123????????????????node?=?current;???????//對目標節點備份
124????????????????newNode.Next?=?current;
125????????????????node.Next?=?newNode;
126????????????}

127????????}

128
129????????/**////?<summary>
130????????///?插入操作,在第i個節點后面插入item
131????????///?</summary>
132????????///?<param?name="item"></param>
133????????///?<param?name="i"></param>

134????????public?void?InsertBack(T?item,?int?i)
135????????{
136????????????Node<T>?newNode?=?new?Node<T>(item);
137????????????Node<T>?current?=?FindNode(i);
138????????????if?(current?!=?null)
139????????????{
140????????????????newNode.Next?=?current.Next;
141????????????????current.Next?=?newNode;
142????????????}

143????????}

144
145????????/**////?<summary>
146????????///?刪除操作
147????????///?</summary>
148????????///?<param?name="i"></param>
149????????///?<returns></returns>

150????????public?T?Delete(int?i)
151????????{
152????????????Node<T>?current?=?FindNode(i);
153????????????Node<T>?node?=?new?Node<T>();
154????????????if?(current?!=?null)
155????????????{
156????????????????node?=?current;???//對目標節點備份
157????????????????node.Next?=?current.Next;
158????????????????return?current.Data;
159????????????}

160????????????else
161????????????{
162????????????????Console.Write("the?node?is?not?exist!");
163????????????????return?default(T);
164????????????}

165????????}

166
167????????/**////?<summary>
168????????///?去表元
169????????///?</summary>
170????????///?<param?name="i"></param>
171????????///?<returns></returns>

172????????public?T?GetElem(int?i)
173????????{
174????????????Node<T>?current?=?FindNode(i);
175
176????????????if?(current?!=?null)
177????????????{
178????????????????return?current.Data;
179????????????}

180????????????else
181????????????{
182????????????????Console.Write("the?node?is?not?exist!");
183????????????????return?default(T);
184????????????}

185????????}

186
187????????/**////?<summary>
188????????///?按值查找
189????????///?</summary>
190????????///?<param?name="value"></param>
191????????///?<returns></returns>

192????????public?int?Locate(T?value)
193????????{
194????????????if?(IsEmpty())
195????????????{
196????????????????Console.WriteLine("List?is?Empty!");
197????????????????return?-1;
198????????????}

199????????????Node<T>?current?=?new?Node<T>();
200????????????current?=?head;
201????????????int?i?=?1;
202????????????while?(current.Next?!=?null?&&?!current.Data.Equals(value))
203????????????{
204????????????????current?=?current.Next;
205????????????????++i;
206????????????}

207????????????return?i;
208????????}

209????}

碰到的問題:1 方法參數為int類型,改用object類型碰到一些問題,查找節點的時候,遍歷整個鏈表,如何對比參數節點

?????????????????? 對比參數會不會影響算法的時間復雜度

??????????????? 2 append,insert,insertback方法本質上都是插入操作,有沒有更好的方法實現?

代碼沒有經過測試,有興趣的朋友可以試試,有問題,告知一下!???

轉載于:https://www.cnblogs.com/Richet/archive/2008/10/15/1311706.html

總結

以上是生活随笔為你收集整理的C#数据结构-单链表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。