數據結構與算法---C#實現LinkedList實例


  這里創建一個單向鏈表,通過三個類來實現單向鏈表的基本操作:創建,新增(指定節點前,指定節點后),刪除,判斷是否為空....

  下面分別實現這三個類以及測試代碼

  LinkedListNode:鏈表的節點類 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CADataStructureTest.LinkedList
{
    public class LinkedListNode
    {
        public object Data { get; private set; }

        public LinkedListNode Next { get; set; }

        public LinkedListNode( object dataValue)
            : this(dataValue, null)
        {
        }

        public LinkedListNode( object dataValue, LinkedListNode nextNode)
        {
            Data = dataValue;
            Next = nextNode;
        }
    }
}

 

  LinkedList:鏈表類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CADataStructureTest.LinkedList
{
     public  class LinkedList
    {
         private LinkedListNode firstNode;
         private LinkedListNode lastNode;
         private  string name;

         public LinkedList( string listName)
        {
            name = listName;
            firstNode = lastNode =  null;
        }
         public LinkedList()
            :  this( " list ")
        {
        }

         public  void InsertAtFront( object insertItem)
        {
             if (IsEmpty())
                firstNode = lastNode =  new LinkedListNode(insertItem);
             else
                lastNode = lastNode.Next =  new LinkedListNode(insertItem);
        }

         public  void InsertAtBack( object insertItem)
        {
             if (IsEmpty())
                firstNode = lastNode =  new LinkedListNode(insertItem);
             else
                firstNode =  new LinkedListNode(insertItem, firstNode);
        }

         public  object RemoveFromFront()
        {
             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()
        {
             if (IsEmpty())
                 throw  new EmptyListException(name);

             object removeItem = lastNode.Data;
             if (firstNode == lastNode)
                firstNode = lastNode =  null;
             else
            {
                LinkedListNode current = firstNode;

                 while (current.Next != lastNode)
                    current = current.Next;
                lastNode = current;
                current.Next =  null;
            }

             return removeItem;
        }

         public  bool IsEmpty()
        {
             return firstNode ==  null;
        }

         public  void Display()
        {
             if (IsEmpty())
            {
                Console.WriteLine( " Empty  " + name);
            }
             else
            {
                Console.Write( " The  " + name +  "  is:  ");
                LinkedListNode current = firstNode;
                 while (current !=  null)
                {
                    Console.Write(current.Data +  "   ");
                    current = current.Next;
                }
                Console.WriteLine( " \n ");
            }
        }

    }
}

  EmptyListException:鏈表操作異常處理類 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CADataStructureTest.LinkedList
{

     public  class EmptyListException : Exception
    {
         public EmptyListException()
            :  base( " The list is empty ")
        {
        }

         public EmptyListException( string name)
            :  base( " the  " + name +  "  is empty ")
        {
        }

         public EmptyListException( string exception, Exception inner)
            :  base(exception, inner)
        {
        }
    }
}

  測試代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CADataStructureTest.LinkedList;
namespace CADataStructureTest
{
     class Program
    {
         static  void Main( string[] args)
        {
            CADataStructureTest.LinkedList.LinkedList list =  new CADataStructureTest.LinkedList.LinkedList();
             bool aBoolean =  true;
             char aCharacter =  ' # ';
             int anInteger =  9258;
             string aString =  " DataStructure ";

            list.InsertAtFront(aBoolean);
            list.Display();
            list.InsertAtFront(aCharacter);
            list.Display();
            list.InsertAtBack(anInteger);
            list.Display();
            list.InsertAtBack(aString);
            list.Display();

             object removedObject;

             try
            {
                removedObject = list.RemoveFromFront();
                Console.WriteLine(removedObject +  "  removed ");
                list.Display();

                removedObject = list.RemoveFromFront();
                Console.WriteLine(removedObject +  "  removed ");
                list.Display();


                removedObject = list.RemoveFromBack();
                Console.WriteLine(removedObject +  "  removed ");
                list.Display();

                removedObject = list.RemoveFromBack();
                Console.WriteLine(removedObject +  "  removed ");
                list.Display();
            }
             catch (CADataStructureTest.LinkedList.EmptyListException emptyListException)
            {
                Console.Error.WriteLine( " \n " + emptyListException);
            }

            Console.ReadLine();
        }
        }
    }

效果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM