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 CircleLinkList<T>
        {
            public Node<T> Head;
            public CircleLinkList()
            {
                Head = null;
            }
            /// <summary>
            /// 增加
            /// </summary>
            /// <param name="data"></param>
            public void Add(T data)
            {
                Node<T> newNode = new Node<T>(data);
                Node<T> tmpNode = new Node<T>();
                if(Head==null)
                {
                    Head = new Node<T>();
                }
                tmpNode = Head;
                while(tmpNode.Next!=null&& tmpNode.Next!= Head)
                {
                    tmpNode = tmpNode.Next;
                }
                tmpNode.Next = newNode;
                newNode.Next = Head;
            }
    
            /// <summary>
            /// 在i位置前面插入节点
            /// </summary>
            /// <param name="data"></param>
            /// <param name="i"></param>
            public void Insert(T data, int i)
            {
                Node<T> newNode = new Node<T>(data);
                Node<T> tmpNode = new Node<T>();
                tmpNode = Head;
                
                int j = 0;
                while (j < i - 1 && tmpNode.Next != null)
                {
                    tmpNode = tmpNode.Next;
                    j++;
                }
                newNode.Next = tmpNode.Next;
                tmpNode.Next = newNode;
            }
    
            /// <summary>
            /// 删除节点
            /// </summary>
            /// <param name="i"></param>
            public void Delete(int i)
            {
                int j = i;
                Node<T> tmpNode = new Node<T>();
                tmpNode = Head;
                while (j > 0 && tmpNode.Next != null)
                {
                    tmpNode = tmpNode.Next;
                    j--;
                }
                Node<T> curNode = tmpNode.Next;
                tmpNode.Next = curNode.Next;
            }
            /// <summary>
            /// 输出整个链表
            /// </summary>
            public void PrintLinkList()
            {
                Node<T> tmpNode = Head;
                while (tmpNode.Next != Head)
                {
                    tmpNode = tmpNode.Next;
                    Console.WriteLine(tmpNode.Data);
                }
                
            }
        }
    }
  • 相关阅读:
    【洛谷习题】公路修建
    priority_queue用法简记
    【洛谷习题】无线通讯网
    【SCOI2005】繁忙的都市
    第四周 3.20-3.26
    第三周 3.13-3.19
    第二周 3.6-3.12
    第一周 2.28-3.5
    第六周 2.21-2.27
    2018湘潭邀请赛 AFK题解 其他待补...
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/10544542.html
Copyright © 2011-2022 走看看