Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.

Similar presentations


Presentation on theme: "1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists."— Presentation transcript:

1

2 1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists

3 2 Singly 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

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

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

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

7 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

8 7 Chain Implementation

9 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;}

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

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

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

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

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

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

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

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

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

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

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

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

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

23 22 InsertBack

24 23 Concatenate Lists

25 24 Reverse List

26 25 Circular Lists

27 26 Circular Lists

28 27 Circular Lists

29 28 Linked Stacks

30 29 Linked Stacks

31 30 Linked Lists

32 31 Linked Queues

33 32 Linked Queues

34 33 Linked Queues

35 34 Linked Polynomials

36 35 Linked Polynomials

37 36 Linked Sparse Matrices

38 37 Linked Sparse Matrices

39 38 Doubly Linked Lists

40 39 Doubly Linked Lists

41 40 Doubly Linked Lists

42 41 Doubly Linked Lists

43 42 Doubly Linked Lists

44 43 Exercises Page 184, Exercise 4 Page 208, Exercise 3 Page 225, Exercise 1 Page 226, Exercise 3 Page 227, Exercise 4


Download ppt "1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists."

Similar presentations


Ads by Google