zoukankan      html  css  js  c++  java
  • 数据结构和算法基础之双向链表

    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);
                }
    
            }
        }
    }
  • 相关阅读:
    浅谈HashMap的内部实现
    浅谈Java的集合体系
    如何通过注解Bean类来封装SQL插入语句
    谈一谈垃圾回收器
    万物皆对象
    关于枚举
    Servlet向客户端发送中文数据的编码情况
    "流"派家族,一脉相承
    个人简历用HTML编写
    get和post的区别
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/10544557.html
Copyright © 2011-2022 走看看