/// <summary> /// Remove last item from the linked list /// </summary> /// <returns>Value of the removed item</returns> /// <exception cref="IndexOutOfRangeException"></exception> public int PopBack() { if (_head == null) { throw new IndexOutOfRangeException("Attempted to remove value from empty list"); } var returnValue = _tail->Value; if (_head == _tail) { _head = null; _tail = null; return(returnValue); } var current = _head; while (true) { if (current->Next != _tail) { continue; } current->Next = null; _tail = current; return(returnValue); } }
/// <summary> /// Remove first element in linked list /// </summary> /// <returns>Value of element removed</returns> /// <exception cref="IndexOutOfRangeException"></exception> public int PopFront() { if (_head == null) { throw new IndexOutOfRangeException("Attempted to remove value from empty list"); } var returnValue = _head->Value; if (_head == _tail) { _head = null; _tail = null; return(returnValue); } _head = _head->Next; return(returnValue); }
/// <summary> /// Add new given integer value to the front of the linked list /// </summary> /// <param name="value">Value to Add</param> public void PushFront(int value) { var insertNode = new IntNode { Value = value }; if (_head != null) { insertNode.Next = _head; } _head = &insertNode; if (_tail == null) { _tail = &insertNode; } return; }
/// <summary> /// Add new integer value to the end of the linked list /// </summary> /// <param name="value">Value to add</param> public void PushBack(int value) { var insertValue = new IntNode { Value = value }; if (_head == null) { _head = &insertValue; _tail = &insertValue; return; } if (_head == _tail) { _head->Next = &insertValue; _tail = &insertValue; return; } _tail->Next = &insertValue; _tail = &insertValue; return; }