Example #1
0
        public static DsalLinkedList AddItems(this DsalLinkedList linkedList, IEnumerable<int> items)
        {
            foreach (var item in items)
            {
                var newNode = new LinkedListNode() { Data = item };

                if (linkedList.Head == null)
                {
                    linkedList.Head = linkedList.Tail = newNode;
                }
                else
                {
                    linkedList.Tail.Next = newNode;
                    linkedList.Tail = newNode;
                }
                linkedList.Count++;
            }
            return linkedList;
        }
Example #2
0
        public static DsalLinkedList RemoveNode(this DsalLinkedList linkedList, LinkedListNode nodeToRemove, LinkedListNode prevNode)
        {
            if (nodeToRemove == null) return linkedList;

            if (prevNode == null)
            {
                linkedList.Head = nodeToRemove.Next;
                if (linkedList.Tail == nodeToRemove)
                {
                    linkedList.Tail = nodeToRemove.Next;
                }
            }
            else
            {
                prevNode.Next = nodeToRemove.Next;
                if (linkedList.Tail == nodeToRemove)
                {
                    linkedList.Tail = prevNode;
                }
            }

            return linkedList;
        }