using System;
namespace ConsoleApplication23
{
public class Node
{
private object data;
private Node next;
public object Data
{
get
{
return data;
}
set
{
data = value;
}
}
public Node Next
{
get
{
return next;
}
set
{
next = value;
}
}
public Node()
{
//
// TODO: Add constructor logic here
//
this.data = null;
this.next = null;
}
public Node( object d )
{
this.data = d;
this.next = null;
}
public Node( object d, Node n )
{
this.data = d;
this.next = n;
}
}
public class List
{
private Node first;
private Node last;
private Node current;
private int length;
public bool isEmpty = true;
public Node First
{
get
{
return first;
}
// set
// {
// first = value;
// }
}
public Node Last
{
get
{
return last;
}
// set
// {
// last = value;
// }
}
public Node Current
{
get
{
return current;
}
// set
// {
// current = value;
// }
}
public int Length
{
get
{
return length;
}
// set
// {
// length = value;
// }
}
public List()
{
//
// TODO: Add constructor logic here
//
this.first = null;
this.last = null;
this.length = 0;
}
public List( object[] oArray )
{
foreach( object o in oArray )
{
if( this.isEmpty )
{
Node temp = new Node( o );
this.first = temp;
this.last = temp;
this.current = temp;
this.isEmpty = false;
this.length++;
}
else
{
Node temp = new Node( o );
this.current.Next = temp;
this.current = temp;
this.last = temp;
this.length++;
}
}
}
public void AddNode( object o )
{
Node temp = new Node( o );
this.last.Next = temp;
this.last = temp;
this.length++;
}
public void RemoveAt( int index )
{
if( index > 0 && index <= this.length )
{
Node removedNode = this.FindNode( index );
Node preNode = this.FindNode( index - 1 );
Node nextNode = this.FindNode( index + 1 );
preNode.Next = nextNode;
this.length--;
}
}
public void InsertAt( object o, int index )
{
Node temp = new Node( o );
if( index > 0 && index <= this.length )
{
Node insertNode = this.FindNode( index );
Node preNode = this.FindNode( index - 1 );
preNode.Next = temp;
temp.Next = insertNode;
this.length++;
}
}
public Node FindNode( int index )
{
Node temp = null;
if( index < 0 )
temp = this.first;
if( index > this.length )
temp = this.last;
if( index > 0 && index <= this.length )
{
this.current = this.first;
for( int i = 0; i < index; i++ )
{
temp = this.current;
this.current = this.current.Next;
}
}
return temp;
}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string[] s = new string[]{"a","b", "c", "d","e","f","g","h","i","j","k"};
List list = new List( s );
list.RemoveAt( 2 );
list.InsertAt( "a", 2 );
for( int i = 1; i <= list.Length; i++ )
Console.Write( " " + list.FindNode( i ).Data );
Console.ReadLine();
}
}
}