Ejemplo n.º 1
0
        public void AddNode(SingleLinkedListNode <T> node, int position)
        {
            if (position < 0)
            {
                throw new ArgumentException("Position should be positive number");
            }

            if (this.head == null)
            {
                this.head = node;
                return;
            }

            SingleLinkedListNode <T> last = this.head.next;

            int counter = 1;

            while (last.next != null && counter != position)
            {
                last = last.next;
                counter++;
            }

            if (last.next != null)
            {
                node.next = last.next;
            }

            last.next = node;

            return;
        }
Ejemplo n.º 2
0
        public bool RemoveElementByIndex(int position)
        {
            if (position == 0 && this.head != null && this.head.next != null)
            {
                this.head = this.head.next;
                return(true);
            }

            SingleLinkedListNode <T> current = this.head;
            int index = 1;

            while (current != null && index != position)
            {
                current = current.next;
                index++;
            }

            if (current == null)
            {
                return(false);
            }

            current.next = current.next.next;

            return(true);
        }
Ejemplo n.º 3
0
        public void ShowNodes()
        {
            SingleLinkedListNode <T> node = this.head;

            while (node != null)
            {
                Console.WriteLine(node.value);
                node = node.next;
            }
        }
Ejemplo n.º 4
0
        public void AddNode(SingleLinkedListNode <T> node)
        {
            if (this.head == null)
            {
                this.head = node;
                return;
            }

            SingleLinkedListNode <T> last = this.head;

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

            last.next = node;
            return;
        }
Ejemplo n.º 5
0
        public bool RemoveElementByKey(T key)
        {
            if (this.head != null && this.head.value.CompareTo(key) == 0)
            {
                if (this.head.next != null)
                {
                    this.head = this.head.next;
                    return(true);
                }

                this.head = null;
                return(true);
            }

            SingleLinkedListNode <T> node = this.head;

            while (node.next != null && node.next.value.CompareTo(key) != 0)
            {
                node = node.next;
            }

            if (node.next == null)
            {
                return(false);
            }

            if (node.next != null && node.next.next != null)
            {
                node.next = node.next.next;
                return(true);
            }

            if (node.next != null && node.next.next == null)
            {
                node.next = node.next.next;
            }

            return(true);
        }
Ejemplo n.º 6
0
 public SinglyLinkedList()
 {
     this.head = null;
 }