linked lists tonga institute of higher education
TRANSCRIPT
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
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.
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
Linked List Class
Has many methods
Reference to first link
Link Class
Data Reference to next Link
Has method to display data
Constructor to set values
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.
Demonstration
Linked List Applet
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
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
Code View
LinkedList
insertFirst() Method
Linked List Methods – deleteFirst() With 1 Link
1. Set the first variable of the Linked List variable to be null.
Deleted Link
1
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
Code View
LinkedList
deleteFirst() Method
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.
Code View
LinkedList
displayData() Method
Code View
LinkedList Overview
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
Code View
LinkedList2
find(key) Method
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
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
Code View
LinkedList2
delete(key) Method
Code View
LinkedList2 Overview
Linked List Methods – Other
There are many more methods we could use with a Linked List insertAfter(key) insertBefore(key) insertLast()deleteLast()
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
Double-Ended Linked Lists - 2
Has many methods
Reference to last link
Code View
DoubleEndedLinkedList
LinkList Class
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
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
Code View
DoubleEndedLinkedList
insertFirst(key) Method
Code View
DoubleEndedLinkedList Overview
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
Demonstration
Linked List Applet (Sorted)
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
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
Code View
SortedLinkedList
insert(key) Method
Code View
SortedLinkedList Overview
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
Code View
DoubleLinkedList
Link Class
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
Code View
DoubleLinkedList
displayForward() and displayBackward() Methods
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
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
Code View
DoubleLinkedList
insertFirst() Methods
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
Code View
DoubleLinkedList
insertLast() Method
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
Code View
DoubleLinkedList insertAfter(key) Method
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.
Code View
DoubleLinkedList deleteKey(key) Method
Doubly Linked List Methods - Other Methods deleteFirst() deleteLast()
Code View
DoubleLinkedList Overview
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.
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.