Skip to content

Commit e50aed7

Browse files
removed display method and related test cases
1 parent 3d28581 commit e50aed7

File tree

2 files changed

+25
-67
lines changed

2 files changed

+25
-67
lines changed

DataStructures.Tests/LinkedList/CircularLinkedListTests.cs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,6 @@ namespace DataStructures.Tests.LinkedList;
77
[TestFixture]
88
public static class CircularLinkedListTests
99
{
10-
[Test]
11-
public static void TestDisplay()
12-
{
13-
var cll = new CircularLinkedList<int>();
14-
cll.InsertAtEnd(10);
15-
cll.InsertAtEnd(20);
16-
cll.InsertAtEnd(30);
17-
18-
Assert.That("10 20 30", Is.EqualTo(GetDisplayOutput(cll).Trim()));
19-
}
20-
21-
[Test]
22-
public static void TestDisplayEmptyList()
23-
{
24-
var cll = new CircularLinkedList<int>();
25-
var ex = Assert.Throws<InvalidOperationException>(() => cll.Display());
26-
27-
Assert.That(ex!.Message, Is.EqualTo("List is empty."));
28-
}
29-
3010
[Test]
3111
public static void TestInsertAtBeginning()
3212
{
@@ -177,35 +157,26 @@ public static void TestDeleteNonExistentNode()
177157
Assert.That("10 20 30", Is.EqualTo(GetDisplayOutput(cll).Trim()));
178158
}
179159

180-
/// <summary>
181-
/// Helper method to capture the output of the Display method for assertions.
182-
/// </summary>
183-
/// <param name="list">The CircularLinkedList instance.</param>
184-
/// <returns>A string representation of the list.</returns>
185-
private static string GetDisplayOutput(CircularLinkedList<int> list)
160+
private static string GetDisplayOutput<T>(CircularLinkedList<T> list)
186161
{
187-
// Save the original output (the default Console output stream)
188-
var originalConsoleOut = Console.Out;
162+
var head = list.GetHead();
163+
if (head == null)
164+
{
165+
return string.Empty;
166+
}
167+
168+
var current = head;
169+
var result = new System.Text.StringBuilder();
189170

190-
// Use a StringWriter to capture Console output
191-
using (var sw = new System.IO.StringWriter())
171+
do
192172
{
193-
try
194-
{
195-
// Redirect Console output to StringWriter
196-
Console.SetOut(sw);
197-
198-
// Call the method that outputs to the console
199-
list.Display();
200-
201-
// Return the captured output
202-
return sw.ToString();
203-
}
204-
finally
205-
{
206-
// Restore the original Console output stream
207-
Console.SetOut(originalConsoleOut);
208-
}
173+
result.Append(current!.Data + " ");
174+
current = current.Next;
209175
}
176+
while (current != head);
177+
178+
return result.ToString().Trim();
210179
}
180+
181+
211182
}

DataStructures/LinkedList/CircularLinkedList/CircularLinkedList.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public CircularLinkedList()
2222
tail = null;
2323
}
2424

25+
/// <summary>
26+
/// Gets the head node (tail.Next) of the Circular Linked List.
27+
/// </summary>
28+
public CircularLinkedListNode<T>? GetHead()
29+
{
30+
return tail?.Next;
31+
}
32+
2533
/// <summary>
2634
/// Determines whether the Circular Linked List is empty.
2735
/// </summary>
@@ -148,26 +156,5 @@ public void DeleteNode(T value)
148156
}
149157
while (current != tail!.Next);
150158
}
151-
152-
/// <summary>
153-
/// Displays the contents of the Circular Linked List.
154-
/// </summary>
155-
public void Display()
156-
{
157-
if (IsEmpty())
158-
{
159-
throw new InvalidOperationException("List is empty.");
160-
}
161-
162-
CircularLinkedListNode<T>? current = tail!.Next;
163-
do
164-
{
165-
Console.Write($"{current!.Data} ");
166-
current = current.Next;
167-
}
168-
while (current != tail!.Next);
169-
170-
Console.WriteLine();
171-
}
172159
}
173160
}

0 commit comments

Comments
 (0)