Click here to Skip to main content
15,615,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
can somebody please give me some feedback for my doubly linked list?
I want to know if I work correctly with reference data types and generic.
I will be really glad if somebody show me how to write reverse metod for my doubly linked list.

What I have tried:

C#
using System;
using System.Collections;
using System.Collections.Generic;

namespace CreateDoublyLinkedList
{
    public class DoublyLinkedList<T> : IEnumerable<T>
    {
        public DoublyLinkedList()
        {
            this.Head = this.Tail = default;
            this.Count = 0;
        }

        private class Node
        {
            public Node(Node previousNode, T value, Node nextNode)
            {
                this.PreviousNode = previousNode;
                this.Value = value;
                this.NextNode = nextNode;
            }

            public Node PreviousNode { get; set; }
            public T  Value { get; set; }
            public Node NextNode { get; set; }
        }
        private Node Head { get; set; }
        private Node Tail { get; set; }
        public int Count { get; private set; }

        public void AddFirst(T element)
        {
            if (this.Count == 0)
            {
                this.Head = this.Tail = new Node(null, element, null);
                this.Count++;
            }
            else
            {
                var newNode = new Node(null, element, this.Head);
                this.Head.PreviousNode = newNode;
                this.Head = newNode;
                this.Count++;
            }          
        }

        public void AddLast(T element)
        {
            if (this.Count == 0)
            {
                this.Head = this.Tail = new Node(null, element, null);
                this.Count++;
            }
            else
            {
                var newNode = new Node(this.Tail, element, null);
                this.Tail.NextNode = newNode;
                this.Tail = newNode;
                this.Count++;
            }           
        }

        public T RemoveFirst()
        {
            if (this.Count == 0)
            {
                throw new Exception("Doubly list is empty!");
            }
            var firstNode = this.Head;
            var secondNode = this.Head.NextNode;
            secondNode.PreviousNode = null;
            firstNode.NextNode = null;
            this.Head = secondNode;
            this.Count--;
            return firstNode.Value;          
        }

        public T RemoveLast()
        {
            if (this.Count == 0)
            {
                throw new Exception("Doubly list is empty!");
            }
            var lastNode = this.Tail;
            var previousNode = this.Tail.PreviousNode;
            previousNode.NextNode = null;
            lastNode.PreviousNode = null;
            this.Tail = previousNode;
            this.Count--;
            return lastNode.Value;
        }

        public IEnumerator<T> GetEnumerator()
        {
            var currentNode = this.Head;
            for (int i = 0; i < this.Count; i++)
            {
                yield return currentNode.Value;
                currentNode = currentNode.NextNode;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public T[] ToArray()
        {
            var newArray = new T[this.Count];
            var currentNode = this.Head;
            for (int i = 0; i <= newArray.Length-1; i++)
            {
                newArray[i] = currentNode.Value;
                currentNode = currentNode.NextNode;
            }
            return newArray;
        }
    }
}
Posted
Updated 17-Feb-21 20:42pm
v2

I did some simple tests, and it seems ok.
var myDoubleListString = new DoublyLinkedList<string>();
var myDoubleListInt = new DoublyLinkedList<int>();
var myDoubleListObj = new DoublyLinkedList<object>();

myDoubleListString.AddFirst("One");
myDoubleListString.AddLast("Two");
myDoubleListString.AddLast("Three");

if (myDoubleListString.Count > 0)
{
    var last = myDoubleListString.Tail;
    var head = myDoubleListString.Head;
    var two = head.NextNode;
    var three = two.NextNode;
    var threePrevious = three.PreviousNode;
}

myDoubleListInt.AddFirst(1);
myDoubleListObj.AddFirst("Object 1");

var myArray1 = myDoubleListString.ToArray();
var myArray2 = myDoubleListInt.ToArray();
var myArray3 = myDoubleListObj.ToArray();

Debug.Print("length = " + myArray1.Length);
Debug.Print("length = " + myArray2.Length);
Debug.Print("length = " + myArray3.Length);

// Walk the nodes in reverse
var lastNode = myDoubleListString.Tail;

while (lastNode != null)
{
    Debug.Print(lastNode.Value);
    lastNode = lastNode.PreviousNode;
}

To get my test working, I had to change the access modifiers to public of:
public class Node
public Node Head
public Node Tail
 
Share this answer
 
v3
Comments
Elena Popova 18-Feb-21 3:13am    
Thank you very much!
Best
Quote:
can somebody please give me some feedback for my doubly linked list?
I want to know if I work correctly with reference data types and generic.

Basically, it is your job to test your production.
Whatever the method your choose, the principle, you build some scenario to check if it works in normal conditions and to challenge your code to see your how the code handle situation.
When your code pass your tests, chances are that the code is correct.

Unit testing - Wikipedia[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900