您的位置:首页 > 其它

链表类(实现IEnumerable)【附部分讲解】

2009-12-09 21:52 204 查看
代码没有什么难度的。要注意的地方:

1.在处理的节点的反转时,注意是谁指向谁。在纸上画清楚不易搞反顺序。

2.实现接口IEnumerable<T>时,应该实现哪些函数。GetEnumerator



全部代码:

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

namespace MyList

{

public interface IListDS<T>

{

int GetLength(); //求长度

void Clear(); //清空操作

bool IsEmpty(); //判断是否为空

void Append(T item); //附加操作

void Insert(T item, int i);//插入操作

T delete(int i);//删除操作

T GetElem(int i);//取表元

int Locate(T value);//按值查找

void Reverse();//倒置链表

}

public class Node<T>

{

private T data; //数据域

private Node<T> next;//引用域

//构造器

public Node(T val, Node<T> p)

{

data = val;

next = p;

}

//构造器

public Node(Node<T> p)

{

data = default(T);

next = p;

}

//构造器

public Node(T val)

{

data = val;

next = null;

}

//构造器

public Node()

{

data = default(T);

next = null;

}

//数据域属性

public T Data

{

get

{

return data;

}

set

{

data = value;

}

}

//引用域属性

public Node<T> Next

{

get

{

return next;

}

set

{

next = value;

}

}

};

public class LinkList<T> : IListDS<T>, IEnumerable<T>

{

private Node<T> head;

//头引用属性

public Node<T> Head

{

get

{

return head;

}

set

{

head = value;

}

}

//构造器

public LinkList()

{

head = new Node<T>();

head.Data = default(T);

head.Next = null;

}

//长度属性

public int GetLength()

{

int len = 0;

Node<T> p = head;

while (p != null)

{

len++;

p = p.Next;

}

return len;

}

//清空操作

public void Clear()

{

head = null;

}

//判断是否为空

public bool IsEmpty()

{

if (head != null)

return false;

else

return true;

}

//附加操作

public void Append(T item)

{

Node<T> NewP = new Node<T>(item);

Node<T> p = head;

while (p.Next != null)

{

p = p.Next;

}

p.Next = NewP;

}

//插入操作

public void Insert(T item, int i)

{

if (i < 0)

throw new Exception("The position Which was wanted to insert can not less than 0");

if (i > GetLength())

throw new Exception("The position Which was wanted to insert IS lager than the length");

Node<T> NewP = new Node<T>(item);

Node<T> p1 = head.Next;

Node<T> p0 = head;//p0是p1靠近head的节点

int len = i;

while (len > 0)

{

len--;

p0 = p0.Next;

p1 = p1.Next;

}

p0.Next = NewP;

NewP.Next = p1;

}

//删除操作

public T delete(int i)

{

if (i < 0 || i > GetLength())

throw new Exception("out of te range");

Node<T> p = new Node<T>();

Node<T> p1 = new Node<T>();

p = head.Next;

p1 = head;

while (i > 0)

{

p = p.Next;

p1 = p1.Next;

i--;

}

p1.Next = p.Next;

return p.Data;

}

//取表元

public T GetElem(int i)

{

if (i < 0 || i > GetLength())

throw new Exception("out of te range");

Node<T> p = new Node<T>();

Node<T> p1 = new Node<T>();

p = head.Next;

p1 = head;

while (i > 0)

{

p = p.Next;

p1 = p1.Next;

i--;

}

return p.Data;

}

//按值查找

public int Locate(T value)

{

Node<T> p = new Node<T>();

int len = 0;

p = head.Next;

while (!p.Data.Equals(value))

{

len++;

p = p.Next;

}

return len;

}

//倒置链表

public void Reverse()

{

if (IsEmpty())

throw new Exception("The list is empty.");

if (this.GetLength() == 1) return;

Node<T> p = new Node<T>();

Node<T> p1 = new Node<T>();

p = head.Next;

p1 = p.Next;

while (p1 != null)

{

p.Next = p1.Next;

Node<T> p0 = new Node<T>();

p0 = head.Next;

if (p0 == p)

{

head.Next = p1;

p1.Next = p;

p1 = p.Next;

}

else

{

head.Next = p1;

p1.Next = p0;

p1 = p.Next;

}

}

}

public IEnumerator<T> GetEnumerator()

{

Node<T> current = head.Next;

while (current != null)

{

yield return current.Data;

current = current.Next;

}

}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

{

return GetEnumerator();

}

}

class Program

{

static void Main(string[] args)

{

LinkList<string> list = new LinkList<string>();

//Create name and age values to initialize Person objects.

string[] names = new string[]

{

"Franscoise",

"Bill",

"Li",

"Sandra",

"Gunnar",

"Alok",

"Hiroyuki",

"Maria",

"Alessandro",

"Raul"

};

//Populate the list.

for (int x = 0; x < 10; x++)

{

list.Append(names[x]);

}

Console.WriteLine(list.GetLength());

foreach (string s in list)

{

Console.WriteLine(s);

}

list.Reverse();

Console.WriteLine("--------------");

Console.WriteLine(list.GetLength());

foreach (string s in list)

{

Console.WriteLine(s);

}

Console.WriteLine("--------------");

Console.WriteLine(list.GetElem(2));

Console.WriteLine("--------------");

list.Insert("Aaron", 0);

Console.WriteLine(list.GetLength());

foreach (string s in list)

{

Console.WriteLine(s);

}

Console.WriteLine("--------------");

Console.WriteLine(list.Locate("Aaron"));

Console.WriteLine("--------------");

list.delete(0);

Console.WriteLine(list.GetLength());

foreach (string s in list)

{

Console.WriteLine(s);

}

Console.WriteLine("--------------");

Console.WriteLine(list.IsEmpty());

list.Clear();

Console.WriteLine(list.IsEmpty());

}

}

}

PS:我的vs无法调试,但是一开始的Reverse函数的结果是错的,又人肉调试了一把,╮(╯▽╰)╭



这里
有关于该blog中链表逆序的指针的讲解,希望对你有帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: