zoukankan      html  css  js  c++  java
  • 在O(1)时间删除指定链表结点

        #region 在O(1)时间删除指定链表结点
        /// <summary>
        /// 给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        class MyList<T> where T:IComparable
        {
            private T _data;
            private MyList<T> _next;
    
            public T data 
            {
                get { return _data; }
                set { _data = value; }
            }
            public MyList<T> next
            {
                get { return _next; }
                set { _next = value; }
            }
    
            public MyList()
            {
                data = default(T);
                next = null;
            }
            public MyList(T nodevalue)
            {
                data = nodevalue;
                next = null;
            }
            public void Insert(MyList<T> head,MyList<T> node)
            {
                if (head == null)
                {
                    head = new MyList<T>();
                    head = node;
                }
                else
                {
                    node.next = head.next;
                    head.next = node;
                }
            }
            public void Insert(MyList<T> head, T nodevalue)
            {
                MyList<T> temp = new MyList<T>(nodevalue);
                if (head == null)
                {
                    head = temp;
                }
                else
                {
                    temp.next = head.next;
                    head.next = temp;
                }
            }
            public void Delete(MyList<T> head, MyList<T> deleteNode)
            {
                if ((deleteNode==null)||(head==null)) return;
                if (deleteNode == head) 
                {
                    head = null;
                    deleteNode = null;
                }
                else if (deleteNode.next == null)
                {
                    MyList<T> pNode = new MyList<T>();
                    pNode = head;
                    while (pNode.next != deleteNode)
                        pNode = pNode.next;
                    pNode.next = null;
                    deleteNode = null;
                }
                else
                {
                    MyList<T> pNext = new MyList<T>();
                    pNext = deleteNode.next;
                    deleteNode.data = pNext.data;
                    deleteNode.next = pNext.next;
                    pNext = null;
                }
            }
            public void Print(MyList<T> head)
            {
                MyList<T> pNode = head;
                while (pNode != null)
                {
                    Console.Write(pNode.data + "   ");
                    pNode = pNode.next;
                     
                }
                Console.WriteLine();
            }
        }
        #endregion
        class Test{
            public void MyListTest()
            {
                MyList<int> head = new MyList<int>(1);
                head.Insert(head,2);
                MyList<int> WishDelete = new MyList<int>(4);
                head.Insert(head, WishDelete);
                head.Insert(head,3);
                head.Print(head);
                head.Delete(head,WishDelete);
                head.Print(head);
            }
        }
        class Program
        {
           
            static void Main(string[] args)
            {
                Test t = new Test();
                t.MyListTest();
            }
        }


  • 相关阅读:
    内存溢出OutOfMemory
    系统问题定位
    final域的内存语义
    volatile内存语义
    锁的内存语义
    happens-before
    python 安装brew(国内的自动安装)
    【Mac】点击dmg文件后无反应,解决方案
    Mac----xmind 8 下载与破解方法
    面试中可向HR 提的问题总结?
  • 原文地址:https://www.cnblogs.com/pangblog/p/3290211.html
Copyright © 2011-2022 走看看