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);
                }
    
            }
        }
    }
  • 相关阅读:
    摊还分析
    web端手机方向传感器闲谈
    研一一年论文总结(下)
    Jupyter自定义设置详解
    HAProxy实现动静分离和负载均衡
    欧拉项目 323题
    mysql基本操作
    以后的IT路还很长(1)
    【翻译】在Ext JS集成第三方库
    吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring IoC容器BeanFactory和ApplicationContext
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/10544557.html
Copyright © 2011-2022 走看看