码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构和算法-单链表

时间:2020-07-12 00:30:11      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:翻转   oid   nod   头结点   sep   set   str   sum   list   

链表

链表是以节点的方式存储
每个节点包含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;
        }

    }

数据结构和算法-单链表

标签:翻转   oid   nod   头结点   sep   set   str   sum   list   

原文地址:https://www.cnblogs.com/fanfan-90/p/13286173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!