这是一个数据结构中单链表的实现,代码如下:
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(){}未将对象引用设置到对象的实例。该怎么解决?
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(){}未将对象引用设置到对象的实例。该怎么解决?
解决方案 »
- 求CSDN的各位大神帮我看看
- if you take OO to the extreme -- which is, of course, completely ridiculous.
- 如何判断两个textbox同时为空?
- 如何实现点击datagridview的某个单元格时,高亮全选单元格中的内容
- 关于使用IrisSkin更换皮肤的问题
- 水晶报表报表可根据纸张大小自动缩放
- C#操作Exchange的问题,菜鸟勿入。
- DataGridView 问题
- 分页程序总是出错,帮我看看吧!
- 如何设置datagrid中某一行的背景颜色和字体颜色?
- c# 427112B5 十六进制 转化为 float ? 求解!
- 全排序算法,各位闲时写下,写者有分
{
int len = 0;
Node<T> p = head;
while(p != null)
{
p = p.Next;
++len;
}
return len;
}不会用 Debugger 啊?
{
head = new Node<T>();
}
我在使用了你的方法后,又出现了新的问题,在执行下面的代码时,明显不是正确的结果
LinkList<int> L = new LinkList<int>();
Console.WriteLine(L.IsEmpty());
在此时,应输出为 True, 但是输出的是false;。
不用再自已实现了吧