Singly Linked List Example With Classes in C#

A linked list is a form of data structure that consists of some combined nodes. Linked lists have different types and the simplest one is singly linked list, or one-way linked list in another words.

A singly linked list has a starting point. To reach all nodes in this list, starting node must be known. To do that, we have to identify a starting node.

Another point on singly linked lists is the last node. The last node has a null value for the next node. Thanks to that data, we can recognize the linked list’s last point.

Singly Linked List

In linked list, we have to use at least 2 classes. One of two is creating node structure, and another one is for list processes such as adding, deleting and listing.

Node Class of Singly Linked List

Each singly linked list node has two components: value and next node…

    class node
    {
        public string data;
        public node nextNode;

        public node(string value)
        {
            this.data = value;
            this.nextNode = null;
        }
    }

We will use this node everytime we need to add new element or removing any.

Processing Class

    class singleLinkedList
    {
        public node firstNode = null, lastNode = null;

        public singleLinkedList();

        public bool addNode(string value,int position)
        {

            node newNode = new node(value);
            
            if (firstNode == null) {
                firstNode = newNode;
                lastNode = newNode;
                return true;
            }

            if (position == -1) //EN lastNodeA EKLEME
            {
                lastNode.nextNode = newNode;
                lastNode = newNode;
                return true;
            }


            node activeNode = new node(null);
            node prevNode = new node(null);

            activeNode = firstNode;
            int say = 0;


            while (activeNode != null)
            {
                if (position == say++)
                {
                    if (activeNode == firstNode) //BAŞA EKLEME
                    {
                        newNode.nextNode = firstNode;
                        firstNode = newNode;
                        return true;
                    }
                    else //ORTAYA EKLEME
                    {
                        newNode.nextNode = prevNode.nextNode;
                        prevNode.nextNode = newNode;
                        return true;
                    }
                }
                prevNode = activeNode; //ÖNCEKİ ELEMANI DEĞİŞTİRME
                activeNode = activeNode.nextNode;  //İLERLEME
            }

            return false;
        }

        public bool delete(int position)
        {
            node activeNode = new node(null);
            node prevNode = new node(null);
            if (firstNode == null)
                return false;

            activeNode = firstNode;
            int say = 0;
            while (activeNode != null)
            {
                if (position == say++)
                {
                    if (firstNode==activeNode)
                    {
                        firstNode = firstNode.nextNode;
                        return true;
                    }

                    if (activeNode==lastNode)
                    {
                        lastNode = prevNode;
                        lastNode.nextNode = null;
                        return true;
                    }

                    prevNode.nextNode = activeNode.nextNode;
                    return true;
                }
                prevNode = activeNode;
                activeNode = activeNode.nextNode;
            }
            return false;
        }

        public ArrayList listNodes() 
        {
            ArrayList _list = new ArrayList();
            if (firstNode == null)
                return liste;

            node activeNode = new node(null);
            activeNode = firstNode;

            while (activeNode!=null) 
            {
                _list.Add(activeNode.data);
                activeNode = activeNode.nextNode;
            }

            return _list;
        }

    }

 

Add a Comment

Your email address will not be published. Required fields are marked *