views:

461

answers:

2

Im really having trouble fixing my code, was wondering if there was anyone out there who could help me.

Basically im getting the following error:

The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

The following are my classes:

Program Class:

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 Class:

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 Class:

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;
                }
            }
        }
    }
}
}

The aim is to create a generic linked list

Im really at my wits end and would appreciate any help offered.

+8  A: 

You have to specify a concrete type here, rather than the type placeholder, T:

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

For example:

        LinkListGen<string> testList = new LinkListGen<string>(); 
Mitch Wheat
Ahh right that is just a placeholder then. I thought I was doing it wrong when I first tried replacing it with a type, silly me. Thank you very much.
+6  A: 
LinkListGen<T> testList = new LinkListGen<T>();

You should replace 'T' here with the type you're trying to use for your generic list.

Scorpion