/** * @fileOverview Implementation of a singly linked-list data structure * @author Jason S. Jones * @license MIT */ import ListIterator from './lib/list-iterator'; import ListNode from './lib/list-node'; /** * Linked list class * * Implementation of a singulary linked list data structure. This * implementation provides the general functionality of adding nodes to * the front or back of the list, as well as removing node from the front * or back. This functionality enables this implemention to be the * underlying data structure for the more specific stack or queue data * structure. * */ declare class LinkedList { head: ListNode; tail: ListNode; size: number; iterator: ListIterator; /** * Creates a LinkedList instance. Each instance has a head node, a tail node * and a size, which represents the number of nodes in the list. * * @constructor * @param {array} arr The data to initialize with the list */ constructor(arr?: any[]); /** * Creates a new Node object with 'data' assigned to the node's data property * * @param {object|string|number} data The data to initialize with the node * @returns {object} Node object intialized with 'data' */ createNewNode(data: any): ListNode; /** * Returns the first node in the list, commonly referred to as the 'head' node * * @returns {object} the head node of the list */ getHeadNode(): ListNode; /** * Returns the last node in the list, commonly referred to as the 'tail' node * * @returns {object} the tail node of the list */ getTailNode(): ListNode; /** * Determines if the list is empty * * @returns {boolean} true if the list is empty, false otherwise */ isEmpty(): boolean; /** * Returns the size of the list, or number of nodes * * @returns {number} the number of nodes in the list */ getSize(): number; /** * Clears the list of all nodes/data */ clear(): void; /** * Utility function to iterate over the list and call the fn provided * on each node, or element, of the list * * @param {object} cb The function to call on each node of the list */ forEach(cb: (node: ListNode) => void): void; /** * Returns an array of all the data contained in the list * * @returns {array} the array of all the data from the list */ toArray(): ListNode[]; /** * Prints to the console the data property of each node in the list */ printList(): void; /** * Inserts a node with the provided data to the end of the list * * @param {object|string|number} data The data to initialize with the node * @returns {boolean} true if insert operation was successful */ insert(data: any): boolean; /** * Inserts a node with the provided data to the front of the list * * @param {object|string|number} data The data to initialize with the node * @returns {boolean} true if insert operation was successful */ insertFirst(data: any): boolean; /** * Inserts a node with the provided data at the index indicated. * * @param {number} index The index in the list to insert the new node * @param {object|string|number} data The data to initialize with the node * @returns {boolean} true if insert operation was successful */ insertAt(index: number, data: any): boolean; /** * Inserts a node before the first node containing the provided data * * @param {object|string|number} nodeData The data of the node to * find to insert the new node before * @param {object|string|number} dataToInsert The data to initialize with the node * @returns {boolean} true if insert operation was successful */ insertBefore(nodeData: any, dataToInsert: any): boolean; /** * Inserts a node after the first node containing the provided data * * @param {object|string|number} nodeData The data of the node to * find to insert the new node after * @param {object|string|number} dataToInsert The data to initialize with the node * @returns {boolean} true if insert operation was successful */ insertAfter(nodeData: any, dataToInsert: any): boolean; /** * Removes the tail node from the list * * There is a slight perfomance cost associated with this operation. * In order to remove the tail node a handle to the node before the * tail node is required, which requires a O(n) operation. * * @returns the node that was removed */ remove(): ListNode; /** * Removes the head node from the list * * @returns the node that was removed */ removeFirst(): ListNode; /** * Removes the node at the index provided * * @param {number} index The index of the node to remove * @returns the node that was removed */ removeAt(index: number): ListNode; /** * Removes the first node that contains the data provided * * @param {object|string|number} data The data of the node to remove * @returns the node that was removed */ removeNode(data: any): ListNode; /** * Returns the index of the first node containing the provided data. If * a node cannot be found containing the provided data, -1 is returned. * * @param {object|string|number} data The data of the node to find * @returns the index of the node if found, -1 otherwise */ indexOf(data: any): number; /** * Determines whether or not the list contains the provided data * * @param {object|string|number} data The data to check if the list * contains * @returns the true if the list contains data, false otherwise */ contains(data: any): boolean; /** * Returns the fist node containing the provided data. If a node * cannot be found containing the provided data, null is returned. * * @param {object|string|number} data The data of the node to find * @returns the node if found, null otherwise */ find(data: any): ListNode; /** * Returns the node at the location provided by index * * @param {number} index The index of the node to return * @returns the node located at the index provided. */ findAt(index: number): ListNode; } export default LinkedList;