-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Added Circular Linked List Data Structure #476
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
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
610a77d
Added StackUtils.cs - This utility provides commonly used operations …
mohit-gogitter ae19f20
Modified StackUtils.cs as per Codacy code analysis report
mohit-gogitter c513a1c
Removed redundant parenthesis from StackUtils.cs file and modified RE…
mohit-gogitter 39f0a66
Removed StackUtils.cs and implemented them in seperate classes. Added…
mohit-gogitter 03b60ad
Moved to Algorithms and updated README.md
mohit-gogitter 64fe596
Added tests, updated document comments and resolved build and codacy …
mohit-gogitter a6732f9
resolved codacy issues
mohit-gogitter 85370d7
Merge branch 'TheAlgorithms:master' into master
mohit-gogitter b820941
Added Circular Linked List Data Structure
mohit-gogitter d99a422
Merge branch 'master' of https://github.com/mohit-gogitter/C-Sharp
mohit-gogitter 9684551
Codacy issue resolution and added test cases for codecov
mohit-gogitter 70c3d74
added test cases for codecov
mohit-gogitter 3d28581
replaced console outputs with exception
mohit-gogitter e50aed7
removed display method and related test cases
mohit-gogitter 75f9233
added test case for codecov
mohit-gogitter 9bd2413
removed unreachable code
mohit-gogitter 7580b7a
Merge branch 'master' into master
mohit-gogitter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
208 changes: 208 additions & 0 deletions
208
DataStructures.Tests/LinkedList/CircularLinkedListTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
using System; | ||
using DataStructures.LinkedList.CircularLinkedList; | ||
using Microsoft.VisualStudio.TestPlatform.Utilities; | ||
using NUnit.Framework; | ||
|
||
namespace DataStructures.Tests.LinkedList; | ||
|
||
[TestFixture] | ||
public static class CircularLinkedListTests | ||
{ | ||
[Test] | ||
public static void TestDisplay() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
cll.InsertAtEnd(20); | ||
cll.InsertAtEnd(30); | ||
|
||
Assert.That("10 20 30", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestDisplayEmptyList() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.Display(); | ||
|
||
Assert.That("List is empty.", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestInsertAtBeginning() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtBeginning(10); | ||
cll.InsertAtBeginning(20); | ||
cll.InsertAtBeginning(30); | ||
|
||
Assert.That("30 20 10", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestInsertAtEnd() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
cll.InsertAtEnd(20); | ||
cll.InsertAtEnd(30); | ||
|
||
Assert.That("10 20 30", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestInsertAfter() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
cll.InsertAtEnd(20); | ||
cll.InsertAtEnd(30); | ||
|
||
cll.InsertAfter(20, 25); | ||
Assert.That("10 20 25 30", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestInsertAtBeginningInEmptyList() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtBeginning(10); | ||
Assert.That("10", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestInsertAtEndInEmptyList() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
|
||
Assert.That("10", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestInsertAfterInEmptyList() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAfter(10, 20); | ||
|
||
Assert.That("List is empty.", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
|
||
|
||
[Test] | ||
public static void TestInsertAfterSpecificNode() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
cll.InsertAtEnd(20); | ||
cll.InsertAtEnd(30); | ||
|
||
cll.InsertAfter(20, 25); // Insert after node with value 20 | ||
Assert.That("10 20 25 30", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestInsertAfterOnNonExistingValue() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
cll.InsertAfter(99, 25); // 99 does not exist | ||
Assert.That("10", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestDeleteNode() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
cll.InsertAtEnd(20); | ||
cll.InsertAtEnd(30); | ||
|
||
cll.DeleteNode(20); | ||
Assert.That("10 30", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestDeleteOnlyNode() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtBeginning(10); | ||
cll.DeleteNode(10); | ||
Assert.That(cll.IsEmpty(), Is.EqualTo(true)); | ||
} | ||
|
||
[Test] | ||
public static void TestDeleteHeadNode() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
cll.InsertAtEnd(20); | ||
cll.InsertAtEnd(30); | ||
cll.DeleteNode(10); | ||
Assert.That("20 30", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestDeleteTailNode() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
cll.InsertAtEnd(20); | ||
cll.InsertAtEnd(30); | ||
cll.DeleteNode(30); | ||
Assert.That("10 20", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
[Test] | ||
public static void TestDeleteFromEmptyList() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.DeleteNode(10); | ||
Assert.That(cll.IsEmpty(), Is.EqualTo(true)); | ||
} | ||
|
||
[Test] | ||
public static void TestDeleteNonExistentNode() | ||
{ | ||
var cll = new CircularLinkedList<int>(); | ||
cll.InsertAtEnd(10); | ||
cll.InsertAtEnd(20); | ||
cll.InsertAtEnd(30); | ||
|
||
cll.DeleteNode(40); // Attempting to delete a node that doesn't exist | ||
Assert.That("10 20 30", Is.EqualTo(GetDisplayOutput(cll).Trim())); | ||
} | ||
|
||
/// <summary> | ||
/// Helper method to capture the output of the Display method for assertions. | ||
/// </summary> | ||
/// <param name="list">The CircularLinkedList instance.</param> | ||
/// <returns>A string representation of the list.</returns> | ||
private static string GetDisplayOutput(CircularLinkedList<int> list) | ||
{ | ||
// Save the original output (the default Console output stream) | ||
var originalConsoleOut = Console.Out; | ||
|
||
// Use a StringWriter to capture Console output | ||
using (var sw = new System.IO.StringWriter()) | ||
{ | ||
try | ||
{ | ||
// Redirect Console output to StringWriter | ||
Console.SetOut(sw); | ||
|
||
// Call the method that outputs to the console | ||
list.Display(); | ||
|
||
// Return the captured output | ||
return sw.ToString(); | ||
} | ||
finally | ||
{ | ||
// Restore the original Console output stream | ||
Console.SetOut(originalConsoleOut); | ||
} | ||
} | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
DataStructures/LinkedList/CircularLinkedList/CircularLinkedList.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
using System; | ||
|
||
namespace DataStructures.LinkedList.CircularLinkedList | ||
{ | ||
/// <summary> | ||
/// CircularLinkedList. | ||
/// @author Mohit Singh. <a href="https://github.com/mohit-gogitter">mohit-gogitter</a> | ||
/// </summary> | ||
/// <typeparam name="T">The generic type parameter.</typeparam> | ||
public class CircularLinkedList<T> | ||
{ | ||
/// <summary> | ||
/// Points to the last node in the Circular Linked List. | ||
/// </summary> | ||
private CircularLinkedListNode<T>? tail; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CircularLinkedList{T}"/> class. | ||
/// </summary> | ||
public CircularLinkedList() | ||
{ | ||
tail = null; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the Circular Linked List is empty. | ||
/// </summary> | ||
/// <returns>True if the list is empty; otherwise, false.</returns> | ||
public bool IsEmpty() | ||
{ | ||
return tail == null; | ||
} | ||
|
||
/// <summary> | ||
/// Inserts a new node at the beginning of the Circular Linked List. | ||
/// </summary> | ||
/// <param name="data">The data to insert into the new node.</param> | ||
public void InsertAtBeginning(T data) | ||
{ | ||
var newNode = new CircularLinkedListNode<T>(data); | ||
if (IsEmpty()) | ||
{ | ||
tail = newNode; | ||
tail.Next = tail; | ||
} | ||
else | ||
{ | ||
newNode.Next = tail!.Next; | ||
tail.Next = newNode; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Inserts a new node at the end of the Circular Linked List. | ||
/// </summary> | ||
/// <param name="data">The data to insert into the new node.</param> | ||
public void InsertAtEnd(T data) | ||
{ | ||
var newNode = new CircularLinkedListNode<T>(data); | ||
if (IsEmpty()) | ||
{ | ||
tail = newNode; | ||
tail.Next = tail; | ||
} | ||
else | ||
{ | ||
newNode.Next = tail!.Next; | ||
tail.Next = newNode; | ||
tail = newNode; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Inserts a new node after a specific value in the list. | ||
/// </summary> | ||
/// <param name="value">The value to insert the node after.</param> | ||
/// <param name="data">The data to insert into the new node.</param> | ||
public void InsertAfter(T value, T data) | ||
{ | ||
if (IsEmpty()) | ||
{ | ||
Console.WriteLine("List is empty."); | ||
return; | ||
} | ||
|
||
var current = tail!.Next; | ||
do | ||
{ | ||
if (current!.Data!.Equals(value)) | ||
{ | ||
var newNode = new CircularLinkedListNode<T>(data); | ||
newNode.Next = current.Next; | ||
current.Next = newNode; | ||
|
||
if (current == tail) | ||
{ | ||
tail = newNode; | ||
} | ||
|
||
return; | ||
} | ||
|
||
current = current.Next; | ||
} | ||
while (current != tail.Next); | ||
|
||
Console.WriteLine($"Node with value {value} not found."); | ||
} | ||
|
||
/// <summary> | ||
/// Deletes a node with a specific value from the list. | ||
/// </summary> | ||
/// <param name="value">The value of the node to delete.</param> | ||
public void DeleteNode(T value) | ||
{ | ||
if (IsEmpty()) | ||
{ | ||
Console.WriteLine("List is empty."); | ||
return; | ||
} | ||
|
||
var current = tail!.Next; | ||
var previous = tail; | ||
|
||
do | ||
{ | ||
if (current!.Data!.Equals(value)) | ||
{ | ||
if (current == tail && current.Next == tail) | ||
{ | ||
tail = null; | ||
} | ||
else if (current == tail) | ||
{ | ||
previous!.Next = tail.Next; | ||
tail = previous; | ||
} | ||
else if (current == tail.Next) | ||
{ | ||
tail.Next = current.Next; | ||
} | ||
else | ||
{ | ||
previous!.Next = current.Next; | ||
} | ||
|
||
Console.WriteLine($"Node with value {value} deleted."); | ||
return; | ||
} | ||
|
||
previous = current; | ||
current = current.Next; | ||
} | ||
while (current != tail!.Next); | ||
|
||
Console.WriteLine($"Node with value {value} not found."); | ||
} | ||
|
||
/// <summary> | ||
/// Displays the contents of the Circular Linked List. | ||
/// </summary> | ||
public void Display() | ||
siriak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
if (IsEmpty()) | ||
{ | ||
Console.WriteLine("List is empty."); | ||
return; | ||
} | ||
|
||
CircularLinkedListNode<T>? current = tail!.Next; | ||
do | ||
{ | ||
Console.Write($"{current!.Data} "); | ||
current = current.Next; | ||
} | ||
while (current != tail!.Next); | ||
|
||
Console.WriteLine(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.