Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Representation

Similar presentations


Presentation on theme: "Linked Representation"— Presentation transcript:

1 Linked Representation
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 firstNode pointer (or link) in e is null use a variable firstNode to get to the first element a

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

5 Chain firstNode 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 pointer.

6 Node Representation package dataStructures; class ChainNode {
// package visible data members Object element; ChainNode next; // constructors come here } next element

7 Constructors Of ChainNode
null ChainNode() {} null element ChainNode(Object element) {this.element = element;} ChainNode(Object element, ChainNode next) {this.element = element; this.next = next;} next element

8 desiredNode = firstNode; // gets you to first node
a b c d e null firstNode desiredNode = firstNode; // gets you to first node Do a checkIndex first. Then move to desired node. 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 desiredNode = firstNode.next; // gets you to second node
a b c d e null firstNode desiredNode = firstNode.next; // gets you to second node

10 desiredNode = firstNode.next.next; // gets you to third node
a b c d e null firstNode desiredNode = firstNode.next.next; // gets you to third node

11 get(5) a b c d e null firstNode desiredNode = firstNode.next.next.next.next.next; // desiredNode = null Note that checkIndex would throw an exception.

12 NullPointerException
a b c d e null firstNode desiredNode = firstNode.next.next.next.next.next.next; // gets the computer mad // you get a NullPointerException firstNode.next.next.next.next.next = null So desiredNode = null.next, which causes the null pointer exception.

13 Remove An Element remove(0) firstNode = firstNode.next; a b c d e
null firstNode remove(0) First do a checkIndex. Even though this change in value of firstNode leaves behind a pointer from the original first node to the new first node, the original first node is no longer part of the chain, because you cannot get to it from firstNode. The memory used by the original first node will be reclaimed automatically by Java’s garbage collector. firstNode = firstNode.next;

14 first get to node just before node to be removed
a b d e null firstNode c b beforeNode c first get to node just before node to be removed beforeNode = firstNode.next;

15 now change pointer in beforeNode
remove(2) firstNode null a b c d e beforeNode now change pointer in beforeNode beforeNode.next = beforeNode.next.next;

16 add(0,’f’) firstNode newNode
b c d e null firstNode f newNode Step 1: get a node, set its data and link fields First, do a checkIndex. ChainNode newNode = new ChainNode(new Character(‘f’), firstNode);

17 add(0,’f’) firstNode newNode Step 2: update firstNode
b c d e null firstNode f newNode Step 2: update firstNode firstNode = newNode;

18 firstNode = new ChainNode( new Character(‘f’), firstNode);
One-Step add(0,’f’) a b c d e null firstNode f newNode firstNode = new ChainNode( new Character(‘f’), firstNode);

19 first find node whose index is 2
add(3,’f’) a b c d e null firstNode f newNode beforeNode c first find node whose index is 2 next create a node and set its data and link fields ChainNode newNode = new ChainNode(new Character(‘f’), beforeNode.next); finally link beforeNode to newNode beforeNode.next = newNode;

20 Two-Step add(3,’f’) beforeNode = firstNode.next.next;
c d e null firstNode f newNode beforeNode beforeNode = firstNode.next.next; beforeNode.next = new ChainNode(new Character(‘f’), beforeNode.next);


Download ppt "Linked Representation"

Similar presentations


Ads by Google