[C#删除链表中的节点

问题描述 投票:0回答:1

我正在研究具有链表的基数排序算法。我已经牢记如何解决它,但是我需要能够删除整个链表(在遍历链表时一一删除节点)。我遇到的问题是我当前的(节点)指针没有首先获得该值。我已经尝试了许多不同的方法,但是我想我不了解如何在删除节点时重置指针“ first”,“ current”,“ left”,“ right”]

Radixsort.cs:

public static void Counting_sort(LinkedList<int> list, int exp)
    {
        LinkedList<int> D = new LinkedList<int>();
        LinkedList<int>[] lists = new LinkedList<int>[10];
        for (int i = 0; i < lists.Length; i++)
        {
            lists[i] = new LinkedList<int>();
        }

        // Skaiciu daznumas
        int number = 0;
        for (list.Beginning(); list.Exists(); list.Next())
        {
            number = (list.Take() / exp % 10);
            lists[number].Add(list.Take());
            list.Remove(list.TakeNode());        <---------- Removing node one by one till it's empty
        }

        for (int i = 0; i < lists.Length; i++)
        {
            for (list.Beginning(); list.Exists(); list.Next())
            {
                //list.Add(lists)
            }
        }
    }

    public static void Radix_sort(LinkedList<NumberPlate> list)
    {
        LinkedList<int> intList = new LinkedList<int>();
        AddIntToLinkedList(list, intList);
        for (int exp = 1; exp < Math.Pow(10, 9); exp *= 10)
        {
            Counting_sort(intList, exp);
        }
    }

LinkedList.cs

public class LinkedList<T> : IEnumerable<T> where T : IComparable<T>, IEquatable<T>
{
    public sealed class Node<T>
    {
        public Node<T> Right;
        public Node<T> Left;

        public T data;

        public Node(T value, Node<T> left, Node<T> right)
        {
            Left = left;
            Right = right;
            data = value;
        }
    }

    private Node<T> first;
    private Node<T> last;
    private Node<T> current;
    public int size;

    public LinkedList()
    {
        first = null;
        last = null;
        current = null;
        size = 0;
    }

    public void Add(T element)
    {
        var node = new Node<T>(element, last, null);

        if (first != null)
        {
            last.Right = node;
            last = node;
        }
        else
        {
            first = node;
            last = node;
        }
        current = node;
        size++;
    }

    public void Remove(Node<T> node)
    {
        if (node == first)
        {
            first = first.Right;

        }
        else if (node == last)
        {
            last = last.Left;
        }
        else if (node.Left != null)
        {
            node.Left.Right = node.Right;
        }
        else if (node.Right != null)
        {
            node.Right.Left = node.Left;
        }
        size--;
    }

    public void Beginning()
    {
        current = first;
    }

    public void End()
    {
        current = last;
    }

    public void Next()
    {
        current = current.Right;
    }

    public void Previous()
    {
        current = current.Left;
    }

    public bool Exists()
    {
        return current != null;
    }

    public T Take()
    {
        return current.data;
    }

    public Node<T> TakeNode()
    {
        return current;
    }
c# linked-list radix-sort
1个回答
0
投票

我认为您应该可以执行此操作以清除列表:

foreach (var node in list)
{
    list.Remove(node);
}

LinkedList类上已经为您实现了Remove方法,因此您应该使用它。

由于LinkedList实现IEnumerable,因此可以使用foreach对其进行迭代。

© www.soinside.com 2019 - 2024. All rights reserved.