这又哪里错了

weikeli19 2016-09-26 08:03:57
请看这段代码 我查了很多遍了 并没有错误啊 为什么老提示未实例化 谁能帮我改改 最好改的幅度不要太大
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication2
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Globalization;
using System.Diagnostics.Contracts;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
namespace delete5
{

class Node<K, T>
{
public K Key;
public T Item;
public Node<K, T> NextNode;
public Node()
{
Key = default(K);
Item = default(T);
NextNode = null;
}
public Node(K key, T item, Node<K, T> nextNode)
{
Key = key;
Item = item;
NextNode = nextNode;
}
}

public class LinkedList<K, T>
{
Node<K, T> m_Head;
public LinkedList()
{
m_Head = new Node<K, T>();
}
public void AddHead(K key, T item)
{
Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
m_Head.NextNode = newNode;
}
public T Find(K key)
{
Node<K, T> current = m_Head;
while (current.NextNode != null)
{
if (((IComparable)(current.Key)).CompareTo(key) == 0) //Will not compile
break;
else

current = current.NextNode;
}
return current.Item;
}

}
public class A : IComparable
{
private int myprop;

public A(int i)
{
myprop = i;

}
public int CompareTo(object obj)
{


if ((A)this == ((A)obj))
{
return 0;
}
else return -1;

}
}
class Program
{

static void Main(string[] args)
{
LinkedList<A, int> a = new LinkedList<A, int>();
A a1 = new A(1);
A a2 = new A(2);
A a3 = new A(3);
a.AddHead(a1, 11);
a.AddHead(a2, 22);
a.AddHead(a3, 33);
A a4 = new A(4);
int i = a.Find(a2);
Console.WriteLine(i);


}
}
}

}
...全文
180 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
weikeli19 2016-09-26
  • 打赏
  • 举报
回复
引用 4 楼 johnliuyuan 的回复:
你的问题在于你的链表的第一个元素是空的,没有赋值,所以会出现空引用的情况
哦 我知道了~~~~~~~~~~~~~~~~~~~~~~谢谢了哈
weikeli19 2016-09-26
  • 打赏
  • 举报
回复
引用 4 楼 johnliuyuan 的回复:
你的问题在于你的链表的第一个元素是空的,没有赋值,所以会出现空引用的情况
我搞不懂了 怎么会第一个元素是空的呢?怎么会没赋值呢?请指教 麻烦了
john_QQ:2335298917 2016-09-26
  • 打赏
  • 举报
回复
你的问题在于你的链表的第一个元素是空的,没有赋值,所以会出现空引用的情况
john_QQ:2335298917 2016-09-26
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LinkedList<A, int> a = new LinkedList<A, int>(new A(0),2);
            A a1 = new A(1);
            A a2 = new A(2);
            A a3 = new A(3);
            a.AddHead(a1, 11);
            a.AddHead(a2, 22);
            a.AddHead(a3, 33);
            A a4 = new A(4);
            int i = a.Find(a2);
            MessageBox.Show(i.ToString());
            
        }
    }
    class Node<K, T>
    {
        public K Key;
        public T Item;
        public Node<K, T> NextNode;
        public Node()
        {
            Key = default(K);
            Item = default(T);
            NextNode = null;
        }
        public Node(K key, T item, Node<K, T> nextNode)
        {
            Key = key;
            Item = item;
            NextNode = nextNode;
        }
    }

    public class LinkedList<K, T>
    {
        Node<K, T> m_Head;
        public LinkedList(K key,T item)
        {
            m_Head = new Node<K, T>(key,item,null);
        }
        public void AddHead(K key, T item)
        {
            Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
            m_Head.NextNode = newNode;
        }
        public T Find(K key)
        {
            Node<K, T> current = m_Head;
            while (current.NextNode != null)
            {
                if (((IComparable)(current.Key)).CompareTo(key) == 0) 
                    break;
                else

                    current = current.NextNode;
            }
            return current.Item;
        }

    }
    public class A : IComparable
    {
        private int myprop;

        public A(int i)
        {
            myprop = i;

        }
        public int CompareTo(object obj)
        {


            if ((A)this == ((A)obj))
            {
                return 0;
            }
            else return -1;

        }
    }
}
john_QQ:2335298917 2016-09-26
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LinkedList<A, int> a = new LinkedList<A, int>(new A(0),2);
            A a1 = new A(1);
            A a2 = new A(2);
            A a3 = new A(3);
            a.AddHead(a1, 11);
            a.AddHead(a2, 22);
            a.AddHead(a3, 33);
            A a4 = new A(4);
            int i = a.Find(a2);
            MessageBox.Show(i.ToString());
            
        }
    }
    class Node<K, T>
    {
        public K Key;
        public T Item;
        public Node<K, T> NextNode;
        public Node()
        {
            Key = default(K);
            Item = default(T);
            NextNode = null;
        }
        public Node(K key, T item, Node<K, T> nextNode)
        {
            Key = key;
            Item = item;
            NextNode = nextNode;
        }
    }

    public class LinkedList<K, T>
    {
        Node<K, T> m_Head;
        public LinkedList(K key,T item)
        {
            m_Head = new Node<K, T>(key,item,null);
        }
        public void AddHead(K key, T item)
        {
            Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
            m_Head.NextNode = newNode;
        }
        public T Find(K key)
        {
            Node<K, T> current = m_Head;
            while (current.NextNode != null)
            {
                if (((IComparable)(current.Key)).CompareTo(key) == 0) 
                    break;
                else

                    current = current.NextNode;
            }
            return current.Item;
        }

    }
    public class A : IComparable
    {
        private int myprop;

        public A(int i)
        {
            myprop = i;

        }
        public int CompareTo(object obj)
        {


            if ((A)this == ((A)obj))
            {
                return 0;
            }
            else return -1;

        }
    }
}
weikeli19 2016-09-26
  • 打赏
  • 举报
回复
请说明原因。。

111,129

社区成员

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

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

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