Home > database >  c# Singly Linked list how exacly this works
c# Singly Linked list how exacly this works

Time:11-06


i have a problem with understand some parts of this code...
I guess this is about how values ​​are stored by reference, my problem is mostly about methods : InsertDataAtTheEnd : I can't understand how variable temp can affect variable head, since it's been truncated more and more by while loop....and finally the assignment of temp.next which affects the head. A similar situation occurs in the method DelteNodeByValue.

Can someone please explain it to me, or show some materials that will be able to simply explain what is going on here?:(

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp8
{
    class SinglyLinkedList 
    {
        public Node head { get; set; }
       


        public void InsertDataAtTheBeggining(int data)
        {

            Node newNode = new Node(data);
            newNode.next = head;
            head = newNode;


        }


        public void InsertDataAtTheEnd(int data)
        {
            Node temp = head;
            while (temp.next != null)
            {
                temp = temp.next;
            }

            Node newNode = new Node(data);
            // Node lastElement = GetLastElement(this);
            temp.next = newNode;

        }


         Node GetLastElement(SinglyLinkedList List)
        {
    
            Node temp = head;
            while (temp.next != null)
            {
               
                temp = temp.next;
            }

            return temp;

        }

         void DelteNodeFromEnd()
        {
            Node lastElement = GetLastElement(this);



        }


        public void DelteNodeByValue(int value)
        {
            var temp = head;
            Node previusly = null;
            if (temp != null && temp.data == value) // w przypadku jesli chce sie usunac pierwszy element listy;
            {
                head = temp.next;
                return;
            }

            while (temp != null && temp.data != value)
            {
                previusly = temp;
                temp = temp.next;
            }

            if (temp == null)
            {
                return;
            }

            previusly.next = temp.next;
        }


        public void DelteNodeFromBeggining()
        {


            head = head.next;




        }


        public void ShowAllData()
        {
            Node holder = head;
            while (holder != null)
            {
                Console.WriteLine(holder.data);
                holder = holder.next;
            }




        }


       internal class Node
        {
           public int data;
          public  Node next;
            public Node(int d)
            {
                data = d;
                next = null;
            }
        }



    }
}

CodePudding user response:

Personally, I wouldn't implement a linked list in C# like that, (there are also several mistakes in the code in my opinion). Probably not at all, considering the generic doubly linked list structure LinkedList and LinkedListNode that are built into System.Collections.Generic. Nevertheless, acquiring the concepts behind the scenes is really important, so I'll try explaining the problematic pieces of code for you.

The method InsertDataAtEnd() is comprised of several steps:
1.

Node temp = head;

head represents the reference to the first node in the linked list. When we declare temp as head, what we're actually doing is setting temp to be another reference to the first node of the list. Namely, both head and temp now point to the the first node of the list. They point in the same place in memory. For example, for a linked list with the values 5->7->4->2: graphic visualization

2.

 while (temp.next != null)
 {
     temp = temp.next;
 }

We advance temp to point to the last element of the list. We achieve that by using the fact that the next attribute of the last element is null. Therefore, we advance the pointer until the next attribute of the node it points at is null. Graphic visualization So after this stage head still points at the first node and next points at the last element of the list.

These two stages are basically the same thing in your GetLastElement() method. 3.

 Node newNode = new Node(data);
 temp.next = newNode;

We create a new node with the specified data, and declaring the next attribute of temp to point at that new node. Since temp points at the last element, its next attribute was null, and now it the new element. So the new node has now become the last element.

  • Related