COMP 213 Advanced Object-oriented Programming Lecture 13 Linked - - PowerPoint PPT Presentation

comp 213
SMART_READER_LITE
LIVE PREVIEW

COMP 213 Advanced Object-oriented Programming Lecture 13 Linked - - PowerPoint PPT Presentation

COMP 213 Advanced Object-oriented Programming Lecture 13 Linked Lists. Linked Lists The array and the linked list are the two primary building blocks for the more complex data structures. Difference: underlying layout of the data in


slide-1
SLIDE 1

COMP 213

Advanced Object-oriented Programming Lecture 13 Linked Lists.

slide-2
SLIDE 2

Linked Lists

  • The array and the linked list are the two primary building blocks for the

more complex data structures.

  • Difference: underlying layout of the data in memory & way of accessing
slide-3
SLIDE 3

Linked Lists

  • The array and the linked list are the two primary building blocks for the

more complex data structures.

  • Difference: underlying layout of the data in memory & way of accessing
slide-4
SLIDE 4

Linked Lists

  • The array and the linked list are the two primary building blocks for the

more complex data structures.

  • Difference: underlying layout of the data in memory & way of accessing

We call this small separate block of memory a “node”

slide-5
SLIDE 5

Linked Lists

  • The linked lists in the Collections library are sufficient for many applications.
  • We need to understand how they work, both to appreciate their limitations,

and also to be able to build custom linked list classes when needed.

slide-6
SLIDE 6

Linked Lists

  • The linked lists in the Collections library are sufficient for many applications.
  • We need to understand how they work, both to appreciate their limitations,

and also to be able to build custom linked list classes when needed.

slide-7
SLIDE 7

Linked Lists

  • The array structure allows us to access any element directly via its index.
  • The linked list structure seems very limited, as its nodes can only be

accessed in a sequential manner.

  • So why bother using Linked Lists?
slide-8
SLIDE 8

Linked Lists

  • The array structure allows us to access any element directly via its index.
  • The linked list structure seems very limited, as its nodes can only be

accessed in a sequential manner.

  • So why bother using Linked Lists?
slide-9
SLIDE 9

Why Linked Lists?

  • The size of an array is fixed. The size of a linked list varies. The nodes of a linked list can be

allocated on an “as needed” basis. When no longer needed, the nodes can be returned to the memory manager.

  • For some operations a linked list is a more efficient implementation approach than an

array, e.g., to place an element into the front of a collection:

slide-10
SLIDE 10

Why Linked Lists?

  • The size of an array is fixed. The size of a linked list varies. The nodes of a linked list can be

allocated on an “as needed” basis. When no longer needed, the nodes can be returned to the memory manager.

  • For some operations a linked list is a more efficient implementation approach than an

array, e.g., to place an element into the front of a collection:

slide-11
SLIDE 11

Why Linked Lists?

  • The size of an array is fixed. The size of a linked list varies. The nodes of a linked list can be

allocated on an “as needed” basis. When no longer needed, the nodes can be returned to the memory manager.

  • For some operations a linked list is a more efficient implementation approach than an

array, e.g., to place an element into the front of a collection:

slide-12
SLIDE 12

Creating our Linked List Class

  • To create a linked list we need to know how to do two things:
  • allocate space for a node dynamically, and
  • allow a node to link to, or reference, another node.
  • Java supplies an operation for dynamically allocating space, an operation we

have been using for all of our objects —the new operation.

  • How can we allow a node to reference another node?
slide-13
SLIDE 13

Creating our Linked List Class

  • To create a linked list we need to know how to do two things:
  • allocate space for a node dynamically, and
  • allow a node to link to, or reference, another node.
  • Java supplies an operation for dynamically allocating space, an operation we

have been using for all of our objects —the new operation.

  • How can we allow a node to reference another node?
slide-14
SLIDE 14

Creating our Linked List Class

  • To create a linked list we need to know how to do two things:
  • allocate space for a node dynamically, and
  • allow a node to link to, or reference, another node.
  • Java supplies an operation for dynamically allocating space, an operation we

have been using for all of our objects —the new operation.

  • How can we allow a node to reference another node?
slide-15
SLIDE 15

Creating our Linked List Class

  • To create a linked list we need to know how to do two things:
  • allocate space for a node dynamically, and
  • allow a node to link to, or reference, another node.
  • Java supplies an operation for dynamically allocating space, an operation we

have been using for all of our objects —the new operation.

  • How can we allow a node to reference another node?
slide-16
SLIDE 16

Self-referencing our Linked List Class

  • Essentially a node in a linked list is an object that holds some important

information, e.g., a string, plus a link to another node.

  • That other node is the exact same type of object —it is also a node in the

linked list.

  • When we define our node class, we must allow the objects created from it to

reference node class objects. We call this type of class a self-referential class.

slide-17
SLIDE 17

Self-referencing our Linked List Class

  • Essentially a node in a linked list is an object that holds some important

information, e.g., a string, plus a link to another node.

  • That other node is the exact same type of object —it is also a node in the

linked list.

  • When we define our node class, we must allow the objects created from it to

reference node class objects. We call this type of class a self-referential class.

slide-18
SLIDE 18

Self-referencing our Linked List Class

  • Essentially a node in a linked list is an object that holds some important

information, e.g., a string, plus a link to another node.

  • That other node is the exact same type of object —it is also a node in the

linked list.

  • When we define our node class, we must allow the objects created from it to

reference node class objects. We call this type of class a self-referential class.

Self-referential class: A class that includes an instance variable or variables that can hold a reference to an object of the same class.

slide-19
SLIDE 19

Creating our Linked List Class

  • We include in the class definition two instance variables:
  • one that holds the important information that the linked list is maintaining (e.g., a

string), and

  • one that is a reference to an object of its same class.
slide-20
SLIDE 20

Example: The LLStringNode Class

public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... }

slide-21
SLIDE 21

Example: The LLStringNode Class

public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... } Instance variable info to hold a reference to the string represented by the node.

slide-22
SLIDE 22

Example: The LLStringNode Class

public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... } Instance variable link to hold a reference to another LLStringNode object.

slide-23
SLIDE 23

Example: The LLStringNode Class

public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... } Instance variable link to hold a reference to another LLStringNode object. That other LLStringNode can hold a reference to a String (info) and a reference to another LLStringNode object, …

slide-24
SLIDE 24

Example: The LLStringNode Class

public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... } Instance variable link to hold a reference to another LLStringNode object. That other LLStringNode can hold a reference to a String (info) and a reference to another LLStringNode object, … Chain ends when a LLStringNode holds the value null in its link.

slide-25
SLIDE 25

Example of LLStringNode linked list

slide-26
SLIDE 26

The LLStringNode Class

public class LLStringNode { ... public LLStringNode(String info) { this.info = info; link = null; } ... }

slide-27
SLIDE 27

The LLStringNode Class

public class LLStringNode { ... public LLStringNode(String info) { this.info = info; link = null; } ... }

E.g., LLStringNode node1 = new LLStringNode(“basketball”); results in:

node1

slide-28
SLIDE 28

How to represent an empty linked list?

  • Declare a variable of the class LLStringNode.
  • Do not instantiate with the new operation.
  • Then, the value held in the string node variable is null:

LLStringNode theList;

slide-29
SLIDE 29

How to represent an empty linked list?

  • Declare a variable of the class LLStringNode.
  • Do not instantiate with the new operation.
  • Then, the value held in the string node variable is null:

LLStringNode theList;

slide-30
SLIDE 30

How to represent an empty linked list?

  • Declare a variable of the class LLStringNode.
  • Do not instantiate with the new operation.
  • Then, the value held in the string node variable is null:

LLStringNode theList;

slide-31
SLIDE 31

The LLStringNode Class (cont.)

public class LLStringNode { ... public void setInfo(String info) { this.info = info; } public String getInfo(){ return info; } public void setLink(LLStringNode link) { this.link = link; } public LLStringNode getLink() { return link; } }

node1

slide-32
SLIDE 32

Example

LLStringNode node1 = new LLStringNode(“basketball”); LLStringNode node2 = new LLStringNode(“baseball”); node1.setLink(node2); results in:

slide-33
SLIDE 33

Example

LLStringNode node1 = new LLStringNode(“basketball”); LLStringNode node2 = new LLStringNode(“baseball”); node1.setLink(node2); results in:

node1 node2

slide-34
SLIDE 34

Standard Linked List Operations

  • Traversal: go through the list and display the information of each node.
  • Insertion: insert a new node in the list –where?
  • Removal: remove a node from the list.
slide-35
SLIDE 35

Traversal

  • We need some way to keep track of our current position in the list.
  • We need to use a variable, currNode, that can reference the current node
  • f the list.
slide-36
SLIDE 36

Traversal

  • We need some way to keep track of our current position in the list.
  • We need to use a variable, currNode, that can reference the current node
  • f the list.

With an array we use an index variable; that will not work here because a linked list is not indexed.

slide-37
SLIDE 37

Traversal

  • We need some way to keep track of our current position in the list.
  • We need to use a variable, currNode, that can reference the current node
  • f the list.
slide-38
SLIDE 38

Pseudocode for Traversal

Set currNode to first node on the list while (currNode is not pointing off the end of the list) display the information at currNode change currNode to point to the next node on the list

slide-39
SLIDE 39

Java code for traversal

LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) display the information at currNode change currNode to point to the next node on the list

slide-40
SLIDE 40

Java code for traversal

LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) display the information at currNode change currNode to point to the next node on the list

slide-41
SLIDE 41

Java code for traversal

LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) System.out.println(currNode.getInfo( )) ; change currNode to point to the next node on the list

slide-42
SLIDE 42

Java code for traversal

LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) System.out.println(currNode.getInfo( )) ; change currNode to point to the next node on the list

slide-43
SLIDE 43

Java code for traversal

LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) System.out.println(currNode.getInfo( )) ; currNode = currNode.getLink();

slide-44
SLIDE 44

Java code for traversal

LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) System.out.println(currNode.getInfo( )) ; currNode = currNode.getLink();

slide-45
SLIDE 45

Java code for traversal

LLStringNode currNode = letters; //letters is the name of our linked list while (currNode != null){ System.out.println(currNode.getInfo( )) ; currNode = currNode.getLink(); }

slide-46
SLIDE 46

Java code for traversal

LLStringNode currNode = letters; //letters is the name of our linked list while (currNode != null){ System.out.println(currNode.getInfo( )) ; currNode = currNode.getLink(); } Does this code still work for empty linked lists? Always double-check when dealing with linked lists!

slide-47
SLIDE 47

Traversal on our linked list

slide-48
SLIDE 48

Traversal on our linked list

slide-49
SLIDE 49

Traversal on our linked list

slide-50
SLIDE 50

Traversal on our linked list

slide-51
SLIDE 51

Traversal on our linked list

slide-52
SLIDE 52

Insertion

We need to consider 3 cases:

  • insertion at the beginning of the list,
  • insertion in the middle of the list, and
  • insertion at the end of the list.
slide-53
SLIDE 53

Insertion at the beginning

slide-54
SLIDE 54

Insertion at the beginning

slide-55
SLIDE 55

Insertion at the beginning

newNode.setLink(letters); letters = newNode; // the order of these lines of //code matters! What happens //if you reverse them?

slide-56
SLIDE 56

Insertion in the middle

slide-57
SLIDE 57

Insertion in the middle

slide-58
SLIDE 58

Insertion in the middle

newNode.setLink(letters.getLink()); letters.setLink(newNode);

slide-59
SLIDE 59

Insertion at the end

slide-60
SLIDE 60

Insertion at the end

slide-61
SLIDE 61

Insertion at the end

LLStringNode currNode = letters; while (currNode.getLink() != null) { currNode = currNode.getLink(); } currNode.setLink(newNode);

slide-62
SLIDE 62

Summary

  • Linked Lists
  • Self-referencing a node class to create a linked list
  • Traversal & Insertion in a Linked List
  • Next: Propositional Logic