Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next
Layout of L = (a,b,c,d,e) using an array representation. Memory Layout Layout of L = (a,b,c,d,e) using an array representation. a b c d e The figures show computer memory. An array uses a contiguous chunk of memory. A linked representation uses an arbitrary layout. c a e d b
Linked Representation c a e d b first pointer (or link) in e is NULL use a variable first to get to the first element a
Normal Way To Draw A Linked List b c d e NULL first link or pointer field of node data field of node
Chain first NULL a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL (or 0) pointer.
Node Representation template <class T> class ChainNode { private: T data; ChainNode<T> *link; // constructors come here }; link data Better to define as a struct than a class as otherwise need to make every using class a friend so that node fields may be accessed.
Constructors Of ChainNode ? ChainNode() {} ? data ChainNode(const T& data) {this->data = data;} ChainNode(const T& data, chainNode<T>* link) {this->data = data; this->link = link;} link data
Get(0) a b c d e first desiredNode = first; // gets you to first node NULL first desiredNode = first; // gets you to first node return desiredNode->data; Move to desired node. Note that desired node may not exist. Once you are at the desired node, you can return the element in that node as in return desiredNode.element; When firstNode = null, there is no element whose index is 0.
Get(1) a b c d e NULL first desiredNode = first->link; // gets you to second node return desiredNode->data; Node that desiredNode may not exist!
Get(2) a b c d e NULL first desiredNode = first->link->link; // gets you to third node return desiredNode->data; Note that desiredNode may not exist
Get(5) a b c d e NULL First desiredNode = first->link->link->link->link->link; // desiredNode = NULL return desiredNode->data; // NULL.element Get an exception now as NULL->data is illegal!
Delete An Element Delete(0) deleteNode = first; b c d e NULL first Delete(0) Note that node 0 may not exist! deleteNode = first; first = first->link; delete deleteNode;
first get to node just before node to be removed Delete(2) a b d e NULL first c b beforeNode c first get to node just before node to be removed beforeNode = first->link;
save pointer to node that will be deleted first null a b c d e beforeNode save pointer to node that will be deleted deleteNode = beforeNode->link;
now change pointer in beforeNode Delete(2) first null a b c d e beforeNode now change pointer in beforeNode beforeNode->link = beforeNode->link->link; delete deleteNode;
Insert(0,’f’) first newNode a b c d e NULL first f newNode Step 1: get a node, set its data and link fields First, do a checkIndex. newNode = new ChainNode<char>(theElement, first);
Insert(0,’f’) first newNode Step 2: update first first = newNode; a b c d e NULL first f newNode Step 2: update first first = newNode;
first = new chainNode<char>(‘f’, first); One-Step Insert(0,’f’) a b c d e NULL first f newNode first = new chainNode<char>(‘f’, first);
first find node whose index is 2 Insert(3,’f’) a b c d e NULL first f newNode beforeNode c first find node whose index is 2 next create a node and set its data and link fields ChainNode<char>* newNode = new ChainNode<char>( ‘f’, beforeNode->link); finally link beforeNode to newNode beforeNode->link = newNode;
Two-Step Insert(3,’f’) beforeNode = first->link->link; a b c d e NULL first f newNode beforeNode beforeNode = first->link->link; beforeNode->link = new ChainNode<char> (‘f’, beforeNode->link);