C#自定义链表

C#自定义链表,测试图:
这里写图片描述

下面是代码:

// 自写,如有错误请见谅。
// 自定义链表,包括索引器,添加及删除功能。
// 仅仅作为mark~

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

namespace ConsoleApplication16
{
    class Program
    {
        static void Main(string[] args)
        {
            LinkedListTest<int> test = new LinkedListTest<int> { };
            test.Add(0);
            test.Add(1);
            test.Add(2);
            test.Add(3);
            Console.WriteLine("链表添加元素测试:");
            for(int i = 0; i< test.Length();i++)
            Console.WriteLine("test{0}", i + " = " + test[i]);
            Console.WriteLine("为链表中2位置添加数字5测试:");
            test.AddAt(2,5);
            for (int i = 0; i < test.Length(); i++)
            Console.WriteLine("test{0}", i + " = " + test[i]);
            Console.WriteLine("删除链表中2位置测试:");
            test.RemoveAt(2);
            for (int i = 0; i < test.Length(); i++)
            Console.WriteLine("test{0}", i + " = " + test[i]);
            Console.ReadKey();
        }

    }
    class LinkedListTest <T>
    {
        LinkedListTest<T> frontNode;
        public T val;
        LinkedListTest<T> nextNode;
        // 在链表末尾添加元素
        public void Add (T v)
        {
            LinkedListTest<T> node = this;
            while (node.nextNode != null)
            {
                node = node.nextNode;
            }
            node.nextNode = new LinkedListTest<T> { val = v };
            node.nextNode.frontNode = node;
        }
        // 在链表的某个位置添加元素
        public void AddAt(int index,T value)
        {
            LinkedListTest<T> node = this; 
            for(int i = 0; i < index + 1; i++)
            {
                node = node.nextNode;
            }
            node.frontNode.nextNode = new LinkedListTest<T> { val = value };
            node.frontNode.nextNode.frontNode = node.frontNode;
            node.frontNode.nextNode.nextNode = node;
            node.frontNode = node.frontNode.nextNode;
        }
        // 在链表某个位置删除元素
        public void RemoveAt(int index)
        {
            LinkedListTest<T> node = this;
            for (int i = 0; i < index + 1; i++)
            {
                node = node.nextNode;
            }
            node.frontNode.nextNode = node.nextNode;
            node.nextNode.frontNode = node.frontNode;
            node = null;
        }
        //链表总长度
        public int Length()
        {
            int count = 0;
            LinkedListTest<T> node = this;
            while(node.nextNode != null)
            {
                node = node.nextNode;
                count++;
            }
            return count;
        }
        // 链表索引器
        public T this[int index]
        {
            get
            {
                LinkedListTest<T> node = this;
                for(int i = 0;i <= index;i++)
                {
                    node = node.nextNode;
                }
                return node.val;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值