单链表

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值