Skip to content

Instantly share code, notes, and snippets.

@sigmadream
Created April 30, 2023 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sigmadream/96eb6da5051c9b1d51caa6e75ff3e0a2 to your computer and use it in GitHub Desktop.
Save sigmadream/96eb6da5051c9b1d51caa6e75ff3e0a2 to your computer and use it in GitHub Desktop.
using Xunit;
using DSWithAlgo.DS.LinkedList.SingleLinkedList;
namespace DSWithAlgo.Tests.LinkedList
{
public static class LinkedListTests
{
[Fact]
public static void Add()
{
var a = new SingleLinkedList();
a.AddLast(1);
a.AddLast(15);
a.AddLast(30);
Assert.Equal(3, a.Length());
}
}
}
namespace DSWithAlgo.DS.LinkedList.SingleLinkedList
{
public class SingleLinkedList
{
private SingleLinkedListNode? Head { get; set; }
public SingleLinkedListNode AddLast(int data)
{
var newNode = new SingleLinkedListNode(data);
if (Head is null)
{
Head = newNode;
return newNode;
}
var tempNode = Head;
while (tempNode.Next is not null)
{
tempNode = tempNode.Next;
}
tempNode.Next = newNode;
return newNode;
}
public int Length()
{
if (Head is null)
{
return 0;
}
var tempNode = Head;
var length = 1;
while (tempNode.Next is not null)
{
tempNode = tempNode.Next;
length = length + 1;
}
return length;
}
}
}
namespace DSWithAlgo.DS.LinkedList.SingleLinkedList
{
public class SingleLinkedListNode
{
public SingleLinkedListNode(int data)
{
Data = data;
Next = null;
}
public int Data { get; }
public SingleLinkedListNode? Next { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment