C# 单链表的实现
来源:互联网 发布:手机投资黄金软件 编辑:程序博客网 时间:2024/06/08 10:24
/// <summary> /// 节点类 /// </summary> public class Node<T> { private T _data; private Node<T> _next; public T Data { get { return _data; } set { _data = value; } } public Node<T> Next { get { return _next; } set { _next = value; } } public Node() { this._data = default(T); this._next = null; } public Node(T data) { this._data = data; this._next = null; } } /// <summary> /// 单链表 /// </summary> public class SingleLinked<T> { private Node<T> _head; private int _count; public Node<T> Head { get { return _head; } } public int Count { get { return _count; } } public SingleLinked() { _head = null; } /// <summary> /// 清空 /// </summary> public void Clear() { _head = null; } /// <summary> /// 判断链表是否为空 /// </summary> /// <returns></returns> public bool IsEmpty() { if (_head == null) { return true; } return false; } /// <summary> /// 添加元素 /// </summary> /// <param name="data"></param> public void Add(T data) { Node<T> newNode = new Node<T>(data);//将数据放入节点 if (_head == null) { _head = newNode; _count++; return; } Node<T> p = new Node<T>(); p = _head; while (p.Next != null) { p = p.Next; } p.Next = newNode; _count++; } /// <summary> /// 移除指定的节点 /// </summary> /// <param name="index"></param> /// <returns></returns> public T RemoveAt(int index) { if (IsEmpty() || index > _count - 1 || index < 0) { Console.WriteLine("SingleLinked is empty or index is error!"); throw new Exception("SingleLinked is empty or index is error!"); } Node<T> p = new Node<T>(); p = _head; if (index == 0) //删除头节点 { _head = p.Next; _count--; return p.Data; } int i = 0; Node<T> frontNode = new Node<T>();//要删除节点的前一个节点 while (p.Next != null) { frontNode = p; p = p.Next; if (index == ++i) { break; } } frontNode.Next = p.Next; _count--; return p.Data; } /// <summary> /// 在指定位置插入节点(前插) /// </summary> /// <param name="data"></param> /// <param name="index"></param> /// <returns></returns> public bool Insert(int index, T data) { if (IsEmpty() || index > _count - 1 || index < 0) { Console.WriteLine("SingleLinked is empty or index is error!"); throw new Exception("SingleLinked is empty or index is error!"); } Node<T> newNode = new Node<T>(data); Node<T> p = _head; if (index == 0) { _head = newNode; _head.Next = p; _count++; return true; } Node<T> frontNode = new Node<T>(); int i = 0; while (p.Next != null) { frontNode = p; p = p.Next; if (index == ++i) { break; } } frontNode.Next = newNode; newNode.Next = p; _count++; return true; } /// <summary> /// 获取指定节点的元素 /// </summary> /// <param name="index"></param> /// <returns></returns> public T GetData(int index) { if (IsEmpty() || index > _count - 1 || index < 0) { Console.WriteLine("SingleLinked is empty or index is error!"); throw new Exception("SingleLinked is empty or index is error!"); } Node<T> p = new Node<T>(); p = _head; if (index == 0) { return p.Data; } int i = 0; while (p.Next != null) { p = p.Next; if (index == ++i) { break; } } return p.Data; } /// <summary> /// 判断节点时候包含指定元素 /// </summary> /// <param name="data"></param> /// <returns></returns> public bool Contains(T data) { if (IsEmpty()) return false; if (_head.Data.Equals(data)) return true; Node<T> p = _head; while (p.Next != null) { p = p.Next; if (p.Data.Equals(data)) { return true; } } return false; } /// <summary> /// 获取与元指定素匹配的最后一个元素的索引 /// </summary> /// <param name="data"></param> /// <returns></returns> public int LastIndexOf(T data) { if (IsEmpty()) return -1; Node<T> p = _head; int index = -1; int lastIndex = -1; while (p != null) { index++; if (p.Data.Equals(data)) { lastIndex = index; } p = p.Next; } return lastIndex; } /// <summary> /// 获取与元指定素匹配的第一个元素的索引 /// </summary> /// <param name="data"></param> /// <returns></returns> public int FirstIndexOf(T data) { if (IsEmpty()) return -1; Node<T> p = _head; int index = -1; while (p!=null) { index++; if (p.Data.Equals(data)) { return index; } p = p.Next; } return index; } /// <summary> /// 显示链表节点 /// </summary> public void Display() { Node<T> p = new Node<T>(); p = _head; while (p != null) { Console.WriteLine(p.Data); p = p.Next; } } /// <summary> /// 逆置 /// </summary> public void Reverse() { //头插法:将原链表的头节点的下一个节点插入到当前头接点的前面 Node<T> oriHead = _head;//记录原始头节点 Node<T> tempCurHead = _head;//当前临时头节点 Node<T> nextNode=new Node<T>();//原始节点的下一个节点 //将原始头节点的下一个节点设置为当前的头节点 while (oriHead.Next != null) { nextNode = oriHead.Next;//记录原始节点的下一个节点 oriHead.Next = nextNode.Next;//原始节点的下一个节点指向下下个节点 nextNode.Next = tempCurHead;//原始节点的下一个节点指向当前头节点 tempCurHead = nextNode;//原始节点的下一个节点设置为临时头节点 } _head = tempCurHead; } }
阅读全文
0 0
- C#单链表的实现
- C#单链表的实现
- C# 单链表的实现
- C# 单链表的实现
- c#实现单链表的操作
- C#数据结构单链表的简单实现
- C# 实现单链表的基本操作
- spider的 c#实现
- C#接口的实现
- C#实现的BinaryTree
- HeapSort的C#实现
- C#委托的实现
- C#迭代器的实现
- C#实现的BinaryTree
- SQLiteHelper的C#实现
- C#迷宫的实现~~
- C#四则运算的实现
- C# 俄罗斯方块的实现
- jedis 连接池工具类具体实现demo。
- Linus实验楼笔记——第9节:Linux任务计划crontab
- 这些小工具让你的Android 开发更高效
- Win2008防火墙导致FTP服务器不能访问
- http请求学习总结
- C# 单链表的实现
- Hibernate使用注解的方式配置
- 2017阿里巴巴秋招前端笔试题总结
- Android 框架学习1:EventBus 3.0 的特点与如何使用
- 字符串匹配的KMP算法
- 从无到有构建react项目
- Qt-VS-addin出现错误.VSIX
- Servlet生命周期测试代码2
- Mybatis3文档阅读(一)之简单入门