Linked Lists © John Urrutia 2013, All Rights Reserved1.

48
Data Structures & Algorithms Linked Lists © John Urrutia 2013, All Rights Reserved 1

Transcript of Linked Lists © John Urrutia 2013, All Rights Reserved1.

  • Slide 1

Linked Lists John Urrutia 2013, All Rights Reserved1 Slide 2 Good things about arrays Simple Data structure to use when working with lists Provides fast access to items based on ordinal position Easily uses iteration Best use of memory and processing time when retrieving data or when sequentially adding or deleting. John Urrutia 2013, All Rights Reserved2 Slide 3 Problems With Arrays Searching Unordered array is slow Must scan entire array to determine no element exists Ordered Arrays (sorted) are fast Modifying (unordered) Slow when Deleting Must move entire array after deleted element Fast when adding Must add at the end Modifying (ordered) Slow, Slow, Slow Must rearrange all elements to maintain order when adding or deleting John Urrutia 2013, All Rights Reserved3 Slide 4 Linked list The coupon analogy Sunday Clip all of the coupons you think you will use Each coupon is independent of the others Identify the stores to shop Create lists of coupons organized by store (tape, sew or staple the coupons together) Track coupons that can be used in several stores by creating a note for each store identifying which list the coupon is in. Monday Go shopping Take your coupon lists John Urrutia 2013, All Rights Reserved4 Slide 5 Linked Lists Defined Linked List can be used as an alternative to fix array shortfalls Linked Lists are collections of class objects Each element is called a Link Links are made up of basically 2 components storage field(s) reference field to next Link Reference field connect Links MilkBread Eggs BaconNOTHING John Urrutia 2013, All Rights Reserved5 Slide 6 Linked Lists Defined Major differences between arrays and linked lists Array elements are referenced by their ordinal position (using indexes) Linked list elements are referenced by their relationship to the other elements in the list (using Links) MilkBread Eggs BaconNOTHING John Urrutia 2013, All Rights Reserved6 Slide 7 Linked Lists Defined In the example above Bread follows Milk It is not saying the bread is in the 2 nd position When reading the linked list you traverse it from the beginning Link to the ending Link sequentially MilkBread John Urrutia 2013, All Rights Reserved7 Slide 8 Linked Lists Defined Linear linked lists Are accessed sequentially Terminated with a null reference Link Each class object in the list Allocates memory Links to the following member in the list null Link denotes the end of the list MilkBread Eggs BaconNOTHING John Urrutia 2013, All Rights Reserved8 Slide 9 Many linked lists include a special Link called the header Link or first Link A header is nothing more than an object that references the beginning of the list. It contains no data. Only class member to reference the list NOTHING MilkBread Eggs Bacon HEADER John Urrutia 2013, All Rights Reserved9 Linked Lists Defined Slide 10 Inserting a Link Three Steps Create the Link to add - Find the place to insert after - Store the link From Eggs in the new Link Cookies Replace the link in Eggs with Cookies MILKBread Eggs Bacon Nothing Header After inserting CookiesEggs MILKBread Eggs Bacon Nothing Header Cookies John Urrutia 2013, All Rights Reserved10 Slide 11 Removing A Link Three Steps Find the Link to delete - Store the link From Bacon in the link of Cookies Dispose the Bacon Link After removing MILKBread Eggs Bacon Header Nothing Now no Bacon for you!!! (>.