代码改变世界

[原创]c#,单链表,数据结构

2007-08-13 14:34  Virus-BeautyCode  阅读(899)  评论(0编辑  收藏  举报

 //链表结点
    class SingleLink
    {
        private int idata;
        public int Data
        {
            get
            { return idata; }
            set
            { idata = value; }
        }
        public SingleLink next;

        public SingleLink()
        {
            idata = -1;
            next = null;
        }

        public SingleLink(int data)
        {
            idata = data;
            next = null;
        }
    }

    //链表
    class SingleLinkList
    {
        private SingleLink head;
        private SingleLink rear;
        private SingleLinkList[] my=new SingleLinkList[100];
        public SingleLinkList()
        {
            head = new SingleLink();
            rear = new SingleLink();
        }

        public SingleLinkList this [int index]
        {
            get
            {
                return my[index];
            }
        }

        public bool IsEmpty()
        {
            return (head.next==null);
        }

        public  SingleLink FindSingleLink(int search)
        {
            SingleLink p = new SingleLink();
            p = head;
            if (p.next == null)
            {
                Console.WriteLine("the link is empty.");
                return null;
            }
            while (p.next != null)
            {
                if (p.Data == search)
                {
                    return p;
                }
                p=p.next;
            }
            Console.WriteLine("the link have not the "+search+".");
            return null;
        }

        public bool Insert(int data)
        {
            if (head.next == null)
            {
                return InsertAfterHead(data);
            }
            SingleLink newlink = new SingleLink(data);
            newlink.Data = data;
            rear.next = newlink;
            rear = newlink;
            return true;
        }

        public bool InsertAfter(int search, int data)
        {
            if (head.next == null)
            {
                Console.WriteLine("this link is empty.");
                return false;
                //return InsertAfterHead(data);
            }
            SingleLink x =FindSingleLink(search);

            if (x == null)
            {
                Console.WriteLine("this link have not the " + search+".");
                return false;
            }
           
            SingleLink newlink = new SingleLink(data);
            newlink.Data = data;
            newlink.next = x.next;
            x.next = newlink;
            Console.WriteLine("insert is successful.");
            return true;
        }

        private bool InsertAfterHead(int data)
        {
            Console.WriteLine("this is the first element.");
            SingleLink newlink = new SingleLink(data);
            newlink.Data = data;
            head.next = newlink;
            rear = newlink;
           
            Console.WriteLine("the first element insert successful!");
            return true;
        }

        public void Display()
        {
            SingleLink p=new SingleLink();
            p=head;
            if (p.next == null)
            {
                Console.WriteLine("this link is empty.");
                return;
            }
            p = head.next;
            do
            {
                Console.WriteLine(p.Data.ToString());
                p = p.next;
            } while (p != null);
          
        }

    }

class Program

    {
        static void Main(string[] args)
        {
            SingleLinkList list = new SingleLinkList();
            Console.WriteLine(list.IsEmpty());
            list.InsertAfter(4, 4);
            list.Display();
            list.Insert(4);
            list.Display();
            list.Insert(7);
            list.Insert(3);
            list.Insert(9);
            list.InsertAfter(5, 2);
            list.InsertAfter(7, 8);
            list.Display();

            Console.ReadLine();
        }

     }