06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array...

53
Linked Lists Tonga Institute of Higher Education

Transcript of 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array...

Page 1: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked Lists

Tonga Institute of Higher Education

Page 2: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Introduction to Linked Lists Arrays are very useful but there have

disadvantages Searching is slow for unordered arrays Insertion is slow for ordered arrays Deletion is slow for both arrays The size of an array cannot be changed easily

Linked Lists are another way of storing data that solves some of these problems Probably the 2nd most common data structure after

arrays In many cases, you can use a linked list instead of an

array

Page 3: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked Lists

LinkedList object Contains a reference to the first Link object

The reference may be null

Link object Contains data (example: First Name, Last Name) Contains a reference to the next Link object

The reference may be null

Page 4: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Class

Has many methods

Reference to first link

Page 5: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Link Class

Data Reference to next Link

Has method to display data

Constructor to set values

Page 6: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

References - 1 LinkList and Link have variables that contain Link

objects. LinkList.firstLink Link.nextLink

Declare the variable – Tell the computer to reserve a space in memory for the variable.

How does the computer know how much space to reserve in memory if the computer doesn’t know how big a Link object is?

The computer doesn’t need to know how big a Link object is.

The variable does not contain a Link object, but only a pointer / reference to another link.

Pointer / Reference - A variable that contains the address of a location in memory.

Page 7: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

The computer doesn’t need to know how big a Link object is.

The variable does not contain a Link object, but only a pointer / reference to another link.

Pointer / Reference - A variable that contains the address of a location in memory.

Link aLink = new Link();

Link someLink = aLink;

References - 2

Page 8: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked Lists and Arrays Work Differently Array

Each item occupies a particular position. The position can be accessed with an index number

Linked List The only way to find an item is to follow the chain of

items. It’s like human relations:

I ask Sione where Semisi is Sione asks Sela Sela asks Felipe Felipe knows the answer

You can’t access an item directly. You must use relationships between the items to find the item.

Page 9: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Methods – insertFirst() With No Links

Create a new Link1. Set the first variable of

the Linked List to be the new Link

1

New Link

Page 10: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Methods – insertFirst() With At Least 1 Link

Create a new Link

1. Set the next variable of the new Link to be the old first Link.

2. Set the first variable of the Linked List to be the new Link

New Link

Page 11: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

LinkedListinsertFirst() Method

Page 12: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Methods – deleteFirst() With 1 Link

1. Set the first variable of the Linked List variable to be null.

Deleted Link

1

Page 13: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Methods – deleteFirst() With More Than 1 Link

1. Set the first variable of the Linked List variable to be the next link of the old first Link. (If there are no more Links, then the value will be null)

Deleted Link

1

Page 14: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

LinkedListdeleteFirst() Method

Page 15: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Methods - displayData

1. To display data about the Links, we must travel down every Link and call the displayData method for each Link.

Page 16: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

LinkedList displayData() Method

Page 17: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Methods – find(key)

1. To find a Link with a key, we must travel down every Link and check the key for each Link.

Traverse – Traveling down the links

1

Page 18: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

LinkedList2find(key) Method

Page 19: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Methods – delete(key) With 1 Link

1. To delete a Link with a key, we must first find the Link. Therefore, we must travel down every Link and check the key for each Link.

2. Then, we must set the first Link variable of the Linked List to be null.

Deleted Link

1

2

Page 20: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Methods – delete(key) With More Than 1 Link

1. To delete a Link with a key, we must first find the Link. Therefore, we must travel down every Link and check the key for each Link.

2. If the deleted Link is the last Link, then set the next Link variable of the previous Link to be null. Otherwise, set the next Link variable of the previous Link to be the Link after the current Link.

Deleted Link

1

2

Page 21: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

LinkedList2 delete(key) Method

Page 22: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

LinkedList2 Overview

Page 23: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Methods – Other

There are many more methods we could use with a Linked List insertAfter(key) insertBefore(key) insertLast()deleteLast()

Page 24: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Double-Ended Linked Lists - 1

A Double-Ended Linked List is similar to a normal linked list

However, it has 1 additional feature: A reference to the last Link in the Linked List

Page 25: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Double-Ended Linked Lists - 2

Has many methods

Reference to last link

Page 26: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleEndedLinkedList LinkList Class

Page 27: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Double Ended Linked List Methods – insertLast() With No Links

Create a new Link

1. Set the first variable of the Linked List to be the new Link

2. Set the last variable of the Linked List to be the new Link

New Link

1

2

Page 28: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Double Ended Linked List Methods – insertLast() With At Least 1 Link

Create a new Link

1. Set the next variable of the last Link to be the new Link

2. Set the last variable of the Linked List to be the new LinkNew Link

Page 29: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleEndedLinkedListinsertFirst(key) Method

Page 30: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleEndedLinkedList Overview

Page 31: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Sorted Linked List

Sometimes, we want to store data in order In a Sorted Linked List, the items are arranged in

or by key value Advantages over sorted arrays

Faster insertion of new items Linked List can grow in size

Disadvantages under sorted arraysMore difficult to implement

Page 32: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Demonstration

Linked List Applet (Sorted)

Page 33: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Sorted Linked List Methods –insert(key) With No Links

Create a new Link1. Set the first variable of

the Linked List to be the new Link

1

New Link

Page 34: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Sorted Linked List Methods –insert(key) With More Than 1 Link

Create a new Link1. Determine the proper location to put the new Link. We do this by finding the

Link that will have the new item inserted before it. Call this the current Link.2. Set the next Link variable of the previous Link to be the new Link3. If we’re inserting a Link at the end of the Linked List, set the next Link variable

of the new Link to be null4. Otherwise, set the next Link variable of the new Link be the current Link

CurrentLink

PreviousLink

Inserting a newLink in a locationthat is not thebeginning of thelist

1

New2 3

Page 35: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

SortedLinkedListinsert(key) Method

Page 36: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

SortedLinkedList Overview

Page 37: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Doubly Linked Lists The Linked Lists we have examined cannot traverse

backwards A Doubly Linked List allows you to traverse forwards and

backwards through the list Each Link has a reference to the next Link and the

previous Link

Page 38: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleLinkedListLink Class

Page 39: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Doubly Linked List Methods –displayForward() and displayBackward()

To display data about the Links, we must travel down every Link and call the displayData method for each Link

We start at the first Link or the last Link, depending on whether we’re going forwards or backwards

Page 40: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleLinkedListdisplayForward() and displayBackward() Methods

Page 41: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Doubly Linked List Methods –insertFirst() With No Links

Create a new Link1. Set the first Link variable Linked List to be

the new link2. Set the last Link variable Linked List to be

the new link

New Link

1

2

Page 42: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Doubly Linked List Methods –insertFirst() With More Than 1 Link

Create a new Link1. Set the previous link

variable of the old first link to be the new link

2. Set the next link variable of the new link to be the old first link

3. Set the first variable of the Linked List to be the new link

Page 43: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleLinkedList insertFirst() Methods

Page 44: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Doubly Linked List Methods –insertLast()

Create a new Link1. Set the next variable of the old last Link to be the new Link2. Set the previous variable of the new Link to be the old last Link3. Set the last variable of the Linked List to be the new Link

New Link

12

3

Page 45: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleLinkedListinsertLast() Method

Page 46: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Doubly Linked List Methods –insertAfter(key)

Create a new Link Determine Location of

where to put new Link1. Set the next variable of

the new Link to be the Link after the current Link

2. Set the previous variable of the Link after the current Link to be the new Link

3. Set the previous Link of the new Link to be the current Link

4. Set the next Link of the current Link to be the new Link

Page 47: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleLinkedList insertAfter(key) Method

Page 48: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Doubly Linked List Methods –deleteKey(key)

To delete a Link with a key, we must first find the Link. Therefore, we must travel down every Link and check the key for each Link.

1. Set the next variable of the link before the current Link to be the Link after the Current Link.

2. Set the previous variable of the link after the current Link to be the Link before the Current Link.

Page 49: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleLinkedList deleteKey(key) Method

Page 50: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Doubly Linked List Methods -Other Methods deleteFirst() deleteLast()

Page 51: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Code View

DoubleLinkedList Overview

Page 52: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Linked List Efficiency Insertion and Deletion at the beginning of a

Linked List are very fast.Only requires changing 1 or 2 references which takes

O(1) time Finding, Deleting or Inserting Items next to a

specific item is slower Requires searching through, on average, half of the

list (N/2) which is O(N) time However, inserting and deleting is much faster than

an array because nothing needs to be moved.

Page 53: 06 - LinkedLists - TIHE - LinkedLists.pdf · Deletion is slow for both arrays The size of an array cannot be changed easily Linked Lists are another way of storing data that solves

Sorted Linked List Efficiency

Insertion and Deletion of items Requires searching through, on average, half of the

list (N/2) which is O(N) time If an application frequently accesses the

minimum or maximum item and fast insertion is not important, then this is a good choice

For example, a Linked List would be a good option for creating a priority queue if we peek a lot.