数据结构和算法基础之双向链表

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

namespace ConsoleApplication1
{
    /// <summary>
    /// 双向链表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DoubleNode<T>
    {
        public T Data;
        public DoubleNode<T> Preview;
        public DoubleNode<T> Next;

        public DoubleNode()
        {
            Data = default(T);
            Preview = null;
            Next = null;
        }

        public DoubleNode(T data)
        {
            Data = data;
            Preview = null;
            Next = null;
        }
    }
    /// <summary>
    /// 双向链表
    /// </summary>
    public class DoubleCircle<T>
    {

        public DoubleNode<T> Head;

        public int Length = 0;
        public DoubleCircle()
        {
            Head = null;
            Length = 0;
        }
        /// <summary>
        /// 尾插入
        /// </summary>
        /// <param name="data"></param>
        public void Add(T data)
        {
            DoubleNode<T> newNode = new DoubleNode<T>(data);
            DoubleNode<T> tmpNode = new DoubleNode<T>();
            if(Head==null)
            {
                Head = new DoubleNode<T>();
            }
            tmpNode = Head;
            while(tmpNode.Next!=null)
            {
                tmpNode = tmpNode.Next;
            }
            Length++;
            tmpNode.Next = newNode;
            newNode.Preview = tmpNode;
            newNode.Next = null;


        }

        /// <summary>
        /// 插入
        /// </summary>
        public void Inset(T data,int i)
        {
            DoubleNode<T> newNode = new DoubleNode<T>(data);
            DoubleNode<T> tmpNode = new DoubleNode<T>();
            tmpNode = Head;
            if(i>Length)
            {
                return;
            }
            int j = 0;
            while(j<i-1&&tmpNode.Next!=null)
            {
                tmpNode = tmpNode.Next;
                j++;
            }
            newNode.Preview = tmpNode;
            newNode.Next = tmpNode.Next;
            tmpNode.Next.Preview = newNode;
            tmpNode.Next = newNode;

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="i"></param>
        public void Delete(int i)
        {
           
            DoubleNode<T> tmpNode = new DoubleNode<T>();
            tmpNode = Head;
            if (i > Length)
            {
                return;
            }
            int j = 0;
            while (j < i && tmpNode.Next != null)
            {
                tmpNode = tmpNode.Next;
                j++;
            }
            tmpNode.Preview.Next = tmpNode.Next;
            tmpNode.Next.Preview = tmpNode.Preview;

        }
        /// <summary>
        /// 输出整个链表
        /// </summary>
        public void PrintLinkList()
        {
            DoubleNode<T> tmpNode = Head;
            while (tmpNode.Next != null)
            {
                tmpNode = tmpNode.Next;
                Console.WriteLine(tmpNode.Data);
            }

        }
    }
}

 

posted @ 2019-03-16 21:56  围墙外的世界  阅读(134)  评论(0编辑  收藏  举报