在创建单链表时,使用
Node<T> temp = head;
for (int i = 0; i < index; i++){
temp = temp.Next;
}时,
其中的index值应该就是temp的index,但实际运行测代码时,temp的index为index-1,导致报错,老师视频中就是对的,以下涉及index的方法都有返回null的异常,有的把index-1就正常了(但temp的索引的确是index,我理解单链表错了吗),有的不行,不知道是哪里出问题了
以下源码:
接口:
interface IListDS<T>
{
int GetLength();
void Clear();
bool IsEmpty();
void Add(T item);
void Insert(T item, int index);
T Delete(int index);
T this[int index] { get; }
T GetEle(int index);
int Locate(T value);
}
Node类:
class Node<T>
{
private T date;
private Node<T> next;
public Node()
{
date = default(T);
}
public Node(T value)
{
this.date = value;
this.next = null;
}
public Node(Node<T> next)
{
this.next = next;
}
public Node(T value, Node<T> next)
{
this.date = value;
this.next = next;
}
public T Date
{
get { return date; }
set { date = value; }
}
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
单链表:
class LinkList<T> : IListDS<T>
{
private Node<T> head;
public LinkList()
{
head = null;
}
public T this[int index]
{
get
{
T date = default(T);
Node<T> temp = head;
for (int i = 0; i <= index; i++)
{
temp = temp.Next;
}
date = temp.Date;
return date;
}
}
public void Add(T item)
{
Node<T> newNode = new Node<T>(item);
if (head == null)
{
head = newNode;
}
else
{
Node<T> temp = head;
while (true)
{
if (temp.Next != null)
{
temp = temp.Next;
}
else
{
break;
}
}
temp.Next = newNode;
}
}
public void Clear()
{
head = null;
}
public T Delete(int index)
{
T date = default(T);
if (index == 0)
{
date = head.Date;
head = head.Next;
}
else
{
Node<T> temp = head;
for (int i = 0; i < index; i++)
{
temp = temp.Next;
}
Node<T> preNode = temp;
Node<T> currentNode = temp.Next;
date = currentNode.Date;
Node<T> nextNode = temp.Next.Next;
if (nextNode == null)
{
preNode.Next = null;
}
else
{
preNode.Next = nextNode;
}
}
return date;
}
public T GetEle(int index)
{
return this[index];
}
public int GetLength()
{
int count = 1;
if (head == null) count = 0;
else
{
Node<T> temp = head;
while (true)
{
if (temp.Next != null)
{
count++;
temp = temp.Next;
}
else
{
break;
}
}
}
return count;
}
public void Insert(T item, int index)
{
Node<T> newNode = new Node<T>(item);
if (index == 0)
{
newNode.Next = head;
head = newNode;
}
else
{
Node<T> temp = head;
for (int i = 0; i < index; i++)
{
temp = temp.Next;
}
Node<T> preNode = temp;
Node<T> nextNode = temp.Next;
preNode.Next = newNode;
newNode.Next = nextNode;
}
}
public bool IsEmpty()
{
return head == null;
}
public int Locate(T value)
{
int index = 0;
if (head == null)
{
index = -1;
}
else
{
Node<T> temp = head;
for (int i = 0; i < GetLength(); i++)
{
temp = temp.Next;
if (temp.Date.Equals(value))
{
index = i;
}
}
}
return index;
}
}
测试码:
class Program
{
static void Main(string[] args)
{
LinkList<int> list = new LinkList<int>();
list.Add(12);
list.Add(13);
list.Add(14);
Console.WriteLine(list[1]);
Console.WriteLine(list.GetEle(1));
list.Delete(2);
list.Insert(15, 2);
Console.WriteLine(list.GetLength());
Console.WriteLine(list.IsEmpty());
Console.WriteLine(list.Locate(15));
for (int i = 0; i < list.GetLength(); i++)
{
Console.Write(list[i] + " ");
}
list.Clear();
Console.WriteLine(list.GetLength());
Console.ReadKey();
}
}
同学你好,可以下载一下老师的源码看一下,同学节点的构造方法有问题,删除方法也有问题。可以拿笔画一画理解修改一下。