Linked list implementation along with unit test [Round 2]












2












$begingroup$


Thank you Henrik Hansen for the amazing code review. Here is the new LinkedList class and accompanying unit test after suggestions made by Henrik Hansen.



Here's the link to the previous code review:
Linked list implementation along with unit test



Implementation



/*************************************************************************************************************
*
* Special Thanks to Henrik Hansen for the awesome code review!
* Url: https://codereview.stackexchange.com/questions/216453/linked-list-implementation-along-with-unit-test
*
*************************************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;

namespace DataStructuresAndAlgorithms.DataStructures
{
public class LinkedList<T> : IEnumerable<T>
{
class Node
{
public T Data { get; }
public Node Next { get; set; }
public Node Previous { get; set; }

public Node(T data)
{
Data = data;
}
}

private Node _head, _tail;
public T Head => _head == null ? throw new ArgumentNullException("Head is null") : _head.Data;
public T Tail => _tail == null ? throw new ArgumentNullException("Tail is null") : _tail.Data;

public LinkedList() { }

public LinkedList(IEnumerable<T> items)
{
foreach (var item in items)
{
AddTail(item);
}
}

public void AddHead(T item)
{
if (item == null)
throw new ArgumentNullException("AddHead: An item you are trying to add is not initialized!");

if (_head == null && _tail == null)
{
_head = new Node(item);
_tail = _head;
}
else
{
_head.Previous = new Node(item);
var temp = _head;
_head = _head.Previous;
_head.Next = temp;
}
}

public void AddTail(T item)
{
if (item == null)
throw new ArgumentNullException("AddTail: An item you are trying to add is not initialized!");

if (_tail == null && _head == null)
{
_tail = new Node(item);
_head = _tail;
}
else
{
_tail.Next = new Node(item);
var temp = _tail;
_tail = _tail.Next;
_tail.Previous = temp;
}
}

public void RemoveHead()
{
if (_head == null) return;
else
{
_head = _head.Next;
if (_head == null) return;
_head.Previous = null;
}
}

public void RemoveTail()
{
if (_tail == null) return;
else
{
_tail = _tail.Previous;
if (_tail == null) return;
_tail.Next = null;
}
}

public bool Remove(T item)
{
var pointer = _head;
while (pointer.Data.Equals(item) == false)
{
if (pointer.Next == null)
return false;
pointer = pointer.Next;
}

if (pointer.Previous == null)
{
// It is the Head
pointer.Next.Previous = null;
return true;
}
if (pointer.Next == null)
{
// It is the Tail
pointer.Previous.Next = null;
return true;
}
pointer.Previous.Next = null;
pointer.Next.Previous = null;
return true;
}

public IEnumerator<T> GetEnumerator()
{
var pointer = _head;
while (pointer != null)
{
yield return pointer.Data;
pointer = pointer.Next;
}
}

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


Unit Test



using System;
using Xunit;

using DataStructuresAndAlgorithms.DataStructures;

namespace DataStructuresAndAlgorithms.DataStructures.Tests
{
public class LinkedListTest
{
[Fact]
public void AddHead_Node_Should_Become_Head()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.AddHead(45);

// Assert
Assert.Equal(45, myLinkedList.Head);
}

[Fact]
public void AddTail_Node_Should_Become_Tail()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.AddTail(7777);

// Assert
Assert.Equal(7777, myLinkedList.Tail);
}

[Fact]
public void RemoveHead_Next_Node_Should_Be_Head()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.RemoveHead();

// Assert
Assert.Equal(2, myLinkedList.Head);
}

[Fact]
public void RemoveTail_Next_Node_Should_Be_Tail()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.RemoveTail();

// Assert
Assert.Equal(4, myLinkedList.Tail);
}
}
}


Presentation



using System;
using System.Collections;

using DataStructuresAndAlgorithms.DataStructures;

namespace DataStructuresAndAlgorithms.Presentation.Console
{
class Program
{
static void Main(string args)
{
RunLinkedList();
}

static void RunLinkedList()
{
System.Console.WriteLine("Running the LinkedList class");
System.Console.WriteLine("----------------------------");
var myLinkedList = new LinkedList<int>();
myLinkedList.AddHead(54);
myLinkedList.AddHead(44);
myLinkedList.AddHead(96);
myLinkedList.AddTail(300);
myLinkedList.AddTail(900);
myLinkedList.AddTail(77);
myLinkedList.Remove(900);

System.Console.WriteLine("HEAD = " + myLinkedList.Head);
System.Console.WriteLine("TAIL = " + myLinkedList.Tail);

foreach (var item in myLinkedList)
{
System.Console.WriteLine(item);
}
}
}
}
```









share|improve this question







New contributor




feature_creep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    I have found an error where it prints the wrong tail. I will be fixing this. This means I must be missing some tests.
    $endgroup$
    – feature_creep
    6 hours ago










  • $begingroup$
    I find it difficult to add valuable tests like public void Head_Should_Only_Have_Previous_Equal_NULL() and public void Tail_Should_Only_Have_Next_Equal_NULL() when my _head and _tail member variables are set to private.
    $endgroup$
    – feature_creep
    5 hours ago
















2












$begingroup$


Thank you Henrik Hansen for the amazing code review. Here is the new LinkedList class and accompanying unit test after suggestions made by Henrik Hansen.



Here's the link to the previous code review:
Linked list implementation along with unit test



Implementation



/*************************************************************************************************************
*
* Special Thanks to Henrik Hansen for the awesome code review!
* Url: https://codereview.stackexchange.com/questions/216453/linked-list-implementation-along-with-unit-test
*
*************************************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;

namespace DataStructuresAndAlgorithms.DataStructures
{
public class LinkedList<T> : IEnumerable<T>
{
class Node
{
public T Data { get; }
public Node Next { get; set; }
public Node Previous { get; set; }

public Node(T data)
{
Data = data;
}
}

private Node _head, _tail;
public T Head => _head == null ? throw new ArgumentNullException("Head is null") : _head.Data;
public T Tail => _tail == null ? throw new ArgumentNullException("Tail is null") : _tail.Data;

public LinkedList() { }

public LinkedList(IEnumerable<T> items)
{
foreach (var item in items)
{
AddTail(item);
}
}

public void AddHead(T item)
{
if (item == null)
throw new ArgumentNullException("AddHead: An item you are trying to add is not initialized!");

if (_head == null && _tail == null)
{
_head = new Node(item);
_tail = _head;
}
else
{
_head.Previous = new Node(item);
var temp = _head;
_head = _head.Previous;
_head.Next = temp;
}
}

public void AddTail(T item)
{
if (item == null)
throw new ArgumentNullException("AddTail: An item you are trying to add is not initialized!");

if (_tail == null && _head == null)
{
_tail = new Node(item);
_head = _tail;
}
else
{
_tail.Next = new Node(item);
var temp = _tail;
_tail = _tail.Next;
_tail.Previous = temp;
}
}

public void RemoveHead()
{
if (_head == null) return;
else
{
_head = _head.Next;
if (_head == null) return;
_head.Previous = null;
}
}

public void RemoveTail()
{
if (_tail == null) return;
else
{
_tail = _tail.Previous;
if (_tail == null) return;
_tail.Next = null;
}
}

public bool Remove(T item)
{
var pointer = _head;
while (pointer.Data.Equals(item) == false)
{
if (pointer.Next == null)
return false;
pointer = pointer.Next;
}

if (pointer.Previous == null)
{
// It is the Head
pointer.Next.Previous = null;
return true;
}
if (pointer.Next == null)
{
// It is the Tail
pointer.Previous.Next = null;
return true;
}
pointer.Previous.Next = null;
pointer.Next.Previous = null;
return true;
}

public IEnumerator<T> GetEnumerator()
{
var pointer = _head;
while (pointer != null)
{
yield return pointer.Data;
pointer = pointer.Next;
}
}

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


Unit Test



using System;
using Xunit;

using DataStructuresAndAlgorithms.DataStructures;

namespace DataStructuresAndAlgorithms.DataStructures.Tests
{
public class LinkedListTest
{
[Fact]
public void AddHead_Node_Should_Become_Head()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.AddHead(45);

// Assert
Assert.Equal(45, myLinkedList.Head);
}

[Fact]
public void AddTail_Node_Should_Become_Tail()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.AddTail(7777);

// Assert
Assert.Equal(7777, myLinkedList.Tail);
}

[Fact]
public void RemoveHead_Next_Node_Should_Be_Head()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.RemoveHead();

// Assert
Assert.Equal(2, myLinkedList.Head);
}

[Fact]
public void RemoveTail_Next_Node_Should_Be_Tail()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.RemoveTail();

// Assert
Assert.Equal(4, myLinkedList.Tail);
}
}
}


Presentation



using System;
using System.Collections;

using DataStructuresAndAlgorithms.DataStructures;

namespace DataStructuresAndAlgorithms.Presentation.Console
{
class Program
{
static void Main(string args)
{
RunLinkedList();
}

static void RunLinkedList()
{
System.Console.WriteLine("Running the LinkedList class");
System.Console.WriteLine("----------------------------");
var myLinkedList = new LinkedList<int>();
myLinkedList.AddHead(54);
myLinkedList.AddHead(44);
myLinkedList.AddHead(96);
myLinkedList.AddTail(300);
myLinkedList.AddTail(900);
myLinkedList.AddTail(77);
myLinkedList.Remove(900);

System.Console.WriteLine("HEAD = " + myLinkedList.Head);
System.Console.WriteLine("TAIL = " + myLinkedList.Tail);

foreach (var item in myLinkedList)
{
System.Console.WriteLine(item);
}
}
}
}
```









share|improve this question







New contributor




feature_creep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    I have found an error where it prints the wrong tail. I will be fixing this. This means I must be missing some tests.
    $endgroup$
    – feature_creep
    6 hours ago










  • $begingroup$
    I find it difficult to add valuable tests like public void Head_Should_Only_Have_Previous_Equal_NULL() and public void Tail_Should_Only_Have_Next_Equal_NULL() when my _head and _tail member variables are set to private.
    $endgroup$
    – feature_creep
    5 hours ago














2












2








2





$begingroup$


Thank you Henrik Hansen for the amazing code review. Here is the new LinkedList class and accompanying unit test after suggestions made by Henrik Hansen.



Here's the link to the previous code review:
Linked list implementation along with unit test



Implementation



/*************************************************************************************************************
*
* Special Thanks to Henrik Hansen for the awesome code review!
* Url: https://codereview.stackexchange.com/questions/216453/linked-list-implementation-along-with-unit-test
*
*************************************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;

namespace DataStructuresAndAlgorithms.DataStructures
{
public class LinkedList<T> : IEnumerable<T>
{
class Node
{
public T Data { get; }
public Node Next { get; set; }
public Node Previous { get; set; }

public Node(T data)
{
Data = data;
}
}

private Node _head, _tail;
public T Head => _head == null ? throw new ArgumentNullException("Head is null") : _head.Data;
public T Tail => _tail == null ? throw new ArgumentNullException("Tail is null") : _tail.Data;

public LinkedList() { }

public LinkedList(IEnumerable<T> items)
{
foreach (var item in items)
{
AddTail(item);
}
}

public void AddHead(T item)
{
if (item == null)
throw new ArgumentNullException("AddHead: An item you are trying to add is not initialized!");

if (_head == null && _tail == null)
{
_head = new Node(item);
_tail = _head;
}
else
{
_head.Previous = new Node(item);
var temp = _head;
_head = _head.Previous;
_head.Next = temp;
}
}

public void AddTail(T item)
{
if (item == null)
throw new ArgumentNullException("AddTail: An item you are trying to add is not initialized!");

if (_tail == null && _head == null)
{
_tail = new Node(item);
_head = _tail;
}
else
{
_tail.Next = new Node(item);
var temp = _tail;
_tail = _tail.Next;
_tail.Previous = temp;
}
}

public void RemoveHead()
{
if (_head == null) return;
else
{
_head = _head.Next;
if (_head == null) return;
_head.Previous = null;
}
}

public void RemoveTail()
{
if (_tail == null) return;
else
{
_tail = _tail.Previous;
if (_tail == null) return;
_tail.Next = null;
}
}

public bool Remove(T item)
{
var pointer = _head;
while (pointer.Data.Equals(item) == false)
{
if (pointer.Next == null)
return false;
pointer = pointer.Next;
}

if (pointer.Previous == null)
{
// It is the Head
pointer.Next.Previous = null;
return true;
}
if (pointer.Next == null)
{
// It is the Tail
pointer.Previous.Next = null;
return true;
}
pointer.Previous.Next = null;
pointer.Next.Previous = null;
return true;
}

public IEnumerator<T> GetEnumerator()
{
var pointer = _head;
while (pointer != null)
{
yield return pointer.Data;
pointer = pointer.Next;
}
}

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


Unit Test



using System;
using Xunit;

using DataStructuresAndAlgorithms.DataStructures;

namespace DataStructuresAndAlgorithms.DataStructures.Tests
{
public class LinkedListTest
{
[Fact]
public void AddHead_Node_Should_Become_Head()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.AddHead(45);

// Assert
Assert.Equal(45, myLinkedList.Head);
}

[Fact]
public void AddTail_Node_Should_Become_Tail()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.AddTail(7777);

// Assert
Assert.Equal(7777, myLinkedList.Tail);
}

[Fact]
public void RemoveHead_Next_Node_Should_Be_Head()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.RemoveHead();

// Assert
Assert.Equal(2, myLinkedList.Head);
}

[Fact]
public void RemoveTail_Next_Node_Should_Be_Tail()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.RemoveTail();

// Assert
Assert.Equal(4, myLinkedList.Tail);
}
}
}


Presentation



using System;
using System.Collections;

using DataStructuresAndAlgorithms.DataStructures;

namespace DataStructuresAndAlgorithms.Presentation.Console
{
class Program
{
static void Main(string args)
{
RunLinkedList();
}

static void RunLinkedList()
{
System.Console.WriteLine("Running the LinkedList class");
System.Console.WriteLine("----------------------------");
var myLinkedList = new LinkedList<int>();
myLinkedList.AddHead(54);
myLinkedList.AddHead(44);
myLinkedList.AddHead(96);
myLinkedList.AddTail(300);
myLinkedList.AddTail(900);
myLinkedList.AddTail(77);
myLinkedList.Remove(900);

System.Console.WriteLine("HEAD = " + myLinkedList.Head);
System.Console.WriteLine("TAIL = " + myLinkedList.Tail);

foreach (var item in myLinkedList)
{
System.Console.WriteLine(item);
}
}
}
}
```









share|improve this question







New contributor




feature_creep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




Thank you Henrik Hansen for the amazing code review. Here is the new LinkedList class and accompanying unit test after suggestions made by Henrik Hansen.



Here's the link to the previous code review:
Linked list implementation along with unit test



Implementation



/*************************************************************************************************************
*
* Special Thanks to Henrik Hansen for the awesome code review!
* Url: https://codereview.stackexchange.com/questions/216453/linked-list-implementation-along-with-unit-test
*
*************************************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;

namespace DataStructuresAndAlgorithms.DataStructures
{
public class LinkedList<T> : IEnumerable<T>
{
class Node
{
public T Data { get; }
public Node Next { get; set; }
public Node Previous { get; set; }

public Node(T data)
{
Data = data;
}
}

private Node _head, _tail;
public T Head => _head == null ? throw new ArgumentNullException("Head is null") : _head.Data;
public T Tail => _tail == null ? throw new ArgumentNullException("Tail is null") : _tail.Data;

public LinkedList() { }

public LinkedList(IEnumerable<T> items)
{
foreach (var item in items)
{
AddTail(item);
}
}

public void AddHead(T item)
{
if (item == null)
throw new ArgumentNullException("AddHead: An item you are trying to add is not initialized!");

if (_head == null && _tail == null)
{
_head = new Node(item);
_tail = _head;
}
else
{
_head.Previous = new Node(item);
var temp = _head;
_head = _head.Previous;
_head.Next = temp;
}
}

public void AddTail(T item)
{
if (item == null)
throw new ArgumentNullException("AddTail: An item you are trying to add is not initialized!");

if (_tail == null && _head == null)
{
_tail = new Node(item);
_head = _tail;
}
else
{
_tail.Next = new Node(item);
var temp = _tail;
_tail = _tail.Next;
_tail.Previous = temp;
}
}

public void RemoveHead()
{
if (_head == null) return;
else
{
_head = _head.Next;
if (_head == null) return;
_head.Previous = null;
}
}

public void RemoveTail()
{
if (_tail == null) return;
else
{
_tail = _tail.Previous;
if (_tail == null) return;
_tail.Next = null;
}
}

public bool Remove(T item)
{
var pointer = _head;
while (pointer.Data.Equals(item) == false)
{
if (pointer.Next == null)
return false;
pointer = pointer.Next;
}

if (pointer.Previous == null)
{
// It is the Head
pointer.Next.Previous = null;
return true;
}
if (pointer.Next == null)
{
// It is the Tail
pointer.Previous.Next = null;
return true;
}
pointer.Previous.Next = null;
pointer.Next.Previous = null;
return true;
}

public IEnumerator<T> GetEnumerator()
{
var pointer = _head;
while (pointer != null)
{
yield return pointer.Data;
pointer = pointer.Next;
}
}

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


Unit Test



using System;
using Xunit;

using DataStructuresAndAlgorithms.DataStructures;

namespace DataStructuresAndAlgorithms.DataStructures.Tests
{
public class LinkedListTest
{
[Fact]
public void AddHead_Node_Should_Become_Head()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.AddHead(45);

// Assert
Assert.Equal(45, myLinkedList.Head);
}

[Fact]
public void AddTail_Node_Should_Become_Tail()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.AddTail(7777);

// Assert
Assert.Equal(7777, myLinkedList.Tail);
}

[Fact]
public void RemoveHead_Next_Node_Should_Be_Head()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.RemoveHead();

// Assert
Assert.Equal(2, myLinkedList.Head);
}

[Fact]
public void RemoveTail_Next_Node_Should_Be_Tail()
{
// Arrange
int myNums = { 1, 2, 3, 4, 5 };
var myLinkedList = new LinkedList<int>(myNums);

// Act
myLinkedList.RemoveTail();

// Assert
Assert.Equal(4, myLinkedList.Tail);
}
}
}


Presentation



using System;
using System.Collections;

using DataStructuresAndAlgorithms.DataStructures;

namespace DataStructuresAndAlgorithms.Presentation.Console
{
class Program
{
static void Main(string args)
{
RunLinkedList();
}

static void RunLinkedList()
{
System.Console.WriteLine("Running the LinkedList class");
System.Console.WriteLine("----------------------------");
var myLinkedList = new LinkedList<int>();
myLinkedList.AddHead(54);
myLinkedList.AddHead(44);
myLinkedList.AddHead(96);
myLinkedList.AddTail(300);
myLinkedList.AddTail(900);
myLinkedList.AddTail(77);
myLinkedList.Remove(900);

System.Console.WriteLine("HEAD = " + myLinkedList.Head);
System.Console.WriteLine("TAIL = " + myLinkedList.Tail);

foreach (var item in myLinkedList)
{
System.Console.WriteLine(item);
}
}
}
}
```






c# beginner linked-list unit-testing






share|improve this question







New contributor




feature_creep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




feature_creep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




feature_creep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 6 hours ago









feature_creepfeature_creep

435




435




New contributor




feature_creep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





feature_creep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






feature_creep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    I have found an error where it prints the wrong tail. I will be fixing this. This means I must be missing some tests.
    $endgroup$
    – feature_creep
    6 hours ago










  • $begingroup$
    I find it difficult to add valuable tests like public void Head_Should_Only_Have_Previous_Equal_NULL() and public void Tail_Should_Only_Have_Next_Equal_NULL() when my _head and _tail member variables are set to private.
    $endgroup$
    – feature_creep
    5 hours ago


















  • $begingroup$
    I have found an error where it prints the wrong tail. I will be fixing this. This means I must be missing some tests.
    $endgroup$
    – feature_creep
    6 hours ago










  • $begingroup$
    I find it difficult to add valuable tests like public void Head_Should_Only_Have_Previous_Equal_NULL() and public void Tail_Should_Only_Have_Next_Equal_NULL() when my _head and _tail member variables are set to private.
    $endgroup$
    – feature_creep
    5 hours ago
















$begingroup$
I have found an error where it prints the wrong tail. I will be fixing this. This means I must be missing some tests.
$endgroup$
– feature_creep
6 hours ago




$begingroup$
I have found an error where it prints the wrong tail. I will be fixing this. This means I must be missing some tests.
$endgroup$
– feature_creep
6 hours ago












$begingroup$
I find it difficult to add valuable tests like public void Head_Should_Only_Have_Previous_Equal_NULL() and public void Tail_Should_Only_Have_Next_Equal_NULL() when my _head and _tail member variables are set to private.
$endgroup$
– feature_creep
5 hours ago




$begingroup$
I find it difficult to add valuable tests like public void Head_Should_Only_Have_Previous_Equal_NULL() and public void Tail_Should_Only_Have_Next_Equal_NULL() when my _head and _tail member variables are set to private.
$endgroup$
– feature_creep
5 hours ago










1 Answer
1






active

oldest

votes


















0












$begingroup$


  • Remove... family methods fail with an exception if from the list is of size 1. pointer.Previous == null does not mean that pointer.Next is not.


  • It is quite important to convey a success/failure of the removal to the caller. Remove should return at least a boolean.


  • I am not sure why do you disallow a Node having null data. In any case, if you want to enforce it, do it in a Node constructor, rather than in insertion methods.



  • AddHead should be streamlined. After all, the new Node is created in both branches, and becomes head no matter what. Lift the common functionality out:



    public void AddHead(T item)
    {
    var node = new Node(item);
    node.Next = _head;

    if (_head == null && _tail == null)
    {
    _tail = node;
    }
    else
    {
    _head.Previous = node;
    }
    _head = node;
    }


    Ditto for AddTail.



  • A while (pointer.Data.Equals(item) == false) loop deserves to become a Find method.







share|improve this answer









$endgroup$














    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    feature_creep is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216503%2flinked-list-implementation-along-with-unit-test-round-2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0












    $begingroup$


    • Remove... family methods fail with an exception if from the list is of size 1. pointer.Previous == null does not mean that pointer.Next is not.


    • It is quite important to convey a success/failure of the removal to the caller. Remove should return at least a boolean.


    • I am not sure why do you disallow a Node having null data. In any case, if you want to enforce it, do it in a Node constructor, rather than in insertion methods.



    • AddHead should be streamlined. After all, the new Node is created in both branches, and becomes head no matter what. Lift the common functionality out:



      public void AddHead(T item)
      {
      var node = new Node(item);
      node.Next = _head;

      if (_head == null && _tail == null)
      {
      _tail = node;
      }
      else
      {
      _head.Previous = node;
      }
      _head = node;
      }


      Ditto for AddTail.



    • A while (pointer.Data.Equals(item) == false) loop deserves to become a Find method.







    share|improve this answer









    $endgroup$


















      0












      $begingroup$


      • Remove... family methods fail with an exception if from the list is of size 1. pointer.Previous == null does not mean that pointer.Next is not.


      • It is quite important to convey a success/failure of the removal to the caller. Remove should return at least a boolean.


      • I am not sure why do you disallow a Node having null data. In any case, if you want to enforce it, do it in a Node constructor, rather than in insertion methods.



      • AddHead should be streamlined. After all, the new Node is created in both branches, and becomes head no matter what. Lift the common functionality out:



        public void AddHead(T item)
        {
        var node = new Node(item);
        node.Next = _head;

        if (_head == null && _tail == null)
        {
        _tail = node;
        }
        else
        {
        _head.Previous = node;
        }
        _head = node;
        }


        Ditto for AddTail.



      • A while (pointer.Data.Equals(item) == false) loop deserves to become a Find method.







      share|improve this answer









      $endgroup$
















        0












        0








        0





        $begingroup$


        • Remove... family methods fail with an exception if from the list is of size 1. pointer.Previous == null does not mean that pointer.Next is not.


        • It is quite important to convey a success/failure of the removal to the caller. Remove should return at least a boolean.


        • I am not sure why do you disallow a Node having null data. In any case, if you want to enforce it, do it in a Node constructor, rather than in insertion methods.



        • AddHead should be streamlined. After all, the new Node is created in both branches, and becomes head no matter what. Lift the common functionality out:



          public void AddHead(T item)
          {
          var node = new Node(item);
          node.Next = _head;

          if (_head == null && _tail == null)
          {
          _tail = node;
          }
          else
          {
          _head.Previous = node;
          }
          _head = node;
          }


          Ditto for AddTail.



        • A while (pointer.Data.Equals(item) == false) loop deserves to become a Find method.







        share|improve this answer









        $endgroup$




        • Remove... family methods fail with an exception if from the list is of size 1. pointer.Previous == null does not mean that pointer.Next is not.


        • It is quite important to convey a success/failure of the removal to the caller. Remove should return at least a boolean.


        • I am not sure why do you disallow a Node having null data. In any case, if you want to enforce it, do it in a Node constructor, rather than in insertion methods.



        • AddHead should be streamlined. After all, the new Node is created in both branches, and becomes head no matter what. Lift the common functionality out:



          public void AddHead(T item)
          {
          var node = new Node(item);
          node.Next = _head;

          if (_head == null && _tail == null)
          {
          _tail = node;
          }
          else
          {
          _head.Previous = node;
          }
          _head = node;
          }


          Ditto for AddTail.



        • A while (pointer.Data.Equals(item) == false) loop deserves to become a Find method.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 4 hours ago









        vnpvnp

        40.5k233103




        40.5k233103






















            feature_creep is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            feature_creep is a new contributor. Be nice, and check out our Code of Conduct.













            feature_creep is a new contributor. Be nice, and check out our Code of Conduct.












            feature_creep is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216503%2flinked-list-implementation-along-with-unit-test-round-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

            is 'sed' thread safe

            How to make a Squid Proxy server?