Skip to content

Commit b37d9d5

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Linked Lists: Base code for Linked lists added.
1 parent 91c436b commit b37d9d5

File tree

2 files changed

+91
-0
lines changed
  • algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/linked_list/lib
  • algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/linked_list

2 files changed

+91
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class NodeTest
5+
{
6+
class NodeTestCase
7+
{
8+
public string title = "";
9+
public LinkedList.Node? llist;
10+
public string separator = "";
11+
public string expected = "";
12+
}
13+
14+
private static NodeTestCase[] tests = [];
15+
16+
[TestInitialize]
17+
public void testInitialize()
18+
{
19+
// Linked list sample data:
20+
LinkedList.Node ll1_1 = new(1);
21+
LinkedList.Node ll1_2 = new(2);
22+
LinkedList.Node ll1_3 = new(3);
23+
LinkedList.Node ll1_4 = new(4);
24+
LinkedList.Node ll1_5 = new(5);
25+
26+
ll1_1.next = ll1_2;
27+
ll1_2.next = ll1_3;
28+
ll1_3.next = ll1_4;
29+
ll1_4.next = ll1_5;
30+
31+
tests = [
32+
new()
33+
{
34+
title = "Sample Test case X",
35+
llist = ll1_1,
36+
separator = ", ",
37+
expected = "1, 2, 3, 4, 5"
38+
}
39+
];
40+
}
41+
42+
[TestMethod]
43+
public void testPrintLinkedList()
44+
{
45+
foreach (NodeTestCase test in tests)
46+
{
47+
StringWriter sw = new();
48+
49+
LinkedList.printSinglyLinkedList(test.llist, test.separator, sw);
50+
51+
string result = sw.ToString();
52+
Assert.AreEqual(
53+
test.expected,
54+
result,
55+
String.Format(
56+
"{0} testPrintLinkedList(<Node>, {1}, <Textwriter>) => must be: {2}",
57+
test.title,
58+
test.separator,
59+
test.expected
60+
)
61+
);
62+
}
63+
}
64+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
public class LinkedList
4+
{
5+
public class Node(int nodeData)
6+
{
7+
public int data { get; set; } = nodeData;
8+
public Node? next { get; set; } = null;
9+
}
10+
11+
public static void printSinglyLinkedList(Node? node, string sep, TextWriter textWriter)
12+
{
13+
Node? pointTo = node;
14+
15+
while (pointTo != null)
16+
{
17+
textWriter.Write(pointTo.data);
18+
19+
pointTo = pointTo.next;
20+
21+
if (pointTo != null)
22+
{
23+
textWriter.Write(sep);
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)