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

Info icon This preview shows pages 1–9. 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
Image of page 1

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

View Full Document Right 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.
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).
Image of page 3

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

View Full Document Right 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)
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
Image of page 5

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

View Full Document Right 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’)
Image of page 6
7 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!
Image of page 7

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

View Full Document Right Arrow Icon
8 Deletion in a doubly-linked list null 22; 2.99 first 44 4.99 5A4 6B8 5AC 6B4 null 6B8 Let’s delete this link.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern