一、涉及到的知识点
1.定义
在双向链表中,每个节点有两个指针域,一个指向它的前一个节点(即直接前驱),另一个指向它的后一个节点(即直接后继)。这种设计使得双向链表可以进行双向遍历,即可以从头节点开始向前遍历,也可以从尾节点开始向后遍历。
双向链表的节点结构通常如下所示:
struct node
{
// 数据域
int data;
// 指向直接前驱的指针
node* prev;
// 指向直接后继的指针
node* next;
};
2.双向链表与单向链表的区别
双向链表的算法描述和单向链表基本相同,但是双向链表在删除和插入节点时与单向链表有很大的不同:双向链表在删除节点时,不但要修改节点的直接后继指针,还要同时修改节点的直接前驱指针。在插入时更是要修改插入节点的前驱和后继的两个方向上的指针。
二、双向链表实现基础
为了让实现的链表具有更多的实用性,链表的参数的数据类型选择objects。objects参数可以是1个也可以2个甚至更多,总之为了表达双向链表更能为实际生活中的需要,这一步就要极尽所能地、契合实际需要地设计objects的参数。
1.定义参数类objects
/// <summary>
/// 定义双向链表的参数类型是objects类型
/// </summary>
/// <param name="value">参数,数据数值</param>
/// <param name="name">参数,数据名称</param>
/// <param name="index">参数,数据索引</param>
public class objects(int value, string name, int index)
{
public int value { get; set; } = value;
public string name { get; set; } = name;
public int index { get; set; } = index;
}
2.定义节点类listnode
// 定义结点类
public class listnode(objects obj)
{
public objects _object = obj;
public listnode? _next;
public listnode? previous;
public listnode? next
{
get
{
return _next;
}
set
{
_next = value;
}
}
public objects object
{
get
{
return _object;
}
set
{
_object = value;
}
}
}3.定义链表类linkedlist及其方法
在链表类中要定义两个指针_head、_tail,用于正向、逆向查询;_current用于在链表类实时表达当前节点指针;
private listnode? _head; private listnode? _tail; private listnode? _current;
链表中的方法是自定义的,是满足实际需要时因地制宜设计的方法。因此,需要什么方法,就在链表中设计什么方法。
在本例中,切记一切方法的参数类型都是objects类型的。
(1)在链表类中定义append方法、print方法
public class linkedlist
{
//构造函数
public linkedlist()
{
_listcountvalue = 0;
_head = null;
_tail = null;
}
/// <summary>
/// 链表名
/// </summary>
private string listname = "";
public string listname
{
get
{
return listname;
}
set
{
listname = value;
}
}
/// <summary>
/// 头指针
/// </summary>
private listnode? _head;
/// <summary>
/// 尾指针
/// </summary>
private listnode? _tail;
/// <summary>
/// 定义字段当前指针current
/// 定义属性current
/// 字段是一种变量,属性是一种方法
/// </summary>
private listnode? _current;
public listnode? current
{
get
{
return _current;
}
set
{
_current = value;
}
}
// 定义链表数据的个数
private int _listcountvalue;
/// <summary>
/// 尾部添加数据
/// </summary>
public void append(objects value)
{
listnode newnode = new(value);
if (isnull()) //如果头指针为空
{
_head = newnode;
_tail = newnode;
}
else
{
_tail!.next = newnode; // 原尾节点的下节点=新节点
newnode.previous = _tail;// 新节点的前节点=原尾结点
_tail = newnode; // 新尾结点=新节点
}
_current = newnode; // 当前节点=新节点
_listcountvalue += 1; // 节点总数增加1
}
/// <summary>
/// 删除当前的数据
/// </summary>
public void delete()
{
if (!isnull()) //若为空链表
{
//删除头部
if (isbof())
{
_head = _current!.next;
_current = _head;
_listcountvalue -= 1;
return;
}
//删除尾
if (iseof())
{
_tail = _current!.previous;
_tail!.next = null;
_current = _tail;
_listcountvalue -= 1;
return;
}
//若删除中间数据
_current!.previous!.next = _current.next;
_current = _current.previous;
_listcountvalue -= 1;
return;
}
}(2)在链表类中定义movefirst、moveprevious、movenext、movetail、clear、getcurrentvalue、isnull、iseof、isbof、
/// <summary>
/// 向后移动一个数据
/// </summary>
public void movenext()
{
if (!iseof()) _current = _current!.next;
}
/// <summary>
/// 向前移动一个数据
/// </summary>
public void moveprevious()
{
if (!isbof()) _current = _current!.previous;
}
/// <summary>
/// 移动到第一个数据
/// </summary>
public void movefirst()
{
_current = _head;
}
/// <summary>
/// 移动到最后一个数据
/// </summary>
public void movetail()
{
_current = _tail;
}
/// <summary>
/// 判断是否为空链表
/// </summary>
public bool isnull()
{
if (_listcountvalue == 0)
return true;
else
return false;
}
/// <summary>
/// 判断是否为到达尾部
/// is end of file
/// </summary>
public bool iseof()
{
if (_current == _tail)
return true;
else
return false;
}
/// <summary>
/// 判断是否为到达头部
/// is beginning of file
/// </summary>
public bool isbof()
{
if (_current == _head)
return true;
else
return false;
}
/// <summary>
/// 获取当前节点数据
/// </summary>
public objects getcurrentvalue()
{
return _current!._object;
}
/// <summary>
/// 取得链表的数据个数
/// </summary>
public int listcount
{
get
{
return _listcountvalue;
}
}
/// <summary>
/// 清空链表
/// </summary>
public void clear()
{
movefirst();
while (!isnull())
{
delete();
}
}(3)在链表类中定义insert()、insertascending()、insertunascending()、sortlist()
/// <summary>
/// 对链表数据冒泡排序
/// </summary>
public static listnode? sortlist(listnode? head)
{
if (head == null || head.next == null)
{
return head;
}
bool swapped;
do
{
swapped = false;
listnode? current = head;
while (current != null && current.next != null)
{
if (current.next.object.value < current.object.value)
{
(current.next._object, current._object) = (current._object, current.next._object);
swapped = true;
}
current = current.next;
}
} while (swapped);
return head;
}
/// <summary>
/// 在当前位置前插入数据
/// </summary>
public void insert(objects value)
{
listnode newnode = new(value);
if (isnull())
{
//为空表,则添加
append(value);
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;
}
/// <summary>
/// 进行升序插入
/// </summary>
public void insertascending(objects value)
{
//为空链表
if (isnull())
{
append(value);
return;
}
//移动到头
movefirst();
if (value.value < getcurrentvalue().value) //满足条件,则插入,退出
{
insert(value);
return;
}
while (true)
{
if (value.value < getcurrentvalue().value)//满足条件,则插入,退出
{
insert(value);
break;
}
if (iseof())
{
append(value);
break;
}
movenext();
}
}
/// <summary>
/// 进行非升序插入
/// </summary>
public void insertunascending(objects value)
{
//为空链表
if (isnull())
{
append(value);
return;
}
//移动到头
movefirst();
if (value.value > getcurrentvalue().value)//满足条件,则插入,退出
{
insert(value);
return;
}
while (true)
{
if (value.value > getcurrentvalue().value)//满足条件,则插入,退出
{
insert(value);
break;
}
if (iseof())
{
append(value);
break;
}
movenext();
}
}(4)findobjects(int value)、findobjects(string name)、deleteobjects(string name)、deleteobjects(int value)
/// <summary>
/// 根据数据名称查询数据
/// </summary>
/// <param name="name">
/// 数据名称
/// </param>
public objects? findobjects(string name)
{
listnode? nodes = _head;
if (isnull())
{
return null;
}
else if (iseof())
{
return null;
}
else
while (nodes!._object.name != name)
{
if (nodes.next == null)
{
_current = nodes;
return null;
}
nodes = nodes.next;
}
_current = nodes;
return nodes._object;
}
/// <summary>
/// 根据参数值查询数据
/// </summary>
/// <param name="value">
/// 数据编号
/// </param>
public objects? findobjects(int value)
{
listnode? nodes = _head;
if (isnull())
{
return null;
}
else if (iseof())
{
return null;
}
else
while (nodes!._object.value != value)
{
if (nodes.next == null)
{
_current = nodes;
return null;
}
nodes = nodes.next;
}
_current = nodes;
return nodes._object;
}
/// <summary>
/// 根据数据名称删除数据
/// </summary>
/// <param name="name">
/// 数据名称
/// </param>
public objects? deleteobjects(string name)
{
objects? objects;
objects = findobjects(name);
delete();
return objects;
}
/// <summary>
/// 根据参数值删除数据
/// </summary>
/// <param name="value">
/// 数据编号
/// </param>
public objects? deleteobjects(int value)
{
objects? objects;
objects = findobjects(value);
delete();
return objects;
}
public void print()
{
listnode? current = _head;
while (current != null)
{
console.writeline(current._object.value);
current = current.next;
}
}
public void print()
{
listnode? current = _head;
while (current != null)
{
console.writeline(current._object.value);
current = current.next;
}
}
}三、一些objects类对象操作技巧
1.objects类对象用于比较
objects类对象是复合类型的数据,本身不能用于比较,但是其int类型的属性值是可以进行比较的。比如,应该使用“value”属性来访问对象的整数值。
listnode newnode = new(obj);
if (_head == null || _head.object.value >= obj.value)
{
newnode.next = _head;
return newnode;
}
else
{
listnode? previous = _head;
listnode? current = _head!.next;
while (current != null && current.object.value < obj.value)
{
previous = current;
current = current!.next;
}
newnode!.next = current;
previous!.next = newnode;
}if (_head != null)
{
return _current!.object.value;
}
else
{
return default; // 或者 return 0;
}
while (current != null && current.next != null)
{
if (current.next.object.value < current.object.value)
{
(current.next.object.value, current.object.value) = (current.object.value, current.next.object.value);
swapped = true;
}
current = current.next;
}
2. objects类对象用于链表的初始化
objects类对象是复合类型的数据,用于链表初始化时要复杂一些,不再向单纯的int、string类型赋值得那么简单直接。
objects类对象用于链表初始化要体现 objects类的定义,实参和形参的数量和类型要一一对应。
linkedlist linkedlist = new(); linkedlist.append(new objects(5, "five", 1)); linkedlist.append(new objects(2, "two", 2)); linkedlist.append(new objects(8, "eight", 3)); linkedlist.append(new objects(1, "one", 4));
四、实例main()
在实例的双向链表类中,设计一个append方法向链表的末尾追加初始数据5,2,8,1。然后用print方法显示链表数据。
链表类中的其他方法的示例,详见main方法。
class program
{
static void main(string[] args)
{
argumentnullexception.throwifnull(args);
linkedlist list = new();
// 插入结点
list.append(new objects(5, "five", 0));
list.append(new objects(2, "two", 1));
list.append(new objects(8, "eight", 2));
list.append(new objects(1, "one", 3));
list.append(new objects(3, "three", 4));
// 获取当前结点的值
console.write("当前结点的值:");
console.writeline(list.getcurrentvalue().value);
// 移动到第一个结点
list.movefirst();
console.write("第一结点的值:");
console.writeline(list.getcurrentvalue().value);
// 移动到下一个结点
list.movenext();
console.write("下一结点的值:");
console.writeline(list.getcurrentvalue().value);
// 移动到上一个结点
list.moveprevious();
console.write("上一结点的值:");
console.writeline(list.getcurrentvalue().value);
list.print();
console.writeline("*初始数据*");
// 删除尾首2个结点
list.movetail();
list.delete();
list.movefirst();
list.delete();
list.print();
console.writeline("*删除节点*");
// 插入升序结点
linkedlist.sortlist(list._head);//先排序
list.insertascending(new objects(6, "six", 5));
list.insertascending(new objects(4, "four", 6));
list.insertascending(new objects(9, "none", 7));
list.print();
console.writeline("*升序插入*");
// 插入非升序结点
list.insertunascending(new objects(7, "seven", 8));
list.insertunascending(new objects(3, "three", 9));
list.insertunascending(new objects(10, "ten", 10));
list.print();
console.writeline("*非升序插入*");
// 清空链表
list.clear();
list.print();
console.writeline();
console.writeline("**清空就没有了**");
// 插入数据
list.insert(new objects(0, "zero", 11));
list.insert(new objects(1, "one", 12));
list.insert(new objects(2, "two", 13));
list.insert(new objects(3, "three", 14));
list.print();
console.writeline("*新数据*");
console.writeline(list.getcurrentvalue().value);
console.writeline("*最后插入的是当前值*");
list.movefirst();
console.writeline(list.getcurrentvalue().value);
console.writeline("*当前值居然是头节点*");
list.insert(new objects(5, "five", 15));
list.print();
console.writeline("*在当前值前面插入5*");
list.movenext();
list.insert(new objects(6, "six", 16));
list.print();
console.writeline("*在当前节点的下节点前面插入6*");
list.movefirst();
list.moveprevious();
list.insert(new objects(7, "seven", 17));
list.print();
console.writeline("*头节点没有前节点故在头节点前面插入7*");
list.findobjects(3);
console.writeline(list.getcurrentvalue().value);
console.writeline("*按数据值3查找*");
list.findobjects("three");
console.writeline(list.getcurrentvalue().value);
console.writeline("*按参数名称three查找*");
list.deleteobjects(3);
list.print();
console.writeline("*已经删除了值=3的参数*");
list.deleteobjects("six");
list.print();
console.writeline("*已经删除了名称=six的参数*");
}
}运行结果:
当前结点的值:3
第一结点的值:5
下一结点的值:2
上一结点的值:5
5
2
8
1
3
*初始数据*
2
8
1
*删除节点*
1
2
4
6
8
9
*升序插入*
10
7
3
1
2
4
6
8
9
*非升序插入*
**清空就没有了**
3
2
1
0
*新数据*
3
*最后插入的是当前值*
3
*当前值居然是头节点*
5
3
2
1
0
*在当前值前面插入5*
5
6
3
2
1
0
*在当前节点的下节点前面插入6*
7
5
6
3
2
1
0
*头节点没有前节点故在头节点前面插入7*
3
*按数据值3查找*
3
*按参数名称three查找*
7
5
6
2
1
0
*已经删除了值=3的参数*
7
5
2
1
0
*已经删除了名称=six的参数*

到此这篇关于一文探索c#中实现双向链表的方法的文章就介绍到这了,更多相关c#双向链表内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论