資料結構與演算法—C#實現LinkedList執行個體

來源:互聯網
上載者:User

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

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

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

效果如下:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.