1 / 106

Linked Lists in Action

Linked Lists in Action. This chapter introduces linked lists. This presentation shows how to implement the most common operations on linked lists. LINKED LISTS. Collection Classes - LINKED LIST. The Java 2 platform contains a Collections API

ember
Télécharger la présentation

Linked Lists in Action

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists in Action • This chapter introduces linked lists. • This presentation shows how to implement the most common operations on linked lists. LINKED LISTS

  2. Collection Classes - LINKED LIST • The Java 2 platform contains a Collections API • This group of classes represent various data structuresthat store and manage objects • It includes classes such as ArrayList and LinkedList

  3. Abstract Data Types – A Review • An abstract data type (ADT) is : • an organized collection of information containing • a set of operations used to manage that information • The set of operations includes methods such as add, delete , find etc.. • The set of operations/methods define the interface to the ADT

  4. Abstract Data Types • A List ADT, for instance would define the operations that be used with the list, such as add, delete. • We have looked an array implementation of a list, now we will look at a Linked List.

  5. Static vs. Dynamic Structures • A static data structure has a fixed size • Arrays are static; once you define the number of elements it can hold, it doesn’t change.

  6. DRAWBACKS OF ARRAYS • The array implementation of our collection has one serious drawback: • you must know the maximum number of items in your collection when you create it.

  7. Linked Lists • WHAT IF YOU DON’T KNOW THE NUMBER OF ITEMS BEFOREHAND????? • WHAT IF THE NUMBER OF ITEMS WILL VARY OVER TIME? • We need a dynamic data structure that grows and shrinks as necessary

  8. Array Limitations • Arrays • Simple, Fast but • Must specify size at construction time • WHICH GIVES RISE TO “Murphy’s law” • Construct an array with space for n • n = twice your estimate of largest collection • Tomorrow you’ll need n+1

  9. Linked Lists - it is never full A linked list is a very flexible, • dynamic data structure: • no preset size required • items may be added to it or deleted from it at will. • No need to increase capacity when full - • it is never full

  10. student John Smith 40725 3.57 Object References • Recall that an object referenceis a variable that stores the address of an object in memory. • A reference can also be called a pointer (to an object in memory) • and they are often depicted graphically:

  11. John Smith 40725 3.57 Person Jane Jane Jones 58821 3.72 References as Links • Object references can be used to create linksbetween objects • Suppose a Person class contains a reference to another Person object

  12. Self-Referential Objects • A Person object, for instance, could contain a reference variable to another Person object: public class Person { private String name; private String address; private Person next; // a link to another Person object // whatever else }

  13. Intermediate Nodes • Problem: • Objects should not have to deal with the • details of the structure in which they are stored • E.g., the person class stored a link to the next person object in the list

  14. Linked Lists • Instead, we can use a separate node class that : holds a reference to the stored objectand a link to the next node in the list • This type of reference can be used to form a Linked List.

  15. A Linked List of Persons • So by having a separate structure to contain the list of persons: • One object(object 1) is not dependent for its existence on another object (object 2) • And removing object2 would not require removing object1.

  16. Object References - ListNode class • Consider an object that contains a reference to another object of the same type: class ListNode<T> { T data; // object stored in data– e.g. Client object ListNode next; // reference to the next node } data = Client next = refers to next listnode

  17. Two objects of this class can be instantiated and chained together. • The next referenceof one Node object refers to the next node in the list. • The second object’snext reference refers to a third Node object, and so on, • creating a linked list of Node objects. Client 1 next Client2 next

  18. NODE S IN THE LIST • Each node will contain some data as specified by the programmer. • This constitutes a linked list

  19. Linked Nodes • In a linked list, each item is allocated space as it is added to the list. (called dynamically) • A link(next) is kept within each item to the next Client in the list. Client2 next Client 1 next

  20. Each node of the list has two elements : 1. the object being stored in the list, e.g. a client and 2. a reference to the next nodein the list (next) • The next reference in the last node in the list contains a NULL pointerto indicate that it is the end or tailof the list. Client2 next = null Client 1 next

  21. client head Linked Lists • As items are added to a list, • memory for a node is dynamically allocated. (Allocated as needed) • Thus the number of items that may be added to a list • is limited only by the amount of memory available.

  22. HEAD OF THE LIST • The variable (or handle) which holds on to the list is simply a pointer(reference) to the node at the head of the list. • It is called head • Head holds the memory address of the first node Client next = null

  23. Linked List -Flexible space use • Dynamically allocate space for each element as needed • Includes a pointer to the next item • Linked list • Each ListNodeof the list contains • the data item (e.g. a Client) • next - a reference to the next node Data Next object

  24. Linked Lists - WHAT IS HEAD? • Let’s implement a Collection structure – a linked list - • which has a reference to the first node called HEAD • Head is initially NULL Head = null

  25. Data Next object Linked Lists • The Linked List structure has a reference to the list: • head which is initially NULL • TO Add first item to the head of the list • Allocate memory for the ListNode • Set its data reference to some object • Set next to NULL • Set head to reference the new node ListNode Head

  26. Data Data Next Next object2 object1 1 • Add second item to the head of the list Allocate memory for the node - NewNode Set its data reference to some object (object2) Set new node .next to point to the node Head is pointing to Set Head to point to (reference) the new node 2 3 4 Head 1 NewNode FIRST node 4 3 2

  27. Data Data Next Next object2 object1 Linked Lists • Add second item to the head of the list • Allocate memory for the node • Set its data pointer to an object • Set new node next to point to the node Head is pointing to • Set Head to point to new node 1 2 3 4 Head 1 New node node 4 3 2

  28. Constructors of a ListNode class ListNode {amine the constructor for the class. // This is one of the constructors in the ListNode class public ListNode(T initialData) { data = initialData; // data for the node next = null; // reference to next node } where : data - is the initial data of a new node e.g. a client , next - a reference to the next node in the list.

  29. How to start the linked list • Declare a head node to point to the first node in the list . • To create the first node ListNode head = null; // initialize head // now create the first node head = new ListNode (“dan”); Head now refers to a one-node linked list. The data is “dan” and next is equal to null. See previous slide constructor

  30. dan data next Declarations for linked Lists • We will use Strings as data . // Constructor ListNode public class ListNode { private String data; private ListNode next; // constructors } Create the first Node: head = new ListNode (“dan”); Head ListNode null

  31. Declarations for linked Lists • For this example, each node in the linked list is an object of the class ListNode, as shown here with String data jay Head data public class ListNode { private String data; private ListNode next; ... // Constructor sets next to null initally } next dan data nat data next null next

  32. Declarations for linked Lists • Each ListNodealso contains a next referencewhich refers to the next ListNodein the list. Head jay data public class ListNode { private String data; private ListNode next; ... } next dan data nat data next null

  33. jay data next dan data nat data next 52 is the address of the second node • A program keeps track of the FIRST node by STORING ITS ADDRESS IN HEAD. • Head is a reference to a ListNode. • It holds the address of the first node. • Head and next both refer to list nodes • They store the address of the next node 52 next =60 50 50 Head 60 next =52 head 50 is the address of the first node 50 null

  34. NEXT holds an address • In the previous slide, the head node holds the address of the first node which is 50 • The second node’ next reference holds the address of the next node which is 52. • The third node’ next reference holds the address of the next node which is 60.

  35. Declarations for Linked Lists • We represent the empty list by storing • nullin the head reference. • ListNode head = null; head null

  36. The null reference • The null reference is a special JAVA constant. • It means the reference has no address • so it does not refer to anything.

  37. Null references • Null references are used in several instances. • When a object is declared but not instantiated. Client client; Client is initially null • For the next part of the final node in a linked list. • When we have an empty list, the head and tail reference variables are null.

  38. Null Pointer Exceptions • When a reference variable is null, • you cannot call any of the methods associated with variables’s class. E.g.; ListNode head = null; // head is a null referemce head.next; // error - no next reference yet exists. • The second line will result in a NullPointerException.

  39. Inserting Nodes at the front of the list. To add to front of list, we create a reference to the head node and call it head. • Remember head is initially null. • The code to create the head node is: • ListNode head = null;

  40. Inserting an Node at the Front We could use this code if the list is initially empty: ListNode head = null; // create the node -- uses constructor ListNodenewnode = new ListNode(data1, null); head = newnode; //Point head to it OR Below is a shorter version of the previous two lines: ListNode head = new ListNode(data1, null) data1 head null

  41. INSERTING A NODE This is a constructor IN THE Linkedlist class: public ListNode ( T data, ListNodenxt) { data = initialData; next = nxt; } Does the constructor work correctly for the first node on a new list ? head = new ListNode(data1, null); //Where head is initially null

  42. Creating the first node in a list - Review • head = new ListNode( data1, null) • creates a new node with data1 as the data and null is stored in next. • Head now holds the address • of the node with data1 : 50 data1 null Head = 50

  43. Inserting an Node at the Front • Create a new node…red boxes are addresses of nodes. • Head holds the address of the first node - 50 A new node 50 data1 head null 50 Let’s add another node to the front of the list

  44. Inserting an Node at the Front We want to add a new entry, data2, to the front of the linked list shown here. New node 50 head data1 50 null

  45. Inserting an Node at the Front • Create a new node... • Place the data in the new node's data field – this is data2. Create the new node: ListNodenewnode= new ListNode( data2, null) newnode data2 50 head Next = null data1 50 null

  46. Inserting an Node at the Front of the list, reassign head to it • Connect the next (50) reference of newnodeto the node head is currently pointing to. • So now head and newnode.next point to the same node newnode newnode.next data2 next = 50 head 50 50 data1 ListNodenewnode = new ListNode( data2, null) newnode.next = head newnode.next refers to the same node as head null

  47. ListNodenewnode = new Listnode(data2, null) • newnode.next = head; • head = newnode; // we store address 84 in head • data2 is now the new first node of the linked list – its address is 84 Inserting an Node at the Front Newnode’s address is 84 Finally, we point head to the newnode head = newnode; Head holds address of the newnode- 84 84 50 head data1 84 data2 null NEXT = 50

  48. The brute force way to insert the node at the head of the list would be: public void insertFirst(T data) { // create a node ListNodenewnode = new ListNode(data, null); if(isEmpty()) // is the list is empty head = newnode; // point head to the new node else // list is not empty { // set the next pointer of newnode to what head is pointing to newnode.next = head ; head = newnode// point head to newnode } }

  49. CHALLENGE • For homework, extra credit pts on your quiz if you can do the insertion code in the previous slide in one line of code. • You may assume that head has been set to null.

  50. Adding to a list • This strategy is fast and efficient, but each item is added to the head of the list. • An alternative is a list which contains both head and tail pointers: • The code for Add is modified to make a list in which new nodes are added to the list at the end.

More Related