C#链表;循环链表

本文介绍C#编程中如何实现链表及其循环链表的数据结构。通过Node类详细阐述了节点的创建及链表的操作,包括插入、删除等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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 current;
        protected Node header;
        private int count;
        //长度
        public int Count
        {
            get
            {
                return 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 != header)
            {
                Console.WriteLine(current.Link.Element);
                current = current.Link;
            }
        }
        //查找item如果存在就是返回就是和obj数据相同的的前一个节点(这样就能让链接跳过obj那数据到下一个就相当于删除了),从header开始查找,如果不存在返回的是null
        private Node FindPrevious(object obj)
        {
            Node current = header;
            while (!(current.Link == null) && current.Link.Element != obj)
                current = current.Link;
            return current;
        }
        //查找item如果存在就是返回就是和item数据相同的,从header开始查找,如果不存在返回的是null
        private Node Find(object obj)
        {
            Node current = new Node();
            current = header;
            while (current.Element != obj)
                current = current.Link;
            return current;
        }
        //移除
        public void Remove(object obj)
        {
            Node p = FindPrevious(obj);
            if (!(p.Link == null))
                p.Link = p.Link.Link;
            count--;
        }
        //插入
        public void Insert(object n1, object n2)
        {
            Node current = new Node();
            Node newnode = new Node(n1);
            current = Find(n2);
            newnode.Link = current.Link;
            current.Link = newnode;
            count++;
        }
        //在头位置插入
        public void InsertFirst(object obj)
        {
            Node current = new Node(obj);
            current.Link = header;
            header.Link = current;
            count++;
        }
        //返回第n个节点,从后算起的第n个,起始为0
        public Node Move(int n)
        {
            Node current = header.Link;
            Node temp;
            for (int i = 0; i <= n; i++)
                current = current.Link;
            if (current.Link == header)
                current = current.Link;
            temp = current;
            return temp;
        }
        //得到头节点
        public Node getFirst()
        {
            return header;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值