那位高手帮我看看怎么实现
漫天雪飞 2011-04-02 03:51:46 这是一个数据结构中单链表的实现,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
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 );
}
public class Node<T>
{
private T data;
private Node<T> next;
public Node(T val,Node<T> p)
{
this.data = val;
this.next = p;
}
public Node( Node<T> p )
{
this.next = p;
}
public Node( T val )
{
this.data = val;
this.next = null;
}
public Node()
{
this.data = default(T);
this.next = null;
}
public T Data
{
get
{
return data;
}
set
{
data = value;
}
}
public Node<T> Next
{
get
{
return this.next;
}
set
{
this.next = value;
}
}
}
public class LinkList<T>:IListDS<T>
{
private Node<T> head;
public Node<T> Head
{
get
{
return this.head;
}
set
{
this.head = value;
}
}
public LinkList()
{
head = null;
}
public int GetLength()
{
int len = 0;
Node<T> p = head;
while(p.Next != null)
{
p = p.Next;
++len;
}
return len;
}
public void Clear()
{
head = null;
}
public bool IsEmpty()
{
if(head == null)
{
return true;
}
else
{
return false;
}
}
public void Append( T item )
{
Node<T> q = new Node<T>(item);
Node<T> p = new Node<T>();
if(head == null)
{
head = q;
return;
}
p = head;
while(p.Next != null)
{
p = p.Next;
}
p.Next = q;
}
public void Insert( T item, int i )
{
if(IsEmpty() || i < 1)
{
Console.WriteLine("The list is empty or position is error");
return;
}
if(i == 1)
{
Node<T> q = new Node<T>(item);
q.Next = head;
head = q;
return;
}
int j = 1;
Node<T> p = head;
Node<T> r = new Node<T>();
while(p.Next != null && j < i)
{
r = p;
p = p.Next;
++j;
}
if(j == i)
{
Node<T> q = new Node<T>(item);
q.Next = p;
r.Next = q;
}
}
public void InsertPost( T item, int i )
{
if(IsEmpty() || i < 1)
{
Console.WriteLine("The list is empty or position is error");
return;
}
if(i == 1)
{
Node<T> q = new Node<T>(item);
q.Next = head.Next;
head.Next = q;
return;
}
Node<T> p = head;
int j = 1;
while(p.Next != null && j < i)
{
p = p.Next;
++j;
}
if(j == i)
{
Node<T> q = new Node<T>(item);
q.Next = p.Next;
p.Next = q;
}
}
public T Delete( int i )
{
if(IsEmpty() || i < 1)
{
Console.WriteLine("The list is empty or position is error");
return default(T);
}
Node<T> q = new Node<T>();
if(i == 1)
{
q = head;
head.Next = head;
return q.Data;
}
Node<T> p = head;
int j = 1;
while(p.Next != null && j < i)
{
q = p;
p = p.Next;
++j;
}
if(j == i)
{
q.Next = p.Next;
return p.Data;
}
else
{
Console.WriteLine("The ith node is not exist");
return default(T);
}
}
public T GetElem(int i)
{
if(IsEmpty() || i < 1)
{
Console.WriteLine("List is empty or position is error");
return default(T);
}
Node<T> p = new Node<T>();
p = head;
int j = 1;
while(p.Next != null && j < i)
{
p = p.Next;
++j;
}
if(j == i)
{
return p.Data;
}
else
{
Console.WriteLine("The ith is not exist");
return default(T);
}
}
public int Locate( T value )
{
if(IsEmpty())
{
Console.WriteLine("List is empty");
return -1;
}
Node<T> p = new Node<T>();
p = head;
int i = 1;
while(p.Next != null && !p.Data.Equals(value))
{
p = p.Next;
++i;
}
return i;
}
public void Reverse()
{
Node<T> p = head.Next;
Node<T> q = new Node<T>();
head.Next = null;
while(p != null)
{
q = p;
p = p.Next;
q.Next = head.Next;
head.Next = q;
}
}
}
public class Program
{
static void Main()
{
LinkList<int> L = new LinkList<int>();
Console.WriteLine(L.IsEmpty());
Console.WriteLine(L.GetLength());
L.Append(1);
L.Insert(2 , 1);
Console.WriteLine(L.IsEmpty());
Console.WriteLine(L.GetLength());
Console.Read();
}
}
}
我在编译运行后,提示编译错误,public int GetLength(){}未将对象引用设置到对象的实例。
该怎么解决?