双向链表
来源:互联网 发布:linq to sql pdf 编辑:程序博客网 时间:2024/05/16 15:14
1、双向链表的图文解释:
基本结构:
存储连接情况:
2、下面开始讲解实现双向链表
双向链表定义:
public class ListNode { public ListNode(int NewValue) { Value = NewValue; } //前一个 public ListNode Previous; // 后一个 public ListNode Next; // 值 public int Value; }
下面定义一个线性表
public class Clist { public Clist() { //构造函数 //初始化 ListCountValue = 0; Head = null; Tail = null; } // 头指针 private ListNode Head; // 尾指针 private ListNode Tail; // 当前指针 private ListNode Current; //链表数据的个数 private int ListCountValue; //..............(后面是对链表操作的方法) }
尾部添加数据
//尾部添加数据 public void Append(int DataValue) { ListNode NewNode = new ListNode(DataValue); if (IsNull()) //如果头指针为空 { Head = NewNode; Tail = NewNode; } else { Tail.Next = NewNode; NewNode.Previous = Tail; Tail = NewNode; } Current = NewNode; //链表数据个数加一 ListCountValue += 1; }
删除当前数据
//删除当前的数据 public void Delete() { //若为空链表 if (!IsNull()) { //若删除头 if (IsBof()) { Head = Current.Next; Current = Head; ListCountValue -= 1; return; } //若删除尾 if (IsEof()) { Tail = Current.Previous; Current = Tail; ListCountValue -= 1; return; } //若删除中间数据 Current.Previous.Next = Current.Next; Current = Current.Previous; ListCountValue -= 1; return; } }
移动数据操作
// 向后移动一个数据 public void MoveNext() { if (!IsEof()) Current = Current.Next; } // 向前移动一个数据 public void MovePrevious() { if (!IsBof()) Current = Current.Previous; } // 移动到第一个数据 public void MoveFrist() { Current = Head; } // 移动到最后一个数据 public void MoveLast() { Current = Tail; }
各种判断、获取数据操作、清空链表操作
// 判断是否为空链表 public bool IsNull() { if (ListCountValue == 0) return true; return false; } // 判断是否为到达尾 public bool IsEof() { if (Current == Tail) return true; return false; } // 判断是否为到达头部 public bool IsBof() { if (Current == Head) return true; return false; } // 取得当前数据 public int GetCurrentValue() { return Current.Value; } // 取得链表的数据个数 public int ListCount { get { return ListCountValue; } } // 清空链表 public void Clear() { MoveFrist(); while (!IsNull()) { //若不为空链表,从尾部删除 Delete(); } }
当前位置插入:
// 在当前位置前插入数据 public void Insert(int DataValue) { ListNode NewNode = new ListNode(DataValue); if (IsNull()) { //为空表,则添加 Append(DataValue); return; } if (IsBof()) { //为头部插入 NewNode.Next = Head; Head.Previous = NewNode; Head = NewNode; Current = Head; ListCountValue += 1; return; } //中间插入 NewNode.Next = Current; NewNode.Previous = Current.Previous; Current.Previous.Next = NewNode; Current.Previous = NewNode; Current = NewNode; ListCountValue += 1; }
进行升序插入:
// 进行升序插入 public void InsertAscending(int InsertValue) { //参数:InsertValue 插入的数据 //为空链表 if (IsNull()) { //添加 Append(InsertValue); return; } //移动到头 MoveFrist(); if ((InsertValue < GetCurrentValue())) { //满足条件,则插入,退出 Insert(InsertValue); return; } while (true) { if (InsertValue < GetCurrentValue()) { //满族条件,则插入,退出 Insert(InsertValue); break; } if (IsEof()) { //尾部添加 Append(InsertValue); break; } //移动到下一个指针 MoveNext(); } }
进行降序插入:
//进行降序插入 public void InsertUnAscending(int InsertValue) { //参数:InsertValue 插入的数据 //为空链表 if (IsNull()) { //添加 Append(InsertValue); return; } //移动到头 MoveFrist(); if (InsertValue > GetCurrentValue()) { //满足条件,则插入,退出 Insert(InsertValue); return; } while (true) { if (InsertValue > GetCurrentValue()) { //满族条件,则插入,退出 Insert(InsertValue); break; } if (IsEof()) { //尾部添加 Append(InsertValue); break; } //移动到下一个指针 MoveNext(); } }
备注:以上整理自asp.net 开发实例大全(提高卷),软件开发技术联盟。
0 0
- SzNOI 双向约瑟夫(双向链表)
- 双向链表&&堆栈
- 双向链表
- 使用双向链表
- 双向链表
- 双向循环链表
- 双向循环链表
- 双向链表
- 实现双向链表
- 双向循环链表
- 建立双向链表
- 双向链表
- 双向链表
- 双向循环链表
- 双向链表
- 链表-双向链表
- 双向链表
- 双向动态链表
- 这 10 篇安卓热文,你别错过哦
- android view
- I’m stuck!
- android 下载模拟器镜像文件遇到的问题
- Eclipse突然运行慢的解决实践(以前很快但之后变慢)
- 双向链表
- 10步创建一个Laravel 5 package(翻译)
- 使用Retrofit2.0上传文件,可以监听上传进度[客户端+服务器端代码]
- Maven的使用-最最基础的操作
- Python与图像处理5
- mysql插入中文内容出错问题解决
- Forge定位功能使用教程
- touch事件
- EM 算法