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

2 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

3 Memory Layout abcdecaedb A linked representation uses an arbitrary layout. Layout of L = (a,b,c,d,e) using an array representation.

4 Linked Representation pointer (or link) in e is NULL caedb use a variable first to get to the first element a first

5 Normal Way To Draw A Linked List link or pointer field of node data field of node abcde NULL first

6 Chain 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. abcde NULL first

7 Node Representation template class ChainNode { private: T data; ChainNode *link; // constructors come here }; link data

8 Constructors Of ChainNode ChainNode() {} ? ? ? data link data ChainNode(const T& data) {this->data = data;} ChainNode(const T& data, chainNode * link) {this->data = data; this->link = link;}

9 Get(0) desiredNode = first; // gets you to first node return desiredNode  data; abcde NULL first

10 Get(1) desiredNode = first  link; // gets you to second node return desiredNode  data; abcde NULL first

11 Get(2) desiredNode = first  link  link; // gets you to third node return desiredNode  data; abcde NULL first

12 Get(5) desiredNode = first  link  link  link  link  link; // desiredNode = NULL return desiredNode  data; // NULL.element abcde NULL First

13 Delete An Element Delete(0) abcde NULL first deleteNode = first; first = first  link; delete deleteNode;

14 abde NULL first c Delete(2) first get to node just before node to be removed c c beforeNode = first  link ; b beforeNode

15 Delete(2) save pointer to node that will be deleted deleteNode = beforeNode  link; beforeNode abcde null first

16 Delete(2) now change pointer in beforeNode beforeNode  link = beforeNode  link  link; delete deleteNode; beforeNode abcde null first

17 Insert(0,’f’) abcde NULL first f newNode Step 1: get a node, set its data and link fields newNode = new ChainNode (theElement, first);

18 Insert(0,’f’) abcde NULL first f newNode Step 2: update first first = newNode;

19 One-Step Insert(0,’f’) abcde NULL first f newNode first = new chainNode (‘f’, first);

20 Insert(3,’f’) first find node whose index is 2 abcde NULL first f newNode beforeNode c next create a node and set its data and link fields ChainNode * newNode = new ChainNode ( ‘f’, beforeNode  link); finally link beforeNode to newNode beforeNode  link = newNode;

21 Two-Step Insert(3,’f’) beforeNode = first  link  link; beforeNode  link = new ChainNode (‘f’, beforeNode  link); abcde NULL first f newNode beforeNode c


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