• 数据结构和算法-单链表


    链表

    链表是以节点的方式存储
    每个节点包含data域,next域,next域指向下一个节点
    链表分为:带头结点、不带头节点,根据实际需求确定

    客户端

    LinkedList<string> list = new LinkedList<string>();
                list.Add("fan");
                list.Add("宋江");
                list.Add("卢俊义");
                list.Add("林冲");
                list.Add("武松");
                list.Insert("李逵", 3);
                list.Print();
    

    LinkedNode

    public class LinkedNode<T>
        {
            public LinkedNode(T data)
            {
                this.Data = data;
            }
            public T Data { get; set; }
            public override string ToString()
            {
                return this.Data.ToString();
            }
            public LinkedNode<T> Next { get; set; }
        }
    

    LinkedList

    public class LinkedList<T>
        {
            private LinkedNode<T> _head = new LinkedNode<T>(default(T));
            /// <summary>
            /// 打印
            /// </summary>
            public void Print()
            {
                if (this.IsEmpty())
                {
                    Console.WriteLine("空链表");
                    return;
                }
                var tempNode = _head.Next;
                while (tempNode != null)
                {
                    Console.WriteLine(tempNode.ToString());
                    tempNode = tempNode.Next;
                }
            }
            /// <summary>
            /// 翻转打印
            /// </summary>
            public void ReversePrint()
            {
                if (this.IsEmpty())
                {
                    Console.WriteLine("空链表");
                    return;
                }
                Stack<LinkedNode<T>> stack = new Stack<LinkedNode<T>>();
                var tempNode = _head.Next;
                while (tempNode != null)
                {
                    stack.Push(tempNode);
                    tempNode = tempNode.Next;
                }
                stack.TryPop(out tempNode);
                while (tempNode != null)
                {
                    Console.WriteLine(tempNode.ToString());
                    stack.TryPop(out tempNode);
                }
            }
            /// <summary>
            /// 清空链表
            /// </summary>
            public void Clear()
            {
                _head.Next = null;
            }
            /// <summary>
            /// 查找
            /// </summary>
            /// <returns></returns>
            public LinkedNode<T> Find(T data)
            {
                var tempNode = _head.Next;
                while (tempNode!=null)
                {
                    if (tempNode.Data.GetHashCode() == data.GetHashCode())
                    {
                        return tempNode;
                    }
                }
                return null;
            }
            /// <summary>
            /// 移除节点
            /// </summary>
            /// <param name="node"></param>
            public void Remove(T node)
            {
                var tempNode = _head;
                while (true)
                {
                    if (tempNode==null)
                    {
                        break;
                    }
                    if (node.GetHashCode() == tempNode.Next.GetHashCode())
                    {
                        tempNode.Next = tempNode.Next.Next;
                        break;
                    }
                    tempNode = tempNode.Next;
                }
            }
            /// <summary>
            /// 添加节点
            /// </summary>
            /// <param name="data"></param>
            public void Add(T data)
            {
                var lastNode = this.GetLastNode(_head);
                lastNode.Next = new LinkedNode<T>(data);
            }
            /// <summary>
            /// 插入节点
            /// </summary>
            /// <param name="data"></param>
            /// <param name="index"></param>
            public void Insert(T data, int index)
            {
                var tempNode = _head;
                int curIndex = 0;
                while (tempNode!=null)
                {
                    if (curIndex++ >= index)
                    {
                        break;
                    }
                    tempNode = tempNode.Next;
                }
                if (tempNode!=null)
                {
                    //此时tempNode是index节点的上一个节点
                    var tempNext = tempNode.Next;
                    tempNode.Next = new LinkedNode<T>(data);
                    tempNode.Next.Next = tempNext;
                }
                
    
            }
    
            /// <summary>
            /// 获取链表长度
            /// </summary>
            /// <returns></returns>
            public int GetLength()
            {
                int length = 0;
                var tempNode = _head.Next;
                while (tempNode != null)
                {
                    length++;
                    tempNode = tempNode.Next;
                }
                return length;
            }
            /// <summary>
            /// 获取最后一个节点
            /// </summary>
            /// <param name="node"></param>
            /// <returns></returns>
            private LinkedNode<T> GetLastNode(LinkedNode<T> node)
            {
                if (node.Next == null)
                {
                    return node;
                }
                return this.GetLastNode(node.Next);
            }
            /// <summary>
            /// 获取倒数节点
            /// </summary>
            /// <param name="lastIndex"></param>
            /// <returns></returns>
            public LinkedNode<T> GetLastIndexNode(int lastIndex)
            {
                if (this.IsEmpty())
                {
                    return null;
                }
                int length = this.GetLength();
                int index = length - lastIndex - 1;
                if (index < 0)
                {
                    return null;
                }
                return this.GetIndexNode(index);
    
            }
            /// <summary>
            /// 获取指定位置索引
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            public LinkedNode<T> GetIndexNode(int index)
            {
                if (this.IsEmpty())
                {
                    return null;
                }
                var tempNode = _head.Next;
                int curIndex = 0;
                while (tempNode != null)
                {
                    if (curIndex++ == index)
                    {
                        break;
                    }
                    tempNode = tempNode.Next;
                }
                return tempNode;
            }
            /// <summary>
            /// 链表是否为空
            /// </summary>
            /// <returns></returns>
            private bool IsEmpty()
            {
                return _head.Next == null;
            }
    
        }
    
  • 相关阅读:
    软工实践个人总结
    第06组 Beta版本演示
    第06组 Beta冲刺(5/5)
    第06组 Beta冲刺(4/5)
    第06组 Beta冲刺(3/5)
    第06组 Beta冲刺(2/5)
    第06组 Beta冲刺(1/5)
    第06组 Alpha事后诸葛亮
    第06组 Alpha冲刺(6/6)
    第06组 Alpha冲刺(5/6)
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/13286173.html
Copyright © 2020-2023  润新知