程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#數據結構之循環鏈表的實例代碼

C#數據結構之循環鏈表的實例代碼

編輯:C#基礎知識

代碼如下:

    public class Node
    {
        public object Element;
        public Node Link;

        public Node()
        {
            Element = null;
            Link = null;
        }

        public Node(object theElement)
        {
            Element = theElement;
            Link = null;
        }
    }

代碼如下:

public class LinkedList
    {
        //頭結點
        protected Node Header;

        private int count;

        public LinkedList()
        {
            count = 0;
            Header = new Node("header");
            Header.Link = Header;
        }

        public bool IsEmpty()
        {
            return (Header.Link == null);
        }

        public void MakeEmpty()
        {
            Header.Link = null;
        }

        public void PrintList()
        {
            Node current = new Node();
            current = Header;
            while (current.Link.Element.ToString() != "header")
            {
                Console.WriteLine(current.Link.Element);
                current = current.Link;
            }
        }

        private Node FindPrevious(object n)
        {
            Node current = Header;
            while (!(current.Link == null) && current.Link.Element != n)
            {
                current = current.Link;
            }
            return current;
        }

        private Node Find(object item)
        {
            Node current = new Node();
            current = Header.Link;
            while (current.Element != item)
            {
                current = current.Link;
            }
            return current;
        }

        public void Insert(object newItem, object after)
        {
            Node current = new Node();
            Node newNode = new Node(newItem);
            current = Find(after);
            newNode.Link = current.Link;
            current.Link = newNode;
            count++;
        }

        public void Remove(object n)
        {
            Node p = FindPrevious(n);
            if (!(p.Link == null))
            {
                p.Link = p.Link.Link;
                count--;
            }
        }

        public void InsertFirst(object n)
        {
            Node current = new Node(n);
            current.Link = Header;
            Header.Link = current;
            count++;
        }

        public Node Move(int n)
        {
            Node current = Header.Link;
            Node tmp;
            for (int i = 0; i <= n; i++)
            {
                current = current.Link;
            }
            if (current.Element.ToString() == "header")
            {
                current = current.Link;
            }
            tmp = current;
            return tmp;
        }

        public Node GetFirst()
        {
            return Header;
        }
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved