链表类(实现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中链表逆序的指针的讲解,希望对你有帮助。
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中链表逆序的指针的讲解,希望对你有帮助。
相关文章推荐
- (转)java实现基本数据结构(堆,栈,链表)——推荐后面的链表扩展部分
- 有序链表的部分实现
- java实现链表指定值得删除,查找第二部分代码
- 用链表实现部分图书信息管理功能
- [C语言]链表实现贪吃蛇及部分模块优化
- C++实现打印两个有序链表公共部分的方法
- 数据结构中链表的C#实现部分示例
- C 单向链表的实现及部分功能
- c#自定义简单链表通过IEnumerable接口实现内部迭代
- java双向链表的实现(部分功能)
- 数据结构与算法学习笔记——链表部分实现(数组形式)
- C语言实现单链表的基本操作及其部分面试题
- python实现反转部分单向链表
- C语言实现单链表的基本操作及其部分面试题
- C++: 实现双向链表(例题讲解)
- C实现 LeetCode->Rotate List(双指针大法)(单链表 部分旋转)
- 常见内排序实现汇总(含部分优化实现,基于链表的实现),以及性能比较
- c#自定义简单链表通过IEnumerable接口,范型实现内部迭代
- 左神的书——《程序员代码面试指南》之打印两个有序链表的公共部分 c++实现
- 算法与数据结构-链表 讲解与java代码实现