I'm trying to write a simple generic linked-list, for some reason I can't get the iterators to work for me.
Here's my code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class GenericList<T> : IEnumerable<T>
{
private class Node
{
// Constructor
public Node(T t)
{
data = t;
next = null;
}
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
}
private Node head;
public GenericList()
{
head = null;
}
public void push_front(T t)
{
if (head != null)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
else
{
head = new Node(t);
}
}
/* This is where I'm having trouble:
*/
public IEnumerator<T> GetEnumerator()
{
Node curr = head;
while (curr != null)
{
yield return curr.Data;
curr = curr.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void display()
{
Node curr = head;
while (curr != null)
{
Console.WriteLine(curr.Data);
curr = curr.Next;
}
}
}
class Program
{
static void Main(string[] args)
{
GenericList<int> mList = new GenericList<int>();
for( int i = 0; i < 10; i++ )
{
mList.push_front(i);
}
foreach (T currData in mList)
{
Console.WriteLine(currData, "\n");
}
}
}
}
Here's my error message from VS:
Code:
Error 2 Using the generic type 'System.Collections.Generic.IEnumerator<T>' requires '1' type arguments C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs 68 9 ConsoleApplication1
Thanks in advance
EDIT: I caught one silly thing:
Code:
foreach( T currData in mList )
// Changed it to:
foreach( int currData in mList )
but i still get the same error message.