链表常见操作

class node_test
    {
        int data;
        node_test next;

        public node_test() { }
        public node_test(int data) { this.data = data; }

        public int Data { get { return data; } set { data = value; } }

        public node_test Next { get { return next; } set { next = value; } }
    }

    class List_test
    {
        node_test head;

        int count;

        public List_test() { }
        public List_test(node_test head) { this.head = head; }

        public int Count
        { get
            {
                if (head != null)
                {
                    node_test p = head;
                    while (p.Next != null)
                    {
                        ++count;
                        p = p.Next;
                    }
                    ++count;
                }
                return count;
            }
        }

        public node_test Head { get { return head; }set { head = value; } }

        public void Addpend(int data)
        {
            node_test q = new node_test(data);
            if (head == null)
            {
                head = q;
                return;
            }

            node_test p = head;

            while (p.Next != null)
            {
                p = p.Next;
            }
            p.Next = q;
        }

        public void Insert(int i, int data)
        {
            if (i < 1 || head == null) return;

            node_test q = new node_test(data);
            if (i == 1)
            {
                q.Next = head;
                head = q;
                return;
            }

            node_test p = head;
            int j = 1;
            node_test r = new node_test();
            while (p.Next != null && j < i)
            {
                ++j;
                r = p;
                p = p.Next;
            }

            if (i == j)
            {
                r.Next = q;
                q.Next = p;
            }
            else
                return;
        }

        public void InsertPos(int i, int data)
        {
            if (i < 1 || head == null) return;

            node_test q = new node_test(data);

            if (i == 1)
            {
                q.Next = head.Next;
                head.Next = q;
                return;
            }

            node_test p = head;
            int j = 1;

            while (p.Next != null && j < i)
            {
                ++j;
                p = p.Next;
            }

            if (i == j)
            {
                q.Next = p.Next;
                p.Next = q;
            }
            else
                return;
        }

        public int Delete(int i)
        {
            int temp = -1;
            if (i < 1 || head == null) return temp;

            if (i == 1)
            {
                node_test r = new node_test();
                r = head;
                head = head.Next;
                temp = r.Data;
            }

            node_test p = head;
            node_test q = new node_test();
            int j = 1;
            while (p.Next != null && j < i)
            {
                ++j;
                q = p;
                p = p.Next;
            }

            if (i == j)
            {
                temp = p.Data;
                q.Next = p.Next;
            }

            return temp;
        }

        public int GetIndex(int data)
        {
            if (head == null) return -1;

            node_test p = head;

            int j = 1;
            while (p.Next != null && !p.Data.Equals(data))
            {
                ++j;
                p = p.Next;
            }

            return j;
        }

        public int Getvalue(int i)
        {
            if (head == null) return -1;

            node_test p = head;

            if (i == 1)
            {
                return head.Data;
            }

            int j = 1;
            while (p.Next != null)
            {
                ++j;
                p = p.Next;
            }

            if (i == j)
            {
                return p.Data;
            }

            return -1;
        }

        public void Print()
        {

            if (head == null) return;
            node_test p = head;

            while (p.Next != null)
            {
                Console.WriteLine(p.Data);
                p = p.Next;
            }

            Console.WriteLine(p.Data);
        }

        public void RevertList()
        {
            if (head == null) return;

            node_test p = head.Next;

            node_test q = new node_test();

            head.Next = null;

            while (p != null)
            {
                q = p;
                p = p.Next;
                q.Next = head;
                head = q;
            }
        }

        public int MidNode()
        {
            if (head == null) return -1;

            if (head.Next.Next != null)
            {
                node_test fastNode = head.Next.Next;
                node_test slowNode = head;

                while (fastNode != null && fastNode.Next != null)
                {
                    fastNode = fastNode.Next.Next;
                    slowNode = slowNode.Next;
                }

                return slowNode.Data;
            }

            return -1;
        }

        public List_test MergeList(List_test lis, List_test _lis)
        {
            if (lis.head == null && _lis.head == null) return null;

            node_test ha = lis.head;
            node_test hb = _lis.head;
            List_test hc = new List_test();

            while (ha.Next != null)
            {
                ha = ha.Next;
            }

            ha.Next = hb;
            hc.head = lis.head;

            return hc;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值