0

我真的很难修复我的代码,想知道是否有人可以帮助我。

基本上我得到以下错误:

找不到类型或命名空间名称“T”(您是否缺少 using 指令或程序集引用?)

以下是我的课程:

节目类:

using System;
using System.Collections.Generic;
using System.Text;

namespace LinkedListGen

{
    class program
    {
        public static void Main(string[] args)
        {
            LinkListGen<T> testList = new LinkListGen<T>();
            Console.ReadKey();
        }
    }
}

LinkGen 类:

using System;
using System.Collections.Generic;
using System.Text;

namespace LinkedListGen
{
class LinkGen<T>
{
    private T data;
    private LinkGen<T> next;

    public LinkGen(T item)
    {
        data = item;
        next = null;
    }

    public LinkGen(T item, LinkGen<T> list)
    {
        data = item;
        next = list;
    }

    public LinkGen<T> TailList
    {
        set { this.next = value; }
        get { return this.next; }
    }

    public T HeadList
    {
            set { this.data = value; }
            get { return this.data; }
        }

    }
}

LinkListGen 类:

using System;
using System.Collections.Generic;
using System.Text;

namespace LinkedListGen
{
public class LinkListGen<T> where T : IComparable
{
    private LinkGen<T> list;

    public LinkListGen() //initialise list to be empty
    {
        list = null;
    }

    public void AddItem(T item)
    {
        list = new LinkGen<T>(item, list);
    }
    public string DisplayList() //write items to string
    {
        LinkGen<T> temp = list;
        string buffer = "";
        while (temp != null)
        {
            Console.WriteLine(temp.HeadList);
            temp = temp.TailList;
        }
        return buffer;
    }
    public int NumberOfItems()
    {
        LinkGen<T> temp = list;
        int count;
        count = 0;
        while (temp != null)
        {
            count++;
            temp = temp.TailList;
        }
        Console.Out.WriteLine("There are " + count + "items recorded.");
        return count;
    }

    public bool IsPresentItem(T item)
    {
        bool txf;
        LinkGen<T> temp = list;
        int count;
        count = 0;
        while (temp != null)
        {
            if (item.Equals(temp.HeadList))
            {
                count++;
            }
            temp = temp.TailList;
        }
        if (count > 0)
        {
            Console.Out.WriteLine("There are " + count + " instances of " + item + ".");
            txf = true;
        }
        else
        {
            Console.Out.WriteLine("There are no instances of " + item + ".");
            txf = false;
        }
        return txf;

    }

    public void RemoveItem(T item)
    {
        LinkGen<T> prev = list;
        LinkGen<T> curr = list;
        if (item.Equals(curr.HeadList))
            list = curr.TailList;
        else
        {
            while (curr != null)
            {
                if (item.Equals(curr.HeadList))
                {
                    prev.TailList = curr.TailList;
                }
                else
                {
                    prev = curr;
                    curr = curr.TailList;
                }
            }
        }
    }
}
}

目的是创建一个通用链表

我真的束手无策,将不胜感激提供的任何帮助。

4

2 回答 2

10

您必须在此处指定具体类型,而不是类型占位符T

    public static void Main(string[] args) 
    { 
        LinkListGen<T> testList = new LinkListGen<T>(); 
                    ^                             ^
        Console.ReadKey(); 
    } 

例如:

        LinkListGen<string> testList = new LinkListGen<string>(); 
于 2010-03-06T02:53:08.157 回答
6
LinkListGen<T> testList = new LinkListGen<T>();

您应该将此处的“T”替换为您尝试用于通用列表的类型。

于 2010-03-06T02:53:24.210 回答