Linked list implementation in c#

Jack Herer 110 Reputation points
2023-04-22T14:11:30.3266667+00:00

Hey, I'm just posting how to implement a linked list in c#...

Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-04-22T17:39:35.7133333+00:00

    As the main benefit of a single linked list is adding and removing nodes at the head, adding tail support makes the list more useful.. Then it’s efficient to add / remove nodes at the tail.

    also you should convert to generic.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Jack Herer 110 Reputation points
    2023-04-22T14:19:42.27+00:00
    internal class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello, World!");
                Operations operations = new();
    
                Node node1 = new(420);
    
                SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
                singlyLinkedList.head = node1;
    
                operations.InsertFront(singlyLinkedList, 420420);
                operations.InsertFront(singlyLinkedList, 69);
                operations.InsertFront(singlyLinkedList, 198);
                operations.InsertFront(singlyLinkedList, 254);
    
                Node testNode = operations.GetLastNode(singlyLinkedList);
    
                Console.WriteLine(testNode.Data);
    
    
                Console.ReadKey();
            }
        }
        public class SinglyLinkedList
        {
            public Node head;
        }
    
        public class Node
        {
            public int Data;
            public Node Next;
            public Node(int data)
            {
                this.Data = data;
                this.Next = null;
            }
        }
    
        public class Operations
        {
            // Adding data from front of linked list
            public void InsertFront(SinglyLinkedList singlyLinkedList, int data)
            {
                Node node = new(data);
                node.Next = singlyLinkedList.head;
                singlyLinkedList.head = node;
            }
    
            // Adding data from rear of linked list
            public void InsertLast(SinglyLinkedList singlyLinkedList, int data)
            {
                Node node = new(data);
                if (singlyLinkedList.head == null)
                {
                    singlyLinkedList.head = node;
                    return;
                }
    
                Node lastNode = this.GetLastNode(singlyLinkedList);
                lastNode.Next = node;
            }
    
            // Adding data after specified node
            public void InsertAfter(Node prevNode, int data)
            {
    
                if (prevNode == null)
                {
                    throw new Exception("Previous node cannot be null!");
                }
                Node node = new(data);
                node.Next = prevNode.Next;
                prevNode.Next = node;
            }
    
            // Returns last node
            public Node GetLastNode(SinglyLinkedList singlyLinkedList)
            {
                Node temp = singlyLinkedList.head;
                while (temp.Next != null)
                {
                    temp = temp.Next;
                }
    
                return temp;
            }
    
            // Removes selected node
            public void DeleteSelectedNode(SinglyLinkedList singlyLinkedList, int index)
            {
                Node temp = singlyLinkedList.head;
                Node prev = null;
    
                if (temp != null && temp.Data == index)
                {
                    singlyLinkedList.head = temp.Next;
                    return;
                }
    
                while (temp != null && temp.Data != index)
                {
                    prev = temp;
                    temp = temp.Next;
                }
    
                if (temp == null)
                {
                    return;
                }
    
                prev.Next = temp.Next;
            }
        }
    
    0 comments No comments

  2. Jack Herer 110 Reputation points
    2023-04-22T14:27:18.05+00:00

    Also adding some basic tests...

    [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void Create()
            {
                int data = 420;
                Node firstNode = new(data);
                SinglyLinkedList singlyLinkedList = new();
                singlyLinkedList.head = firstNode;
    
                Assert.AreEqual(data, singlyLinkedList.head.Data);
            }
    
            [TestMethod]
            public void InsertFront()
            {
                int data = 420;
                Node firstNode = new(data);
                SinglyLinkedList singlyLinkedList = new();
                singlyLinkedList.head = firstNode;
    
                int newData = 240;
                Operations operations = new();
                operations.InsertFront(singlyLinkedList, newData);
    
                Assert.AreEqual(newData, singlyLinkedList.head.Data);
                Assert.AreEqual(data, singlyLinkedList.head.Next.Data);
            }
    
            [TestMethod]
            public void InsertLast()
            {
                int data = 420;
                Node firstNode = new(data);
                SinglyLinkedList singlyLinkedList = new();
                singlyLinkedList.head = firstNode;
    
                int newData = 240;
                Operations operations = new();
                operations.InsertLast(singlyLinkedList, newData);
    
                Assert.AreEqual(data, singlyLinkedList.head.Data);
                Assert.AreEqual(newData, singlyLinkedList.head.Next.Data);
            }
    
            [TestMethod]
            public void GetLastNode()
            {
                int data1 = 420;
                Node firstNode = new(data1);
                SinglyLinkedList singlyLinkedList = new();
                singlyLinkedList.head = firstNode;
    
                int data2 = 240;
                Operations operations = new();
                operations.InsertLast(singlyLinkedList, data2);
    
                int data3 = 123;
                operations.InsertLast(singlyLinkedList, data3);
    
                Node lastNode = operations.GetLastNode(singlyLinkedList);
    
                Assert.AreEqual(data3, lastNode.Data);
                Assert.IsNull(lastNode.Next);
            }
    
            [TestMethod]
            public void InserAfter()
            {
                int data1 = 420;
                Node firstNode = new(data1);
                SinglyLinkedList singlyLinkedList = new();
                singlyLinkedList.head = firstNode;
    
                int data2 = 240;
                Operations operations = new();
                operations.InsertLast(singlyLinkedList, data2);
    
                int data3 = 123;
                operations.InsertAfter(firstNode, data3);
    
                Assert.AreEqual(data3, firstNode.Next.Data);
                Assert.AreEqual(data2, firstNode.Next.Next.Data);
            }
    
            [TestMethod]
            public void InserAfter_PreviousNodeNull_ThrowsException()
            {
                Operations operations = new();
    
                try
                {
                    operations.InsertAfter(null, 420);
                    Assert.Fail();
                }
                catch (Exception e)
                {
                    Assert.AreEqual("Previous node cannot be null!", e.Message);
                }
            }
    
        }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.