Download presentation
Presentation is loading. Please wait.
Published byOphelia Tucker Modified over 9 years ago
1
Linked Lists Tonga Institute of Higher Education
2
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 2 nd most common data structure after arrays In many cases, you can use a linked list instead of an array
3
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.
4
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
5
Linked List Class Has many methods Reference to first link
6
Link Class Data Reference to next Link Has method to display data Constructor to set values
7
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.
8
Demonstration Linked List Applet
9
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
10
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
11
Code View LinkedList insertFirst() Method
12
Linked List Methods – deleteFirst() With 1 Link 1. Set the first variable of the Linked List variable to be null. Deleted Link 1
13
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
14
Code View LinkedList deleteFirst() Method
15
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.
16
Code View LinkedList displayData() Method
17
Code View LinkedList Overview
18
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
19
Code View LinkedList2 find(key) Method
20
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
21
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
22
Code View LinkedList2 delete(key) Method
23
Code View LinkedList2 Overview
24
Linked List Methods – Other There are many more methods we could use with a Linked List insertAfter(key) insertBefore(key) insertLast() deleteLast()
25
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
26
Double-Ended Linked Lists - 2 Has many methods Reference to last link
27
Code View DoubleEndedLinkedList LinkList Class
28
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
29
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 Link New Link
30
Code View DoubleEndedLinkedList insertFirst(key) Method
31
Code View DoubleEndedLinkedList Overview
32
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
33
Demonstration Linked List Applet (Sorted)
34
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
35
Sorted Linked List Methods – insert(key) With More Than 1 Link Create a new Link 1. 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 Link 3. If we’re inserting a Link at the end of the Linked List, set the next Link variable of the new Link to be null 4. Otherwise, set the next Link variable of the new Link be the current Link Current Link Previous Link Inserting a new Link in a location that is not the beginning of the list 1 New 2 3
36
Code View SortedLinkedList insert(key) Method
37
Code View SortedLinkedList Overview
38
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
39
Code View DoubleLinkedList Link Class
40
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
41
Code View DoubleLinkedList displayForward() and displayBackward() Methods
42
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
43
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
44
Code View DoubleLinkedList insertFirst() Methods
45
Doubly Linked List Methods – insertLast() Create a new Link 1. Set the next variable of the old last Link to be the new Link 2. Set the previous variable of the new Link to be the old last Link 3. Set the last variable of the Linked List to be the new Link New Link 1 2 3
46
Code View DoubleLinkedList insertLast() Method
47
Doubly Linked List Methods – insertAfter(key) Create a new Link Determine Location of where to put new Link 1. 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
48
Code View DoubleLinkedList insertAfter(key) Method
49
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.
50
Code View DoubleLinkedList deleteKey(key) Method
51
Doubly Linked List Methods - Other Methods deleteFirst() deleteLast()
52
Code View DoubleLinkedList Overview
53
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.
54
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.