设计链表算法,它包括下列操作:
创建链表
插入结点
删除结点
查找结点
遍历链表
删除链表
要求(请选作):
写出算法,实现创建链表、遍历链表和删除链表的程序。
写出算法并实现,其中的DATA是其它类的对象。
写出算法并实现,其中的DATA是一个嵌套类的对象。
using System;
using System.Collections;
namespace exercises3_1
{
/// <summary>
/// </summary>
class ListNode:IEnumerable,IEnumerator
{
//声明节点
Node firstNode,lastNode,currentNode;
int _count = 0;
/// <summary>
/// 删除一个节点
/// </summary>
private void delete()
{
if(currentNode.Equals(firstNode))
firstNode = firstNode.nextNode;
else
{
Node previous = firstNode;
while(previous.nextNode != currentNode)
{
previous = previous.nextNode;
}
previous.nextNode = currentNode.nextNode;
}
_count--;
}
public void deleteall()
{
currentNode=null;
Console.WriteLine("链表为空");
}
/// <summary>
/// 获得一个节点
/// </summary>
/// <param name="key">索引Key</param>
/// <param name="obj">节点的值</param>
/// <returns>返回一个节点</returns>
private Node getNode(double Key,object obj)
{
Node newNode = new Node();
newNode.Value = obj;
newNode.Key = Key;
_count++;
return newNode;
}
/// <summary>
/// 节点索引
/// </summary>
public object this[int index]
{
get
{
Reset();
for(int i = 0;i <= index;i++)
currentNode = currentNode.nextNode;
Node getvalue = currentNode;
Reset();
return getvalue.Value;
}
set
{
Reset();
for(int i = 0;i <= index;i++)
currentNode = currentNode.nextNode;
if(!currentNode.Value.Equals(value))
currentNode.Value = value;
Reset();
}
}
/// <summary>
/// 一次重载节点索引
/// </summary>
public object this[double Key]
{
get
{
Reset();
while(currentNode.Key != Key)
currentNode = currentNode.nextNode;
Node getvalue = currentNode;
Reset();
return getvalue.Value;
}
set
{
Reset();
while(currentNode.Key != Key)
currentNode = currentNode.nextNode;
if(!currentNode.Value.Equals(value))
currentNode.Value = value;
Reset();
}
}
/// <summary>
/// 增加一个节点
/// </summary>
/// <param name="key">索引Key</param>
/// <param name="obj">节点的值</param>
public void Add(double Key,object obj)
{
Node newNode = getNode(Key,obj);
if(firstNode == null)
{
firstNode = lastNode = newNode;
Reset();
}
else
{
lastNode.nextNode = newNode;
lastNode = newNode;
}
}
/// <summary>
/// 移除一个节点
/// </summary>
/// <param name="obj">要移除的节点的值</param>
public void removeNode(object obj)
{
Reset();
while(MoveNext())
{
if(currentNode.Value.Equals(obj))
{
delete();
Reset();
return;
}
}
}
/// <summary>
/// 移除一个节点
/// </summary>
/// <param name="Key">要移除的节点的Key索引</param>
public void removeNodeKey(double Key)
{
Reset();
while(MoveNext())
{
if(currentNode.Key == Key)
{
delete();
Reset();
return;
}
}
}
/// <summary>
/// 返回节点总数
/// </summary>
public int Count
{
get
{
return _count;
}
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("开始创建链表,请先输入链表长度值");
double q;
q=Double.Parse(Console.ReadLine());
ListNode ln = new ListNode();
for(int x=1;x<=q;x++)
{
Console.WriteLine("请输入第{0}个值",x);
string aa;
aa=Console.ReadLine();
ln.Add(x,aa);
}
Console.WriteLine("现在遍历链表");
foreach(Node no in ln)
{
Console.Write(no.Value + " ");
}
Console.WriteLine("现在删除链表");
ln.deleteall();
// TODO: 在此处添加代码以启动应用程序
//
}
#region IEnumerable 成员
public IEnumerator GetEnumerator()
{
// TODO: 添加 ListNode.GetEnumerator 实现
return this;
}
#endregion
#region IEnumerator 成员
public void Reset()
{
currentNode = new Node();
currentNode.nextNode = firstNode;
// TODO: 添加 ListNode.Reset 实现
}
public object Current
{
get
{
// TODO: 添加 ListNode.Current getter 实现
return currentNode;
}
}
public bool MoveNext()
{
currentNode = currentNode.nextNode;
if(currentNode == lastNode.nextNode)
{
Reset();
return false;
}
else
return true;
// TODO: 添加 ListNode.MoveNext 实现
}
#endregion
}
class Node
{
public Node nextNode;
double _key;
object _value;
/// <summary>
/// 设置或返回节点的值
/// </summary>
public object Value
{
get
{
return _value;
}
set
{
if(!value.Equals(_value))
_value = value;
}
}
/// <summary>
/// 置或返回索引值
/// </summary>
public double Key
{
get
{
return _key;
}
set
{
if(_key != value)
_key = value;
}
}
}
}