C#数据结构之循环链表的实例代码

C#数据结构之循环链表的实例代码,第1张

概述复制代码代码如下:   publicclassNode   {       publicobjectElement;       publicNodeLink;

复制代码 代码如下:
    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;
        }
    }

总结

以上是内存溢出为你收集整理的C#数据结构循环链表的实例代码全部内容,希望文章能够帮你解决C#数据结构之循环链表的实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1263901.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存