using System;
namespace list
{
/// <summary>
/// summary description for listnode.
/// </summary>
// 结点类
public class listnode
{
public listnode(int newvalue)
{
value = newvalue;
}
/// <summary>
/// 前一个
/// </summary>
///
public listnode previous;
/// <summary>
/// 后一个
/// </summary>
public listnode next;
/// <summary>
/// 值
/// </summary>
public int value;
}
/// <summary>
/// 链表类
/// </summary>
//定义结点之后,开始类线性表的操作编程了.在list 类中,采用了,head ,tail, current,三个指针,使用append ,movefrist,moveprevious,movenext,movelast ,delete,insertascending,insertunascending ,clear 实现移动,添加,删除,升序插入,降序插入,清空链表操作,getcurrentvalue() 方法取得当前的值。
public class clist
{
public clist()
{
//构造函数
//初始化
listcountvalue = 0;
head = null;
tail = null;
}
/// <summary>
/// 头指针
/// </summary>
private listnode head;
/// <summary>
/// 尾指针
/// </summary>
private listnode tail;
/// <summary>
/// 当前指针
/// </summary>
private listnode current;
/// <summary>
/// 链表数据的个数
/// </summary>
private int listcountvalue;
/// <summary>
/// 尾部添加数据
/// </summary>
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;//链表数据个数加一
}
/// <summary>
/// 删除当前的数据
/// </summary>
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;
}
}
/// <summary>
/// 向后移动一个数据
/// </summary>
public void movenext()
{
if (!iseof()) current = current.next;
}
/// <summary>
/// 向前移动一个数据
/// </summary>
public void moveprevious()
{
if (!isbof()) current = current.previous;
}
/// <summary>
/// 移动到第一个数据
/// </summary>
public void movefrist()
{
current = head;
}
/// <summary>
/// 移动到最后一个数据
/// </summary>
public void movelast()
{
current = tail;
}
/// <summary>
/// 判断是否为空链表
/// </summary>
public bool isnull()
{
if (listcountvalue == 0)
return true;
return false;
}
/// <summary>
/// 判断是否为到达尾部
/// </summary>
public bool iseof()
{
if (current == tail)
return true;
return false;
}
/// <summary>
/// 判断是否为到达头部
/// </summary>
public bool isbof()
{
if (current == head)
return true;
return false;
}
public int getcurrentvalue()
{
return current.value;
}
/// <summary>
/// 取得链表的数据个数
/// </summary>
public int listcount
{
get
{
return listcountvalue;
}
}
/// <summary>
/// 清空链表
/// </summary>
public void clear()
{
movefrist();
while (!isnull())
{
//若不为空链表,从尾部删除
delete();
}
}
/// <summary>
/// 在当前位置前插入数据
/// </summary>
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;
}
/// <summary>
/// 进行升序插入
/// </summary>
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();
}
}
/// <summary>
/// 进行降序插入
/// </summary>
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();
}
}
}
}
好了,一个简单的链表类实现了,当然更有许多的功能,能够根据自己的需要添加就好了。to be continue 。
以上内容由 华夏名网 收集整理,如转载请注明原文出处,并保留这一部分内容。
2227

被折叠的 条评论
为什么被折叠?



