Showing posts with label Linked list implementation in C#. Show all posts
Showing posts with label Linked list implementation in C#. Show all posts

Friday, May 17, 2019

C# : Data Structure : Linked list implementation in C#



Linked List in C#:

 class LinkedListImpl
    {
        Node temp, head,last;
        int count = 0;
        public void InsertAtEnd(int value)
        {
            temp = new Node() { data = value, Next = null };
            if (head == null)
            {
                head = temp;
                last = temp;
            }
         
            last.Next = temp;
            last = temp;
         
            count++;
        }
        public void PrintLinkedList()
        {
            temp = head;
            if (temp == null) throw new Exception("Empty linked list!");

            while (temp != null)
            {
                Console.Write($"{temp.data}=>");
                temp = temp.Next;
            }

            Console.WriteLine("First element of List is " + head.data);
            Console.WriteLine("Last element of List is " + last.data);
        }
        public void InsertAtStart(int value)
        {
            temp = new Node() { data = value, Next = head };
            head = temp;

        }
        public void Insert(int insertAfter,int value)
        {
            temp = head;
            while (temp != null)
            {
                if (temp.data == insertAfter)
                {
                    Node temp2 = new Node { data = value, Next = temp.Next };
                    temp.Next = temp2;

                    // if inserted at last then need to Move last pointer.
                    if (temp2.Next is null)
                    {
                        last = temp2;
                    }
                    break;
                 
                }
                temp = temp.Next;
            }
        }

        public bool Find(int value)
        {
            temp = head;
            while (temp != null)
            {
                if (temp.data == value)
                {
                    return true;
                }
             
                temp = temp.Next;
            }
            return false;
        }
        public bool Delete(int value)
        {
            if (head.data == value)// if data is available at head node
            {
                head = head.Next;
                return true;
            }
            temp = head;
            while (temp != null)// moving till( desired node -1). i.e. before the desired node (1=>2=>5=>6=>7=>8 and we want to delete 6, so need to move till 5 th only.)
            {
             
                if (temp.Next?.data == value)
                {
                    temp.Next = temp.Next.Next;
                }

                if (temp.Next is null)// to set last node.
                {
                    last = temp;
                }
                temp = temp.Next;
             
            }
            return false;
        }
        class Node
        {
            public int data { get; set; }
            public Node Next { get; set; }
        }
/*
        public static void Main()
        {
            LinkedListImpl linkedList = new LinkedListImpl();

            linkedList.InsertAtEnd(2);
            linkedList.InsertAtEnd(4);
            linkedList.InsertAtEnd(6);

            linkedList.InsertAtStart(1);
            linkedList.InsertAtStart(0);

            linkedList.Insert(4, 5);
            linkedList.Insert(6, 7);
            linkedList.Insert(7, 8);


            Console.WriteLine(linkedList.Find(3));
            Console.WriteLine(linkedList.Find(0));
            Console.WriteLine(linkedList.Find(7));

            linkedList.PrintLinkedList();
            linkedList.Delete(7);
            linkedList.Delete(0);
            linkedList.Delete(8);
            linkedList.PrintLinkedList();
            Console.Read();

        }
        */

    }

Monday, October 22, 2018

Linked List implementation in C#

  public class Node
    {
       public int item;
       public Node next;
       public Node(int it)
        {
            item = it;
            next = null;
            Console.Write("[{0}]-->", item);
        }
    }

    public class LinkedList
    {
       public Node top;

        public LinkedList(int item)
        {
            Node n = new Node(item);
            top = n;

        }

        public void Push(int item)
        {
            Node _node = new Node(item);

            //Add Node at before top
            if (top == null)
            {
                top = _node;
            }
            else
            {
                _node.next = top;
                top = _node;
            }
        }

        public void InsertAfter(Node node, int item)//Inserting item (new node) after given node.
        {
            Node newNode = new Node(item);
            if (top == null) return;
            Node nxtNode = top;
           
            while (nxtNode != null)
            {
                if (nxtNode.item == node.item)
                {
                    newNode.next = nxtNode.next;
                    nxtNode.next = newNode;
                    break;
                }
                else
                {
                    nxtNode = nxtNode.next;
                }
            }
        }

        public void Print()
        {
            if (top == null)
            {
                Console.WriteLine("List is empty");
                return;
            }
            Node nexNode =top;
            while (nexNode != null)
            {
                Console.WriteLine(nexNode.item);
                nexNode = nexNode.next;
            }

        }
    }
    class TestLinked
    {
        public static void Main()
        {
            LinkedList ll = new LinkedList(1);
            Console.WriteLine("Top item=" + ll.top.item); //each newly added item will be top of linked list.
            ll.Push(3);
            Console.WriteLine("Top item=" + ll.top.item);
            ll.Push(5);
            Console.WriteLine("Top item=" + ll.top.item);
            ll.Push(6);
            Console.WriteLine("Top item=" + ll.top.item);

            ll.InsertAfter(new Node(3), 4);
            ll.Print();
            Console.ReadLine();
        }
    }

C# Record type: Something to remember while using record types

  Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...