链表

链表

using System;

namespace 链表
{
    //一般链表都是有头部节点的 简称为头结点 头结点不参与运算
    public class LinkList
    {
        private Node _head;
        private int _count;
        
        public LinkList()
        {
            //new 对象 _head.next --> null head.data = 0
            _head = new Node();
            _count = 0;
        }

        public void AddItem(Node newNode)
        {
            //找到头结点
            Node tmpNode = _head;
            //循环找到最后结点
            while (tmpNode.Next != null)
            {
                //一直下移
                tmpNode = tmpNode.Next;
            }
//将最后结点和即将插入的结点链接
            tmpNode.Next = newNode;
            //个数++
            _count++;

        }

        public int GetLength()
        {
            return _count;
        }

        public void Insert(int index, Node newNode)
        {
            //0 
            if (index < 0 || index > _count)
            {
                Console.WriteLine("Over");
                return;
            }

            Node tmpNode = _head;
            for (int i = 0; i < index; i++)
            {
                tmpNode = tmpNode.Next;
            }
            //tmpNode? index的前一个结点
          
            newNode.Next = tmpNode.Next;
            tmpNode.Next = newNode;
            _count++;
            //0~l-1
            // l

        }


        /// <summary>
        /// 第一个就是index 第二个是value
        /// </summary>
        /// <param name="ac"></param>
        public void ShowItem(Action<int,int> ac)
        {
            if (_count == 0)
            {
                Console.WriteLine("空");
                return;
            }

            Node tmNode = _head.Next;
            for (int i = 0; i < _count; i++)
            {
                ac(i, tmNode.Data);
                //下移动
                tmNode = tmNode.Next;
            }
        }


        public int RemoveAt(int index)
        {
            //定义一个data返回值
            int returnValue = default(int);
            //判断是否出界
            if (index < 0 || index >=_count)
            {
                Console.WriteLine("error");
                goto returnTip;
            }
            //删除结点的前一个结点
            Node tmpNode = _head;
            //循环走
            for (int i = 0; i < index; i++)
            {
                tmpNode = tmpNode.Next;
            }
            //要删除的结点
            Node deleteNode = tmpNode.Next;
            //牵手删除结点的后一个结点
            tmpNode.Next = tmpNode.Next.Next;
            //不让其连接
            deleteNode.Next = null;
            //个数--
            _count--;
            //返回删除结点的数据data
            returnValue = deleteNode.Data;

            returnTip:
                return returnValue;
        }


        public void Clear()
        {
            _head.Next = null;
            _count = 0;
        }

    public void FanZhuan()
        {
            Node T1, T2;
           T2 = _head.Next;
            _head.Next = null;
            while (T2 != null)
            {
               T1 = T2.Next;
               T2.Next = _head.Next;
               _head.Next = T2;
                T2 = T1;
           }
        }
        public int Min()
        {
           int returnValue = default(int);
            Node min, deletemin, deletepre, tmp;
            deletepre = tmp = _head;
            deletemin = min = _head.Next;
            while (min != null)
            {
                if(deletemin.Data >min.Data)
                {
                    deletepre = tmp;
                    deletemin = min;
                   
                }
                min = min.Next;
                tmp = tmp.Next;
            }
            deletepre.Next = deletepre.Next.Next;
            deletemin.Next = null;
             _count--;
            returnValue = deletemin.Data;
            return returnValue;
        }

    }
}


namespace 链表
{
    public class Node
    {
        public int Data;
        //这个就是地址
        public Node Next;

        /// <summary>
        /// 构造函数目的就是初始化
        /// </summary>
        public Node()
        {
            Data = default(int);
            Next = null;
        }
        
        public Node(int  value)
        {
            Data = value;
            Next = null;
        }
    }
}

using System;

namespace 链表
{
    internal class Program
    {

        public static void Show(int index, int value)
        {
            Console.WriteLine("第{0}个元素是{1}",index+1,value);
        }

        public static void Main(string[] args)
        {
            
            LinkList linkList = new LinkList();
            linkList.AddItem(new Node(1));
            linkList.AddItem(new Node(2));
            linkList.AddItem(new Node(3));
            linkList.AddItem(new Node(4));
            linkList.AddItem(new Node(5));
          linkList.Insert(1,new Node(1000));
            //1 1000 2 3 4 5 
            linkList.Clear();

            Console.WriteLine(linkList.RemoveAt(1));
            Console.WriteLine(linkList.GetLength());
            linkList.ShowItem(Show);
           
        }
    }
}

  

posted on 2018-11-10 11:31  大鱼。  阅读(83)  评论(0)    收藏  举报