• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

数据结构与算法---C#实现LinkedList实例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  这里创建一个单向链表,通过三个类来实现单向链表的基本操作:创建,新增(指定节点前,指定节点后),删除,判断是否为空....

  下面分别实现这三个类以及测试代码

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

效果如下:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VisualC#使用DirectX实现视频播放发布时间:2022-07-10
下一篇:
[转][C#]拆分参数对发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap