那位高手帮我看看怎么实现

漫天雪飞 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(){}未将对象引用设置到对象的实例。

该怎么解决?
...全文
153 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
漫天雪飞 2011-05-19
  • 打赏
  • 举报
回复


漫天雪飞 2011-05-19
  • 打赏
  • 举报
回复


using System;

[/code]
漫天雪飞 2011-04-06
  • 打赏
  • 举报
回复
我在学习数据结构 !!!!谢谢
漫天雪飞 2011-04-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ccsldya 的回复:]

除了一楼的办法,还可以把 LinkList构造函数改改,否则new linklist时,head为空,head.next就越界

public LinkList()
{
head = new Node<T>();
}
[/Quote]

我在使用了你的方法后,又出现了新的问题,在执行下面的代码时,明显不是正确的结果
LinkList<int> L = new LinkList<int>();
Console.WriteLine(L.IsEmpty());
在此时,应输出为 True, 但是输出的是false;。。。。
wt_sanlian 2011-04-03
  • 打赏
  • 举报
回复
.net 中已经有了链表的的类了 LinkedList<T>
不用再自已实现了吧
技术小新 2011-04-02
  • 打赏
  • 举报
回复
我到是老在页面和后台的传值之间遇到这种问题:应该是你取来赋给变量的值是页面不存在的....(参考)
zhushoudong 2011-04-02
  • 打赏
  • 举报
回复
断点调试一下看看 提示什么啊,这么长的代码 晕了
ccsldya 2011-04-02
  • 打赏
  • 举报
回复
除了一楼的办法,还可以把 LinkList构造函数改改,否则new linklist时,head为空,head.next就越界

public LinkList()
{
head = new Node<T>();
}
ccsldya 2011-04-02
  • 打赏
  • 举报
回复
好强大的教材!
FengYuanMSFT 2011-04-02
  • 打赏
  • 举报
回复
public int GetLength()
{
int len = 0;
Node<T> p = head;
while(p != null)
{
p = p.Next;
++len;
}
return len;
}

不会用 Debugger 啊?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧