linked lists tonga institute of higher education

54
Linked Lists Tonga Institute of Higher Education

Upload: ophelia-tucker

Post on 03-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Linked Lists Tonga Institute of Higher Education

Linked Lists

Tonga Institute of Higher Education

Page 2: Linked Lists Tonga Institute of Higher Education

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: Linked Lists Tonga Institute of Higher Education

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 4: Linked Lists Tonga Institute of Higher Education

Linked Lists

LinkedList (LinkList) 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 5: Linked Lists Tonga Institute of Higher Education

Linked List Class

Has many methods

Reference to first link

Page 6: Linked Lists Tonga Institute of Higher Education

Link Class

Data Reference to next Link

Has method to display data

Constructor to set values

Page 7: Linked Lists Tonga Institute of Higher Education

References 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 8: Linked Lists Tonga Institute of Higher Education

Demonstration

Linked List Applet

Page 9: Linked Lists Tonga Institute of Higher Education

Linked List Methods – insertFirst() With No Links

Create a new Link

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

1

New Link

Page 10: Linked Lists Tonga Institute of Higher Education

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: Linked Lists Tonga Institute of Higher Education

Code View

LinkedList

insertFirst() Method

Page 12: Linked Lists Tonga Institute of Higher Education

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: Linked Lists Tonga Institute of Higher Education

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: Linked Lists Tonga Institute of Higher Education

Code View

LinkedList

deleteFirst() Method

Page 15: Linked Lists Tonga Institute of Higher Education

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: Linked Lists Tonga Institute of Higher Education

Code View

LinkedList

displayData() Method

Page 17: Linked Lists Tonga Institute of Higher Education

Code View

LinkedList Overview

Page 18: Linked Lists Tonga Institute of Higher Education

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 19: Linked Lists Tonga Institute of Higher Education

Code View

LinkedList2

find(key) Method

Page 20: Linked Lists Tonga Institute of Higher Education

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 21: Linked Lists Tonga Institute of Higher Education

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 22: Linked Lists Tonga Institute of Higher Education

Code View

LinkedList2

delete(key) Method

Page 23: Linked Lists Tonga Institute of Higher Education

Code View

LinkedList2 Overview

Page 24: Linked Lists Tonga Institute of Higher Education

Linked List Methods – Other

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

Page 25: Linked Lists Tonga Institute of Higher Education

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 26: Linked Lists Tonga Institute of Higher Education

Double-Ended Linked Lists - 2

Has many methods

Reference to last link

Page 27: Linked Lists Tonga Institute of Higher Education

Code View

DoubleEndedLinkedList

LinkList Class

Page 28: Linked Lists Tonga Institute of Higher Education

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 29: Linked Lists Tonga Institute of Higher Education

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 30: Linked Lists Tonga Institute of Higher Education

Code View

DoubleEndedLinkedList

insertFirst(key) Method

Page 31: Linked Lists Tonga Institute of Higher Education

Code View

DoubleEndedLinkedList Overview

Page 32: Linked Lists Tonga Institute of Higher Education

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 arrays More difficult to implement

Page 33: Linked Lists Tonga Institute of Higher Education

Demonstration

Linked List Applet (Sorted)

Page 34: Linked Lists Tonga Institute of Higher Education

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

Create a new Link

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

1

New Link

Page 35: Linked Lists Tonga Institute of Higher Education

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

New

2 3

Page 36: Linked Lists Tonga Institute of Higher Education

Code View

SortedLinkedList

insert(key) Method

Page 37: Linked Lists Tonga Institute of Higher Education

Code View

SortedLinkedList Overview

Page 38: Linked Lists Tonga Institute of Higher Education

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 39: Linked Lists Tonga Institute of Higher Education

Code View

DoubleLinkedList

Link Class

Page 40: Linked Lists Tonga Institute of Higher Education

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 41: Linked Lists Tonga Institute of Higher Education

Code View

DoubleLinkedList

displayForward() and displayBackward() Methods

Page 42: Linked Lists Tonga Institute of Higher Education

Doubly Linked List Methods – insertFirst() With No Links

Create a new Link

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

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

New Link

1

2

Page 43: Linked Lists Tonga Institute of Higher Education

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

Create a new Link

1. 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 44: Linked Lists Tonga Institute of Higher Education

Code View

DoubleLinkedList

insertFirst() Methods

Page 45: Linked Lists Tonga Institute of Higher Education

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

1

2

3

Page 46: Linked Lists Tonga Institute of Higher Education

Code View

DoubleLinkedList

insertLast() Method

Page 47: Linked Lists Tonga Institute of Higher Education

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 48: Linked Lists Tonga Institute of Higher Education

Code View

DoubleLinkedList insertAfter(key) Method

Page 49: Linked Lists Tonga Institute of Higher Education

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 50: Linked Lists Tonga Institute of Higher Education

Code View

DoubleLinkedList deleteKey(key) Method

Page 51: Linked Lists Tonga Institute of Higher Education

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

Page 52: Linked Lists Tonga Institute of Higher Education

Code View

DoubleLinkedList Overview

Page 53: Linked Lists Tonga Institute of Higher Education

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 54: Linked Lists Tonga Institute of Higher Education

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.