Skip to content

Commit

Permalink
Add DeleteFirst and DeleteLast to SinglyLinkedList (TheAlgorithms#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozzy-ZY authored Apr 19, 2024
1 parent c9e2fa1 commit f32c043
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
67 changes: 67 additions & 0 deletions DataStructures.Tests/LinkedList/LinkedListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,71 @@ public static void RemoveItemsFromList()
Assert.That(l3RemoveSucess, Is.False);
Assert.That(nonExistantRemoveSucess, Is.False);
}

[Test]
public static void DeleteFirstFromList()
{
// Arrange
var testObj = new SinglyLinkedList<string>();
_ = testObj.AddLast("H");
_ = testObj.AddLast("E");
_ = testObj.AddLast("L");
_ = testObj.AddLast("L");
_ = testObj.AddLast("O");

// Act
var deleteSuccess = testObj.DeleteFirst();

// Assert
Assert.That(deleteSuccess, Is.True);
Assert.That(4, Is.EqualTo(testObj.Length()));
Assert.That("E", Is.EqualTo(testObj.GetElementByIndex(0)));
}

[Test]
public static void DeleteFirstFromEmptyList()
{
// Arrange
var testObj = new SinglyLinkedList<string>();

// Act
var deleteSuccess = testObj.DeleteFirst();

// Assert
Assert.That(deleteSuccess, Is.False);
}

[Test]
public static void DeleteLastFromList()
{
// Arrange
var testObj = new SinglyLinkedList<string>();
_ = testObj.AddLast("H");
_ = testObj.AddLast("E");
_ = testObj.AddLast("L");
_ = testObj.AddLast("L");
_ = testObj.AddLast("O");

// Act
var deleteSuccess = testObj.DeleteLast();

// Assert
Assert.That(deleteSuccess, Is.True);
Assert.That(4, Is.EqualTo(testObj.Length()));
Assert.That("L", Is.EqualTo(testObj.GetElementByIndex(testObj.Length() - 1)));
}

[Test]
public static void DeleteLastFromEmptyList()
{
// Arrange
var testObj = new SinglyLinkedList<string>();

// Act
var deleteSuccess = testObj.DeleteLast();

// Assert
Assert.That(deleteSuccess, Is.False);
}

}
47 changes: 47 additions & 0 deletions DataStructures/LinkedList/SinglyLinkedList/SinglyLinkedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,51 @@ public bool DeleteElement(T element)

return false;
}

/// <summary>
/// Deletes the first element of the list.
/// </summary>
/// <returns> true if the operation is successul.</returns>
public bool DeleteFirst()
{
// checks if the List is empty
if(Head is null)
{
return false;
}

// if not, the head is overwritten with the next element and the old head is deleted
Head = Head.Next;
return true;
}

/// <summary>
/// Deletes the last element of the list.
/// </summary>
/// <returns> returns true if the operation is successful. </returns>
public bool DeleteLast()
{
// checks if the List is empty
if(Head is null)
{
return false;
}

// checks if the List has only one element
if(Head.Next is null)
{
Head = null;
return true;
}

// if not, iterates through the list to the second last element and deletes the last one
SinglyLinkedListNode<T>? secondlast = Head;
while(secondlast.Next?.Next is not null)
{
secondlast = secondlast.Next;
}

secondlast.Next = null;
return true;
}
}

0 comments on commit f32c043

Please sign in to comment.