Skip to content

added to Methods to the SinglyLinkedList (DeleteFirst, DeleteLast) #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}