Grisson's .net

源码之前,了无秘密

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
using System;

namespace LinkedListLibrary

{

     
//定义节点

     
class ListNode

     
{

          
private object data;        //数据

          
private ListNode next;      //指向下一个节点

          
public ListNode(object dataValue)

               :
this(dataValue,null)

          
{

          
//初始化

          }


          
public ListNode(object dataValue,ListNode nextNode)

          
{

               data 
= dataValue;

               next 
= nextNode;

          }


          
#region ListNode属性

          
public object Data

          
{

               
get

               
{

                    
return data;

               }



               
set

               
{

                    data 
= value;

               }
 

          }


          
public ListNode Next

          
{

               
get

               
{

                    
return next;

               }


               
set  

               
{

                    next 
= value;

               }


          }


          
#endregion


     }


     
public class List

     
{

          
private ListNode firstNode;

          
private ListNode lastNode;

          
private string name;//链表的名字

          

          
public List():this("list")

          
{

          }


          
public List(string listName)

          
{

               name 
= listName;

               firstNode 
= lastNode = null;

          }


          
//在链表头插入

          
public void InsertAtFront(object insertItem)

          
{

               
lock(this)

               
{

                    
if(IsEmpty())

                         firstNode 
= lastNode = new ListNode(insertItem);

                    
else

                         firstNode 
= new ListNode(insertItem,firstNode);

               }


          }


          
//在表尾插入

          
public void InsertAtBack(object insertItem)

          
{

               
lock(this)

               
{

                    
if(IsEmpty())

                         firstNode 
= lastNode = new ListNode(insertItem);

                    
else

                         lastNode 
= lastNode.Next = new ListNode(insertItem);

               }


          }


          
//移出表头数据

          
public object RemoveFromFront()

          
{

               
lock(this)

               
{

                    
if(IsEmpty())

                         
throw new EmptyListException(name);

                    
object removeItem = firstNode.Data;//重新得到数据

                    
if(firstNode == lastNode)

                    
{

                         firstNode 
= lastNode = null;

                    }


                    
else

                         firstNode 
= firstNode.Next;

                         
return removeItem;

               }


          }


          
//移出表尾数据

          
public object RemoveFromBack()

          
{

               
lock(this)

               
{

                    
if(IsEmpty())

                         
throw new EmptyListException(name);

                    
object removeItem = lastNode.Data;

                    
if(firstNode == lastNode)

                    
{

                         firstNode 
= lastNode = null;

                    }


                    
else

                    
{

                         ListNode current 
= firstNode;

                         
while(current.Next != lastNode)

                              current 
= current.Next;//移动到下一节点

                         lastNode 
= current;

                         current.Next 
= null;

                    }


                    
return removeItem;

               }


          }


          
//判断是不是为空

          
public bool IsEmpty()

          
{

               
lock(this)

                    
return firstNode == null;

          }


          
//显示链表

          
virtual public void Print()

          
{

               
lock(this)

               
{

                    
if(IsEmpty())

                    
{

                         Console.WriteLine(
"Empty "+name);

                         
return;

                    }


                    Console.Write(
"the "+name+" is:");

                    ListNode current 
= firstNode;

                    
while(current != null)

                    
{

                         Console.Write(current.Data 
+" ");

                         current 
= current.Next;

                    }


                    Console.Write(
"\n");

               }


          }


     }


     
//自定义异常

     
public class EmptyListException:ApplicationException

     
{

          
public EmptyListException(string name)

               :
base("the "+name+" is Empty")

          
{

          }


     }


}

这里只是实现了链表的基本基本操作,下面给出调用函数:


using System;

using LinkedListLibrary;

namespace ListText

{

     
public class ListText

     
{

          
public static void Main(string []args)

          
{

               List list 
= new List();

               
bool aBoolean = true;

               
char aCharacter = '$';

               
int anInteger = 34567;

               
string aString = "hello";

               
//利用List的方法

               list.InsertAtFront(aBoolean);

               list.Print();

               list.InsertAtFront(aCharacter);

               list.Print();

               list.InsertAtBack(anInteger);

               list.Print();

               list.InsertAtBack(aString);

               list.Print();

               
//使用移出方法

               
object removedObject;

               
try

               
{

                    removedObject 
= list.RemoveFromFront();

                    Console.WriteLine(removedObject
+" removed");

                    list.Print();

                    removedObject 
= list.RemoveFromFront();

                    Console.WriteLine(removedObject
+" removed");

                    list.Print();

                    removedObject 
= list.RemoveFromBack();

                    Console.WriteLine(removedObject
+" removed");

                    list.Print();

                    removedObject 
= list.RemoveFromBack();

                    Console.WriteLine(removedObject
+" removed");

                    list.Print();

               }


               
catch(EmptyListException emptyListException)

               
{

                    Console.Error.WriteLine(
"\n"+emptyListException);

               }


          }
         

     }


}


通过这个类来调用链表。
结果:

the list is:True

the list is:$ True

the list is:$ True 34567

the list is:$ True 34567 hello

$ removed

the list is:True 34567 hello

True removed

the list is:34567 hello

hello removed

the list is:34567

34567 removed

Empty list

posted on 2005-08-30 18:39  海盗  阅读(346)  评论(0编辑  收藏  举报