The realization of simple one-way linked list

Posted by hbsnam on Fri, 13 Dec 2019 21:41:40 +0100

The picture is quoted from mooc.com. I have realized a simple linked list myself

Realization

class LinkedList<E>
    {
        private class Node
        {
            public E e;
            public Node next;
            public Node(E e,Node next)
            {
                this.e = e;
                this.next = next;
            }
            public Node(E e) : this(e, null)
            {

            }
            public Node() : this(default(E), null)
            {

            }
            public override string ToString()
            {
                return e.ToString();
            }
        }
        private Node head;
        private int size;
        public LinkedList()
        {
            head = null;
            size = 0;
        }
        public int getSize()
        {
            return size;
        }
        public bool isEmpty()
        {
            return size == 0;
        }
        //Add a new element e to the list header
        public void addFirst(E e)
        {
            Node node = new Node(e);
            node.next = head;
            head = node;

            //head = new Node(e, head);
            size++;

        }
        //Add a new element e in the index position of the linked list
        public void add(int index,E e)
        {
            if (index < 0 || index > size)
                throw new ArgumentException("Add failed. Illegal index.");
            if (index == 0)
                addFirst(e);
            else
            {
                //Find previous element of index location
                Node prev = head;
                for (int i = 0; i < index - 1; i++)
                    prev = prev.next;
                Node node = new Node(e);
                node.next = prev.next;
                prev.next = node;

                prev.next = new Node(e, prev.next);
                size++;
            }
        }
        //Add a new element e at the end of the list
        public void addLast(E e)
        {
            add(size, e);
        }
    }

Add elements to linked list head

Now I'm going to make an explanation for adding elements. First, add elements to the head of the list.

In our chain header, there is a head pointer head pointing to the head element. Now we need to add elements before the head element. We just need to point the next pointer of the element node to be added to the head, and point the head pointer to the node, so that we can add elements before the head element, namely:
1.node.next=head;
2.head=node;
Remember that the order of these two steps cannot be changed, or it will be meaningless.

Add elements anywhere in the list

This element is added to better understand the processing logic of linked list.

The first step is to find the previous element that needs to be inserted by traversing, and point to it with prev. The next operation is that the next of the element to be inserted points to the next of prev, and then the next of perv points to the node.
1. Use prev to point to the previous element of index;
2.node.next = prev.next;
3.prev.next=node;

Topics: Programming