当前位置:编程学习 > C#/ASP.NET >>

数据结构(c#)——双向链表


using System;

using System.Collections.Generic;

using System.Text;

 

namespace DataStructure

{

    /// <summary>

    /// 双向链表类

    /// </summary>

    /// <typeparam name="T"></typeparam>

    class DoubleLinkList<T> : IListDS<T>

    {

        private DoubleLinkItem<T> head;

        public DoubleLinkItem<T> Head

        {

            get { return head; }

            set { head = value; }

        }

 

        /// <summary>

        /// 获取双向链表长度

        /// </summary>

        /// <returns></returns>

        public int GetLength()

        {

            if (IsEmpty())

                return 0;

            int length = 1;

            DoubleLinkItem<T> temp = head;

            while (temp.Next != null)

            {

                temp = temp.Next;

                length++;

            }

            return length;

        }

 

        /// <summary>

        /// 清除双向链表所有数据

        /// </summary>

        public void Clear()

        {

            if (!IsEmpty())

            {

                DoubleLinkItem<T> temp = head;

                while (temp.Next != null)

                {

                    temp = temp.Next;

                    temp.Last = null;

                }

                temp = null;

            }

        }

 

        /// <summary>

        /// 判断双向链表是否为空

        /// </summary>

        /// <returns></returns>

        public bool IsEmpty()

        {

            if (head == null)

                return true;

            else

                return false;

        }

 

        /// <summary>

        /// 判断双向链表是否已满

        /// </summary>

        /// <returns></returns>

        public bool IsFull()

        {

            return false;

        }

 

        /// <summary>

        /// 在双向链表的尾端添加一个新数据

        /// </summary>

        /// <param name="item"></param>

        public void Append(T item)

        {

            DoubleLinkItem<T> temp = new DoubleLinkItem<T>(null, item, null);

            if (IsEmpty())

            {

                this.head = temp;

                return;

            }

            DoubleLinkItem<T> tempItem = GetListItem(this.GetLength() - 1);

            tempItem.Next = temp;

        }

 

        /// <summary>

        /// 在双向链表指定的位置插入一个新项

        /// </summary>

        /// <param name="item"></param>

        /// <param name="index"></param>

        public void Insert(T item, int index)

        {

            if (index < 0)

                throw new Exception("插入位置不能小于0");

            if (index > this.GetLength() + 1)

                throw new Exception("插入位置超出链表长度");

            if (index == 0)

            {

<
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,