C#数据结构-单链表
理论基础:
链表是用一组任意的存储单元来存储线性表中的数据元素。
如果结点的引用域只存储该结点直接后继结点的存储地址,则该链表叫单链表(Singly Linked List)。
单链表由头引用H唯一确定。头引用指向单链表的第一个结点,也就是把单链表第一个结点的地址放在H中。
C#实现:
1接口
引用线性表的接口IListDS<T>
2实现
首先,必须定义一个单链表的节点类。
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来实现,有兴趣的朋友试试。
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#数据结构-单链表相关推荐
- 20175330 数据结构-单链表(选做)
要求 参见附件,补充MyList.java的内容,提交运行结果截图(全屏) 课下推送代码到码云 ``` public class MyList { public static void mai ...
- php链表和联表的区别,PHP_浅谈PHP链表数据结构(单链表),链表:是一个有序的列表,但 - phpStudy...
浅谈PHP链表数据结构(单链表) 链表:是一个有序的列表,但是它在内存中是分散存储的,使用链表可以解决类似约瑟夫问题,排序问题,搜索问题,广义表 单向链表,双向链表,环形链表 PHP的底层是C,当一个 ...
- 数据结构——单链表的C++实现
数据结构--单链表的C++实现 \qquad单链表的创建.求长度.查找.插入和删除的C++实现. #include<iostream> using namespace std;//1.定义 ...
- php mysql 链表_浅谈PHP链表数据结构(单链表)
链表:是一个有序的列表,但是它在内存中是分散存储的,使用链表可以解决类似约瑟夫问题,排序问题,搜索问题,广义表 单向链表,双向链表,环形链表 PHP的底层是C,当一个程序运行时,内存分成五个区(堆区, ...
- python 单链表是否有回路_(Python3)数据结构--单链表之判断链表是否有环
前言 有Python基础 有数据结构单链表基础,没接触过的可以看下面链接 https://blog.csdn.net/sf9898/article/details/104946291 原理和实现 有一 ...
- 数据结构 —— 单链表(超详细图解 接口函数实现)
系列文章目录 数据结构 -- 顺序表 数据结构 -- 单链表 数据结构 -- 双向链表 数据结构 -- 队列 数据结构 -- 栈 数据结构 -- 堆 数据结构 -- 二叉树 数据结构 -- 八大排序 ...
- C语言数据结构单链表链表
数据结构–单链表 学习了顺序表,我们发现顺序表在向里面存放数据的时候很麻烦,比如我们要使用头插法存放一个数据到顺序表的时候,我们要将整个表都向后挪一位,这个操作就让人很难受.那么有没有一种结构可以让我 ...
- 数据结构——单链表(小白入门第二天)
一.什么是单链表? 定义:每个结点 除了存放数据元素外,还要存储指向下一个节点的指针: 优点:不要求大片连续空间,改变容量方便: 缺点:不可随机存取,要耗费一定空间存放指针 局限性:无法逆向检索 二. ...
- 数据结构-单链表基本操作(C语言实现)
参考书:王道考研数据结构 (此贴为博主学习408的笔记,因博主也是学习者,个人总结如有错误欢迎指正.如有侵权请告知,马上删除致歉) 单链表定义 单链表是线性表的链式存储,通过一组任意的存储单元来存 ...
- 数据结构 -- 单链表
快要放假了,实在是待不住了,论文也看不下去,也没啥其他的事做,就写写常用的数据结构吧,正好下个学期就要找工作了,也是方便以后自己使用这些数据结构,好吧,这里实现的数据结构没有太多的错误控制 ...
最新文章
- 【c语言】求最大最小值
- golangsha1解码_golang中几种加密方式的处理
- make: Nothing to be done for `everything'.的原因
- Winform中将WebBrower浏览器控件由IE内核修改为Chrome的WebKit内核
- mysql老叶博客_MySQL binlog后面的编号最大是多大?【老叶茶馆公众号】
- js修改id的值_如何修改pytesthtml源码来优化接口自动化测试报告
- pymongo的使用 0916
- C# 中XML序列化与反序列化学习笔记
- Openlayers4加载天地图
- python 笔记数据类型
- 微软Hyper-V虚拟化技术全面体验
- android百度在线音乐api,百度音乐免费API接口
- 福布斯中国上市公司最佳CEO排行
- Python关键词百度指数采集,抓包Cookie及json数据处理
- Unity3D 获取资源运行时内存和硬盘大小
- 他们竟用后台数据偷窥喜欢的女性!
- 【眼见为实】数据库并发问题 封锁协议 隔离级别
- Html控制大华摄像头
- 三、Fiddler抓包——Fiddler过滤器-Fiddler抓包数据筛选
- 什么是多态?为什么用多态?有什么好处?[转]
热门文章
- ORACLE EXP/IMP 说明
- ecs服务器数据迁移_某国际物流集团的云迁移解决方案
- python做硬件自动化测试仪器_基于Python PyVisa和GPIB的硬件测试仪器控制方法
- mysql基础知识整理_MYSQL基础知识整理
- oracle中OEM证书失效怎么办,安全证书过期怎么办 网站安全证书失效处理【解决方法】...
- 如何让fragment刷新界面_快速实现android版抖音主界面的心得
- c语言编程计算人口增长模式转变示意图,读“人口增长模式及其转变示意图”,回答下列问题。(5分)(1)图中字母代表的人口增长模式是:A____________、B____...
- mysql 超长记录_谁记录了mysql error log中的超长信息(记pt-stalk一个bug的定位过程)...
- apktool重新打包,error:No resource identifier found for attribute ‘compileSdkVersionCodename‘ in package
- ukt机器人_doc/readme/feishu.md · 贫道法号-乱来/PrometheusAlert - Gitee.com