Created
April 30, 2023 14:27
-
-
Save sigmadream/96eb6da5051c9b1d51caa6e75ff3e0a2 to your computer and use it in GitHub Desktop.
This file contains 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
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()); | |
} | |
} | |
} |
This file contains 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
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; | |
} | |
} | |
} |
This file contains 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
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