Download presentation
Presentation is loading. Please wait.
Published byAnthony Magnus Hopkins Modified over 9 years ago
1
Linked Lists © John Urrutia 2013, All Rights Reserved1
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
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
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
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
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
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
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
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
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
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!!! (>.<) Bacon MILKBread Eggs Bacon Nothing Header Cookies © John Urrutia 2013, All Rights Reserved11
12
Object-Oriented Linked List Design Linked Lists use these two classes Link Class LinkedList Class © John Urrutia 2013, All Rights Reserved12
13
The Link Class Once again a Link is made up of basically two data members The element which stores the Links Data The Link which references the next object in the list Two or more constructor methods are needed; The default, to make empty Link set to null values The parameterized, to assign the Data element and set the Link to null © John Urrutia 2013, All Rights Reserved13
14
EXAMPLE public class Link { public Object elements; //Data to store in this Link public Link next; //Reference to the next Link public Link() //Default constructor creates NULL Link { elements = null; next = null; } //Parameterized constructor populates Link public Link(Object theElements) { elements = theElements; //Data to store in this Link next = null; } © John Urrutia 2013, All Rights Reserved14
15
The LinkedList Class The LinkedList class creates and connects the Links of our list There are MANY methods for adding, removing, traversing, and finding Links in the list. The only data member in this class is the first Link used to designate the beginning of the list. © John Urrutia 2013, All Rights Reserved15
16
Example for header Link public class LinkedList { //Declare the beginning Link of the list protected Link header; //Default constructor for list public LinkedList() { header = new Link("HEADER"); }... } © John Urrutia 2013, All Rights Reserved16
17
The Header Link starts out with the link field set to null “HEADER” NULL Null ??? As you can see the header link is set to a null value Now as we add a new Link check out what happens As you can see the header’s link was attached to the new Link and the new Link now has a link value of null, but how did it know where to insert and how we got there? Example for header (After Instantiation) “HEADER” “NEW ITEM” “NEW ITEM” NULL © John Urrutia 2013, All Rights Reserved17
18
Insert Method To insert a Link into the list you have to say where you want to insert the Link, before or after an existing Link. In the following example we will insert after. First we must find the “after Link” © John Urrutia 2013, All Rights Reserved18
19
The Find( ) Method private Link Find(Object item); { Link current = new Link(); current = header; while(current.elements != item) current = current.next; return current; } © John Urrutia 2013, All Rights Reserved19
20
Insert Method continued. Once we found the Link, we will add our new Link after it. Update the link of the new Link with the link of the found Link Update the link of the found Link with the new Link. © John Urrutia 2013, All Rights Reserved20
21
The Insert( ) Method public void Insert(Object newItem, Object after) { Link current = new Link(); Link newLink = new Link(newItem); current = Find(after); newLink.next = current.next; current.next = newLink; } © John Urrutia 2013, All Rights Reserved21
22
Oops… We put in the wrong thing into our list so now we need to remove it Its still kind of the same processes from earlier. We have this right now HeaderNew LinkNull And we want to take out New Link So it should end out looking like this HeaderNew LinkNull To find the Link that we want to remove we use the FindPrevious method © John Urrutia 2013, All Rights Reserved22
23
FindPrevious() Method Private Link FindPrevious(Object n) { Link current = header; While(!(current.next == null) && (current.next.Elements !=n)) current = current.next; return current; } Now that we found it we would use the remove code to take out what we don’t want © John Urrutia 2013, All Rights Reserved23
24
Remove() Method Public void Remove(Object n) { Link p = FindPrevious(n); if (!(current.next == null)) current.next = current.next.next; } © John Urrutia 2013, All Rights Reserved24
25
Remove method (cont.) The remove method only removes the first occurrence of an item in a linked list Also if the item is not on that list nothing will happen © John Urrutia 2013, All Rights Reserved25
26
Finally there is PrintList It takes the linked list and displays the element fields of each Link on that list Public void PrintList() { Link Current = new Link(); current = header; while(!(current.next == null)) { Console.WriteLine(current.next.Element); current = current.next } © John Urrutia 2013, All Rights Reserved26
27
Double-Ended List This is basically the same as the LinkList class but with one addition. The FirstLastList Contains two Link objects Link first Link last Modifications are needed for the Add and Delete methods to adjust the last element © John Urrutia 2013, All Rights Reserved27
28
ADT –Abstract Data Types Abstract - “considered apart from detailed specifications or implementation” Example: The Office of President – The attributes, responsibility and power are consistent and don’t vary. The President – the person in the Office – comes and goes. To use and ADT you only need to know what data is required & what you can do with that data. How the class does this is and what internal representation of the data is not relevant. © John Urrutia 2013, All Rights Reserved28
29
ADT –Abstract Data Types The data of an ADT is reflected in the public properties fields The actions in the public methods Delegates The interface of an ADT describes all of the signatures needed to implement the data type. © John Urrutia 2013, All Rights Reserved29
30
Doubly Linked List So going forward on a linked list is very easy but going back is well not so much We can make this a lot easier but adding a new field to our Link Class that stores the link from the last Link Figure 11.5 © John Urrutia 2013, All Rights Reserved30 fLinkbLink Data Data1null fLinkbLink Data1 Data2Data fLinkbLink Data2 nullData1
31
But How to do this? First things first We need to Modify the Link Class to add an extra link to the class You are going to need to use two different names for each link Example: fLink for the next Link (forward link) bLink for the last Link (backward link) © John Urrutia 2013, All Rights Reserved31
32
public class Link { public Object Element; //Data for this Link public Link fLink; //Previous Link public Link bLink; //Next Link public Link() //Default Constructor { Element = null; fLink = null; bLink = null; } public Link(Object theElement) //Data Constructor { Element = theElement; fLink = null; bLink = null; } © John Urrutia 2013, All Rights Reserved32
33
Insertion Insertion is almost the same code as the except we have to set the new Link’s back link to point to the pervious Link public void Insert(Object newItem, Object after) { Link current = new Link(); //Insert after Link newLink = new Link(newItem); //New Link To Insert current = Find(after); //Find Link newLink.fLink = current.fLink; //Connect new Link newLink.bLink = current; //to the list current.fLink = newLink; //Repl. current current.fLink.bLink = newLink; //Repl. current } © John Urrutia 2013, All Rights Reserved33
34
Doubly Linked List © John Urrutia 2013, All Rights Reserved34 fLinkbLink Data Data2 null fLinkbLink Data1 fLinkbLink Data2 nullData Data1 newLink.fLink = current.fLink; newLink.bLink = current; current.fLink = newLink; current newLink newLink.fLink.bLink = newLink; Data1 Data2Data
35
Removing The remove method is much simpler to write than in a singularly linked list Find the Link in the list Set the Link’s back link property to the deleted Link’s forward link Set the deleted Link points to and point it to the Link before the deleted Link SOUNDS CONFUSING but is very easy to write © John Urrutia 2013, All Rights Reserved35
36
public void Remove(Object n) { Link deleteLink = Find(n); if (!(deleteLink.fLink == null)) { deleteLink.bLink.fLink = deleteLink.fLink; deleteLink.fLink.bLink = deleteLink.bLink; deleteLink.fLink = null; deleteLink.bLink = null; } © John Urrutia 2013, All Rights Reserved36
37
DataData2 Doubly Linked List © John Urrutia 2013, All Rights Reserved37 fLinkbLink Data Data2 null fLinkbLink Data1 fLinkbLink Data2 nullData Data1 deleteLink.bLink.fLink = deleteLink.fLink; deleteLink.fLink.bLink = deleteLink.bLink; previous deleteLink Data1 Data2 Data following
38
The Circular Linked List Where the last Link Points back to the first Link In the.Net Framework Library, we use the array list data structure while using a circular Linked list Programming example later Header Raymond MikeDavid © John Urrutia 2013, All Rights Reserved38
39
Iterator Class In a linked list you can’t refer to two Links at the same time IF we want to refer to more we use an iterator Iterators contains 3 data fields A field that stores the linked list A field that stores the current Link A field that stores the previous Link © John Urrutia 2013, All Rights Reserved39
40
public class ListIter { private Link current; private Link previous; LinkedList theList; public ListIter(LinkedList list) { theList = list; current = theList.getFirst(); previous = null; } © John Urrutia 2013, All Rights Reserved40
41
Iterators allow us to move from Link to Link through the list. The nextLink method does this: public void nextLink() { previous = current; current = current.link; } Note: Keeping track of the previous Link in addition to the current Link makes insertion and removal easier to perform. © John Urrutia 2013, All Rights Reserved41
42
getCurrent() method It returns the Link pointed to by the iterator public Link getCurrent() { return current; } © John Urrutia 2013, All Rights Reserved42
43
Insertion Method There are two insertion methods in the iterator class InsertBefore: inserts a new Link before the current Link InsertAfter : inserts a new Link after the current Link © John Urrutia 2013, All Rights Reserved43
44
InsertBefore First thing we do is to check and see if we are at the beginning of the list If we are we will throw an exception Otherwise: Set the new Link’s Link field to the previous Link Link field Set the previous Link’s Link field to the new Link Reset the current position to the new Link © John Urrutia 2013, All Rights Reserved44
45
public void InsertBefore(Object theElement) { Link newLink = new Link(theElement); if (current == header) throw new InsertBeforeHeaderException(); Else { newLink.Link = previous.Link; previous.Link = newLink; current = newLink; } public class InsertBeforeHeaderException { public InsertBeforeHeaderException() { base("Can't insert before the header Link."); } © John Urrutia 2013, All Rights Reserved45
46
InsertAfter Since we already know the position of the current Link the method just needs to set the proper links and set the current Link to the next Link © John Urrutia 2013, All Rights Reserved46
47
public void InsertAfter(Object theElement) { Link newLink = new Link(theElement); newLink.Link = current.Link; current.Link = newLink; NextLink(); } © John Urrutia 2013, All Rights Reserved47
48
Remove method Removing a Link is just as easy in the iterator class simply sets the Link field of the previous Link to the Link pointed to by the current Link’s Link field public void Remove() { prevous.Link = current.Link; } © John Urrutia 2013, All Rights Reserved48
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.