Chapter 5.3 - Linked Lists - COP 3540 Data Structures with...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 COP 3540 Data Structures with OOP Chapter 5 - Part 3 Linked Lists Doubly-Linked Lists and Iterators
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Doubly Linked Lists How do we track in singly-linked lists? current = current.next; Must always keep link from ‘last’ link for insert() and delete(), etc…. A given link has its data and a forward link! Many applications require both forward and rearward traversal of a linked list. To do this, each link has both forward and rearward (backward) links (pointers). Thus, from any given link, can go either direction.
Background image of page 2
3 Code for class Link class Link { public long dData; // data item public Link next; // next link in list public Link previous; // previous link in list } Downside: each time you insert() a link, you MUST account for two pointers : one to the next link and one to the rear link. Inserting and Deleting can be very complicated. One MUST keep track of the links and change them in a prescribed order (we shall see).
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 null 22; 2.99 first null 22; 2.99 first 44 4.99 null 22; 2.99 first 44 4.99 66; 6.99 Conceptual: Doubly-Linked List null null null These may also be double-ended too (not shown) (Can start at both the beginning and ending of the linked list. Double ends not shown above)
Background image of page 4
5 null 22; 2.99 first null 22; 2.99 first 44 4.99 null 22; 2.99 first 44 4.99 Using Memory Addresses – Doubly-Linked List 5A4 5A4 5A4 5A4 5A4 6B8 5AC 5AC 5AC 6B4 null 6B8 Null null 5AC First: find out where the link should be located in list. Search to find . Found . To add the link at 6B8, we had to change the forward link in 5AC to point to the new link at 6B8, and move 5A4 into the forward link of the new link at location 6B8. Then, need to move backward link in 5A4 to 6B8 and set backward link in 6B8 to 5AC. But s equence is important! Fill in links in the new cell firs t! Remember, you are pointing at 5A4 when you s tart. So, you have all neces s ary addres s es to do job. Here, we inserted a new first link. First: we will inserted new link at front of list Allocated link is at 5AC. Now insert another link where indicated. The link itself is at 6B8. 33 3.99 5A4 6B8 5AC
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Inserting into a doubly-linked list. Now, you may need to insert ‘before’ or insert ‘after’ a link; You are in control. You may have to insert at the ‘beginning’ and insert at the very ‘end’ too. You will almost always have to search to discover where you will insert a new link. (assumes links are ‘ordered’)
Background image of page 6
Deleting a link from a doubly-linked list Must have to search for a hit for the link to be deleted. May be at front of list May be last link of list May be in the middle somewhere in linked list. Item to be deleted may be not found!! Your code must account for all of these!
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/04/2011 for the course COP 3540 taught by Professor Bobroggio during the Fall '11 term at University of South Florida - Tampa.

Page1 / 43

Chapter 5.3 - Linked Lists - COP 3540 Data Structures with...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online