Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does c#/.net x.x have an implementation of a doubly linked list (that can be iterated over backwards)?

I've been searching for the standard implementation of a doubly linked list in c# (so that I have a linked list I can iterate over backwards) and cannot find one. I feel like something so simple must have an implementation that I'm just missing.

If it does exist, for which version of c#/.net does it exist?

Reverse iteration in general seems to be something not intended to be done in c#. Is my mind just stuck too much in c++/stl mode or is this something sorely lacking in c#?

I'm aware of LinkedList but in failing to find a way to iterate over it backwards had assumed it was singly linked.

If LinkedList is doubly linked how does one go about iterating over it backwards (Efficiently)?

like image 258
Catskul Avatar asked Jan 26 '10 00:01

Catskul


People also ask

Does hep C go away?

Hepatitis C virus (HCV) causes both acute and chronic infection. Acute HCV infections are usually asymptomatic and most do not lead to a life-threatening disease. Around 30% (15–45%) of infected persons spontaneously clear the virus within 6 months of infection without any treatment.

What is the main cause of hep C?

Hepatitis C is a liver infection caused by the hepatitis C virus (HCV). Hepatitis C is spread through contact with blood from an infected person. Today, most people become infected with the hepatitis C virus by sharing needles or other equipment used to prepare and inject drugs.

What does hep C pain feel like?

Many people with chronic HCV suffer from aches and pains in their joints. A variety of different joints can be involved but the most common are in the hands and wrists. These pains are often minor but occasionally the pain can be quite severe. In such cases painkillers can be used to relieve the symptoms.

How long can you have hep C without knowing?

People with an HCV infection commonly go without noticeable symptoms for as many as 20 to 30 years. Those who are infected experience no significant symptoms when they first acquire the infection, and then they can remain symptomless for years, even while the infection is causing damage to their liver and other organs.


2 Answers

The following code will efficiently iterate over a LinkedList in reverse:

        LinkedList<string> list = new LinkedList<string>
            (new[] {"cat", "dog", "frog", "antelope", "gazelle"});
        LinkedListNode<string> item = list.Last;
        do
        {
            Console.WriteLine(item.Value);
            item = item.Previous;
        }
        while (item != null);
        Console.ReadKey();

The key here is that a LinkedList contains reference to only the First and Last LinkedListNode instances of the list. Each LinkedListNode instance holds a reference to the next and previous item in the list (or null at each end of the list) and also a Value property. This means iteration from the first or last LinkedListNode is easy, but random access requires iteration from the first or last in the list.

If you need to make an insertion along the way, use LinkedList.AddBefore or AddAfter to insert a new LinkedListNode.

like image 157
spender Avatar answered Nov 07 '22 10:11

spender


As well as the answers given here, you can write an extension method to LinkedList<T> to make it slightly easier to reuse:

public static IEnumerable<T> Backwards<T>(this LinkedList<T> list)
{
    LinkedListNode<T> node= list.Last;
    while (node != null)
    {
        yield return node.Value;
        node = node.Previous;
    }
}

Use with:

foreach (string x in list.Backwards())
{
    // ...
}
like image 36
Jon Skeet Avatar answered Nov 07 '22 11:11

Jon Skeet