I am rereading some chapters in “Data Structures & Problem Solving Using Java”. I just finished reading chapter 17 on linked lists. Here is the singly linked list and sorted linked list from this chapter done in c#.

``````using System;
using System.Collections.Generic;

{
public class ListNode<T>
{
private T elem;
private ListNode<T> nextNode;

public T Element
{
get {return elem;}
set {elem = value;}
}

public ListNode<T> Next
{
get {return nextNode;}
set {nextNode = value;}
}

public ListNode(T theElement) :
this(theElement, null)
{
}

public ListNode(T theElement, ListNode<T> n)
{
elem = theElement; nextNode = n;
}

}

{
private ListNode <T> cur;

{
cur = theNode;
}

public ListNode<T> Current
{
get{return cur;}
set{cur = value;}
}

public bool IsValid()
{
return cur != null;
}

public T Retrieve()
{
if ( IsValid() )
{
return cur.Element;
}
else
{
return default(T); //null;
}
}

{
if (IsValid())
{
cur	= cur.Next;
}
}
}

{

/// <summary>
/// Construct the List.
/// </summary>
{
this.header = new ListNode<T> (default(T)); //(null);
}

public bool IsEmpty()
{
}

public void MakeEmpty()
{
}

/// <summary>
/// Return an iterator representing the header node.
/// </summary>
/// <returns>
/// </returns>
{
}

/// <summary>
/// Return an iterator representing the first node in the list.
/// </summary>
/// <returns>
/// </returns>
{
}

{

while(itr != null && !itr.Element.Equals(x))
{
itr = itr.Next;
}

}

/// <summary>
/// Remove the first occurrence of an item.  The code is not foolproof.
/// There may be two iterators, and one can left dangling if the other
/// removes a node.
/// </summary>
/// <param name="x">
/// A <see cref="T"/>
/// </param>
public void Remove(T x)
{
if(p.Current.Next != null)
{
p.Current.Next = p.Current.Next.Next;
}
}

/// <summary>
/// Return iterator prior to the first node containing an item.
/// </summary>
/// <param name="x">
/// A <see cref="T"/>
/// </param>
/// <returns>
/// </returns>
{

while (itr.Next != null && !itr.Next.Element.Equals(x))
{
itr = itr.Next;
}
}

public void Insert(T x, LinkedListIterator<T> p)
{
if(p != null && p.Current != null)
{
p.Current.Next = new ListNode<T>(x, p.Current.Next);
}
}

{
if (theList.IsEmpty())
{
System.Console.WriteLine("Empty List");
}
else
{
{
System.Console.WriteLine(itr.Retrieve());
}
}

System.Console.WriteLine("");
}
}

{
public new void Insert(T x, LinkedListIterator<T> p)
{
Insert(x);
}

public void Insert(T x)
{

//while(curr.IsValid() && ( x.CompareTo(curr.Retrieve() ) > 0) )
while(curr.IsValid() && (!EqualityComparer<T>.Default.Equals(x, curr.Retrieve())) )
{
}

base.Insert(x, prev);
}
}

class MainClass
{
public static void Main(string[] args)
{

int i;

theItr = theList.Zeroth( );
theList.PrintList(theList);

for( i = 0; i < 10; i++ )
{
theList.Insert( i , theItr );
theList.PrintList( theList );
}

for( i = 0; i < 10; i += 2 )
{
theList.Remove( i );
}

for( i = 0; i < 10; i++ )
{
if( ( i % 2 == 0 ) == ( theList.Find( i ).IsValid( ) ) )
{
Console.WriteLine( "Find fails!" );
}
}

Console.WriteLine( "Finished deletions" );
theList.PrintList( theList );
}
}

}

``````