Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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

3 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

4 Normal Way To Draw A Linked List
b c d e NULL first link or pointer field of node data field of node

5 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.

6 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.

7 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

8 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.

9 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!

10 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

11 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!

12 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;

13 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;

14 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;

15 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;

16 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);

17 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;

18 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);

19 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;

20 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);


Download ppt "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."

Similar presentations


Ads by Google