数据结构和算法-单链表

Posted fanfan-90

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构和算法-单链表相关的知识,希望对你有一定的参考价值。

链表

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

客户端

LinkedList<string> list = new LinkedList<string>();
            list.Add("fan");
            list.Add("宋江");
            list.Add("卢俊义");
            list.Add("林冲");
            list.Add("武松");
            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())
            {
                return;
            }
            var tempNode = _head.Next;
            while (tempNode != null)
            {
                Console.WriteLine(tempNode.ToString());
                tempNode = tempNode.Next;
            }
        }
        /// <summary>
        /// 翻转打印
        /// </summary>
        public void ReversePrint()
        {
            if (this.IsEmpty())
            {
                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>
        /// <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 (curIndex++ < index)
            {
                if (tempNode == null)
                {
                    return;
                }
                tempNode = tempNode.Next;
            }
            if (tempNode != null)
            {
                var nextNode = tempNode.Next;
                tempNode.Next = new LinkedNode<T>(data);
                tempNode.Next.Next = nextNode;
            }
        }

        /// <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;
        }

    }


以上是关于数据结构和算法-单链表的主要内容,如果未能解决你的问题,请参考以下文章

[每日算法220508] 单链表和双链表的反转

数据结构代码(用C语言) 单链表的插入和删除

单链表逆置

[每日算法220508] 单链表和双链表的反转

[每日算法220508] 单链表和双链表的反转

#yyds干货盘点# 数据结构与算法之单链表