数据结构和算法-单链表

2021-04-20 06:27

阅读:378

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

链表

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

客户端

LinkedList list = new LinkedList();
            list.Add("fan");
            list.Add("宋江");
            list.Add("卢俊义");
            list.Add("林冲");
            list.Add("武松");
            list.Print();

LinkedNode

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

LinkedList

public class LinkedList
    {
        private LinkedNode _head = new LinkedNode(default(T));
        /// 
        /// 打印
        /// 
        public void Print()
        {
            if (this.IsEmpty())
            {
                return;
            }
            var tempNode = _head.Next;
            while (tempNode != null)
            {
                Console.WriteLine(tempNode.ToString());
                tempNode = tempNode.Next;
            }
        }
        /// 
        /// 翻转打印
        /// 
        public void ReversePrint()
        {
            if (this.IsEmpty())
            {
                return;
            }
            Stack> stack = new Stack>();
            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);
            }



        }
        /// 
        /// 添加节点
        /// 
        /// 
        public void Add(T data)
        {
            var lastNode = this.GetLastNode(_head);
            lastNode.Next = new LinkedNode(data);
        }
        /// 
        /// 插入节点
        /// 
        /// 
        /// 
        public void Insert(T data, int index)
        {
            var tempNode = _head;
            int curIndex = 0;
            while (curIndex++ (data);
                tempNode.Next.Next = nextNode;
            }
        }

        /// 
        /// 获取链表长度
        /// 
        /// 
        public int GetLength()
        {
            int length = 0;
            var tempNode = _head.Next;
            while (tempNode != null)
            {
                length++;
                tempNode = tempNode.Next;
            }
            return length;
        }
        /// 
        /// 获取最后一个节点
        /// 
        /// 
        /// 
        private LinkedNode GetLastNode(LinkedNode node)
        {
            if (node.Next == null)
            {
                return node;
            }
            return this.GetLastNode(node.Next);
        }
        /// 
        /// 获取倒数节点
        /// 
        /// 
        /// 
        public LinkedNode GetLastIndexNode(int lastIndex)
        {
            if (this.IsEmpty())
            {
                return null;
            }
            int length = this.GetLength();
            int index = length - lastIndex - 1;
            if (index 
        /// 获取指定位置索引
        /// 
        /// 
        /// 
        public LinkedNode 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;
        }
        /// 
        /// 链表是否为空
        /// 
        /// 
        private bool IsEmpty()
        {
            return _head.Next == null;
        }

    }

数据结构和算法-单链表

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

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


评论


亲,登录后才可以留言!