数据结构——单链表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSDN_Testing
{
    #region
    //@Author: Peter
    //@Date: 7/26/2010
    #endregion

    #region
    /// <summary>
    /// The definition of the node, and the link is composed of node.
    /// There are two fields in the class, val and next.
    /// </summary>
    public class Node
    {
        private Int32 val;
        private Node next;

        public Node()
        { }

        public Node(Int32 n)
        {
            this.val = n;
            this.next = null;
        }

        public Int32 Val
        {
            get
            {
                return val;
            }
            set
            {
                val = value;
            }
        }

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

        public override bool Equals(object obj)
        {
            return (val == ((Node)obj).val) && (next == ((Node)obj).next);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }

    #endregion

    #region 
    /// <summary>
    /// The definition of the link list.
    /// There is only one field, it is head, the Head of the link.
    /// The definition contains serval operations on the link,
    /// such as creating a link, add a node to the end, remove a node from link,
    /// put out the information of the link.
    /// </summary>
    public class LinkedList:ICloneable
    {
        private Node head;

        public LinkedList()
        {
            head = null;
        }

        public LinkedList(params Int32 [] ns)
        {
            for (Int32 i = 0; i < ns.Length; i++)
            {
                AddNodeToEnd(ns[i]);
            }
        }

        //Add a new node at the end of the link
        public void AddNodeToEnd(Int32 n)
        {
            Node newN = new Node(n);

            if (head == null)
            {
                head = newN;
            }
            else
            {
                Node pt = head;

                while (pt.Next != null)
                    pt = pt.Next;

                pt.Next = newN;
            }
        }

        //Return the length of the link
        public Int32 Length
        {
            get
            {
                if (head == null)
                {
                    return 0;
                }
                else
                {
                    Int32 len = 0;
                    Node pt = head;
                    while (pt != null)
                    {
                        pt = pt.Next;
                        len++;
                    }
                    return len;
                }
            }
        }

        //Insert a new node at the position of index
        public Boolean InsertNodeAt(Int32 index, Int32 val)
        {
            if (index < 1 || index > Length + 1)
                return false;

            if (index == 1)
            {
                Node newN = new Node(val);
                newN.Next = head;
                head = newN;
            }
            else if (index == Length + 1)
            {
                Node pt = head;
                while (pt.Next != null)
                    pt = pt.Next;

                pt.Next = new Node(val);
            }
            else
            {
                Node pt = head;
                Int32 i = 1;

                while (pt.Next != null)
                {
                    if (i + 1 == index)
                    {
                        Node newN = new Node(val);
                        newN.Next = pt.Next;
                        pt.Next = newN;
                        break;
                    }
                    i++;
                    pt = pt.Next;
                }
            }


            return true;
        }

        //put out the info about the elements in the link and the length
        public void PrintLinkedList()
        {
            Node pt = head;
            while (pt.Next != null)
            {
                Console.Write(pt.Val + " -> ");
                pt = pt.Next;
            }
            Console.WriteLine(pt.Val);
            Console.WriteLine("The lenth of the LinkedList is {0}", Length);
        }

        //Delete the node which value equals v
        public void DeleteNodeByValue(Int32 v)
        {
            if (Length == 0)
                return;

            if (head.Val == v)
            {
                head = head.Next;
                return;
            }
            else
            {
                Node pt = head;
                while (pt.Next != null)
                {
                    if (pt.Next.Val == v)
                    {
                        pt.Next = pt.Next.Next;
                        break;
                    }
                    pt = pt.Next;
                }
            }
        }

        //Reverse the link
        public void ReverseLink()
        {
            if (head == null || head.Next == null)
                return;

            Node pre = head;
            Node cur = head.Next;
            if (cur.Next == null)
            {
                cur.Next = pre;
                pre.Next = null;
                head = cur;
            }
            else
            {
                Node next = cur.Next;

                while (next != null)
                {
                    cur.Next = pre;
                    pre = cur;
                    cur = next;
                    next = next.Next;
                }
                cur.Next = pre;
                head.Next = null;
                head = cur;
            }
        }

        //Sort the link by ascending
        public void Sort()
        {
            Node cur = head;
            if (head == null || head.Next == null)
                return;

            Node next = cur.Next;
            while (next != null)
            {
                Node pt = head;
                Node ptPre = head;
                while (!pt.Equals(next))
                {
                    if (next.Val < pt.Val)
                    {
                        if (pt.Equals(ptPre) && pt.Equals(head))
                        {
                            cur.Next = next.Next;
                            next.Next = head;
                            head = next;
                        }
                        else
                        {
                            cur.Next = next.Next;
                            next.Next = pt;
                            ptPre.Next = next;
                        }

                        break;
                    }
                    ptPre = pt;
                    pt = pt.Next;
                }

                cur = next;
                next = next.Next;
            }
        }

        //Merge two link, the result is stored in the first link in order.
        //no influence on the second link
        public void Merge(LinkedList link2)
        {
            Node pt = head;

            LinkedList temp = (LinkedList)link2.Clone();
            while (pt.Next != null)
                pt = pt.Next;

            pt.Next = temp.head;

            this.Sort();
        }

        public object  Clone()
        {
            Node pt = head;
            if (head == null)
                return null;

            LinkedList link = new LinkedList();
            while (pt != null)
            {
                link.AddNodeToEnd(pt.Val);
                pt = pt.Next;
            }
            return link;
        }

    }

    #endregion

    class Program
    {
        static void Main(string[] args)
        {
            
            LinkedList link = new LinkedList(1, 3, 15, 4, 5);

            Console.WriteLine("Construct a new link:");
            link.PrintLinkedList();
            Console.WriteLine();

            Console.WriteLine("Insert a node which is 0 at index:1 ");
            link.InsertNodeAt(1,0);
            link.PrintLinkedList();
            Console.WriteLine();

            Console.WriteLine("Insert a node which is 6 at index:7");
            link.InsertNodeAt(7,6);
            link.PrintLinkedList();
            Console.WriteLine();

            Console.WriteLine("Insert a node which is 7 at index:7");
            link.InsertNodeAt(7,7);
            link.PrintLinkedList();

            Console.WriteLine("Delete the first node which vaule is 0 from start");
            link.DeleteNodeByValue(0);
            link.PrintLinkedList();
            Console.WriteLine();

            Console.WriteLine("Delete the first node which vaule is 6 from start");
            link.DeleteNodeByValue(6);
            link.PrintLinkedList();
            Console.WriteLine();

            Console.WriteLine("Delete the first node which vaule is 3 from start");
            link.DeleteNodeByValue(3);
            link.PrintLinkedList();
            Console.WriteLine();

            Console.WriteLine("Reverse the link");
            link.ReverseLink();
            link.PrintLinkedList();
            Console.WriteLine();

            Console.WriteLine("Sort the link by ascending");
            link.Sort();
            link.PrintLinkedList();
            Console.WriteLine();

            LinkedList link2 = new LinkedList(11,22);

            Console.WriteLine("Merge the link2 to the link and store result in link by ascending");
            link.Merge(link2);
            link.PrintLinkedList();
            Console.WriteLine("put out the info of link2, confirm the merger has no influence on link2");
            link2.PrintLinkedList();
            
        }
    }
}

posted on 2010-07-26 20:56  PeterZhang  阅读(1529)  评论(0)    收藏  举报