Lesson 5 – Linked List
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM BY: AREGATON
A Simple Linked List
Our first example program, linkList.java,
demonstrates a simple linked list. The only
operations allowed in this version of a list
are:
 Inserting an item at the beginning of the list
 Deleting the item at the beginning of the list
 Iterating through the list to display its contents
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 2
The Link Class
You’ve already seen the data part of the Link
class. Here’s the complete class definition:
The linkList.java Program
Listing 5.1 shows the complete linkList.java program. You’ve already seen all the components
except the main() routine.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 3
The linkList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 4
Output:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 5
In main() we create a new list, insert four new links into it with insertFirst(), and display it. Then,
in the while loop, we remove the items one by one with deleteFirst() until the list is empty. The
empty list is then displayed. Here’s the output from linkList.java:
List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99}
Deleted {88, 8.99}
Deleted {66, 6.99}
Deleted {44, 4.99}
Deleted {22, 2.99}
List (first-->last):
Finding and Deleting Specified Links
Our next example program adds methods to
search a linked list for a data item with a specified
key value and to delete an item with a specified
key value. These, along with insertion at the start
of the list, are the same operations carried out by
the LinkList Workshop applet. The complete
linkList2.java program is shown in Listing 5.2.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 6
Finding and Deleting Specified Links
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 7
Finding and Deleting Specified Links
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 8
Output:
The main() routine makes a list, inserts four items, and displays the resulting list. It then
searches for the item with key 44, deletes the item with key 66, and displays the list again.
Here’s the output:
List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99}
Found link with key 44
Deleted link with key 66
List (first-->last): {88, 8.99} {44, 4.99} {22, 2.99}
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 9
LISTING 5.3 The firstLastList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 10
LISTING 5.3 The firstLastList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 11
LISTING 5.3 The firstLastList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 12
For simplicity, in this program we’ve reduced the number of
data items in each link from two to one. This makes it easier to
display the link contents. (Remember that in a serious program
there would be many more data items, or a reference to
another object containing many data items.) This program
inserts three items at the front of the list, inserts three more at
the end, and displays the resulting list. It then deletes the first
two items and displays the list again.
Here’s the output:
List (first-->last): 66 44 22 11 33 55
List (first-->last): 22 11 33 55
LISTING 5.4 The linkStack.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 13
LISTING 5.4 The linkStack.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 14
LISTING 5.4 The linkStack.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 15
Output:
The main() routine creates a stack object, pushes two items on it, displays the stack, pushes two
more items, and displays the stack again. Finally, it pops two items and displays the stack a third
time.
Here’s the output:
Stack (top-->bottom): 40 20
Stack (top-->bottom): 80 60 40 20 Stack (top-->bottom): 40 20
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 16
The sortedList.java Program
The sortedList.java example shown in Listing 5.6
presents a SortedList class with insert(),
remove(), and displayList() methods. Only the
insert() routine is different from its counterpart
in non-sorted lists.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 17
The sortedList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 18
The sortedList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 19
Output:
In main() we insert two items with key values 20 and 40. Then we insert three more
items, with values 10, 30, and 50. These values are inserted at the beginning of the
list, in the middle, and at the end, showing that the insert() routine correctly
handles these special cases. Finally, we remove one item, to show removal is always
from the front of the list. After each change, the list is displayed.
Here’s the output from sortedList.java:
List (first-->last): 20 40
List (first-->last): 10 20 30 40 50
List (first-->last): 20 30 40 50
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 20
LISTING 5.7 The listInsertionSort.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 21
LISTING 5.7 The listInsertionSort.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 22
LISTING 5.7 The listInsertionSort.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 23
This program displays the values in the array
before the sorting operation and again
afterward.
Here’s some sample output:
Unsorted array: 59 69 41 56 84 15 86 81 37 35
Sorted array: 15 35 37 41 56 59 69 81 84 86
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 24
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 25
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 26
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 27
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 28
In main() we insert some items at the beginning of the list
and at the end, display
the items going both forward and backward, delete the
first and last items and the
item with key 11, display the list again (forward only),
insert two items using the
insertAfter() method, and display the list again.
Here’s the output:
List (first-->last): 66 44 22 11 33 55
List (last-->first): 55 33 11 22 44 66
List (first-->last): 44 22 33
List (first-->last): 44 22 77 33 88
Iterator Methods
Additional methods can make the iterator a flexible and powerful class. All operations previously performed by
the class that involve iterating through the list, such as insertAfter(), are more naturally performed by the iterator.
In our example the iterator includes the following methods:
• reset()—Sets the iterator to the start of the list
• nextLink()—Moves the iterator to the next link
• getCurrent()—Returns the link at the iterator
• atEnd()—Returns true if the iterator is at the end of the list
234 CHAPTER 5 Linked Lists
• insertAfter()—Inserts a new link after the iterator
• insertBefore()—Inserts a new link before the iterator
• deleteCurrent()—Deletes the link at the iterator
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 29
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 30
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 31
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 32
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 33
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 34
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 35
Output:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 36
The main() routine inserts four items into the list, using an iterator and its insertAfter() method.
Then it waits for the user to interact with it. In the following sample interaction, the user
displays the list, resets the iterator to the beginning, goes forward two links, gets the current
link’s key value (which is 60), inserts 100 before this, inserts 7 after the 100, and displays the list
again:
Summary
A linked list consists of one linkedList object and a number of Link objects.
• The linkedList object contains a reference, often called first, to the first link
in the list.
• Each Link object contains data and a reference, often called next, to the next
link in the list.
• A next value of null signals the end of the list.
• Inserting an item at the beginning of a linked list involves changing the new
link’s next field to point to the old first link and changing first to point to the
new item.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 37
Summary
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 38
Stacks and queues are ADTs. They can be implemented using either arrays or linked lists.
• In a sorted linked list, the links are arranged in order of ascending (or sometimes descending) key
value.
• Insertion in a sorted list takes O(N) time because the correct insertion point must be found.
Deletion of the smallest link takes O(1) time.
• In a doubly linked list, each link contains a reference to the previous link as well as the next link.
• A doubly linked list permits backward traversal and deletion from the end of the list.
• An iterator is a reference, encapsulated in a class object, that points to a link in an associated list.
• Iterator methods allow the user to move the iterator along the list and access the link currently
pointed to.
• An iterator can be used to traverse through a list, performing some operation
on selected links (or all links).

Lesson 5 link list

  • 1.
    Lesson 5 –Linked List 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM BY: AREGATON
  • 2.
    A Simple LinkedList Our first example program, linkList.java, demonstrates a simple linked list. The only operations allowed in this version of a list are:  Inserting an item at the beginning of the list  Deleting the item at the beginning of the list  Iterating through the list to display its contents 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 2 The Link Class You’ve already seen the data part of the Link class. Here’s the complete class definition:
  • 3.
    The linkList.java Program Listing5.1 shows the complete linkList.java program. You’ve already seen all the components except the main() routine. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 3
  • 4.
    The linkList.java Program 4/4/2021PC-108 DATA STRUCTURE AND ALGORITHM 4
  • 5.
    Output: 4/4/2021 PC-108 DATASTRUCTURE AND ALGORITHM 5 In main() we create a new list, insert four new links into it with insertFirst(), and display it. Then, in the while loop, we remove the items one by one with deleteFirst() until the list is empty. The empty list is then displayed. Here’s the output from linkList.java: List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99} Deleted {88, 8.99} Deleted {66, 6.99} Deleted {44, 4.99} Deleted {22, 2.99} List (first-->last):
  • 6.
    Finding and DeletingSpecified Links Our next example program adds methods to search a linked list for a data item with a specified key value and to delete an item with a specified key value. These, along with insertion at the start of the list, are the same operations carried out by the LinkList Workshop applet. The complete linkList2.java program is shown in Listing 5.2. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 6
  • 7.
    Finding and DeletingSpecified Links 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 7
  • 8.
    Finding and DeletingSpecified Links 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 8
  • 9.
    Output: The main() routinemakes a list, inserts four items, and displays the resulting list. It then searches for the item with key 44, deletes the item with key 66, and displays the list again. Here’s the output: List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99} Found link with key 44 Deleted link with key 66 List (first-->last): {88, 8.99} {44, 4.99} {22, 2.99} 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 9
  • 10.
    LISTING 5.3 ThefirstLastList.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 10
  • 11.
    LISTING 5.3 ThefirstLastList.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 11
  • 12.
    LISTING 5.3 ThefirstLastList.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 12 For simplicity, in this program we’ve reduced the number of data items in each link from two to one. This makes it easier to display the link contents. (Remember that in a serious program there would be many more data items, or a reference to another object containing many data items.) This program inserts three items at the front of the list, inserts three more at the end, and displays the resulting list. It then deletes the first two items and displays the list again. Here’s the output: List (first-->last): 66 44 22 11 33 55 List (first-->last): 22 11 33 55
  • 13.
    LISTING 5.4 ThelinkStack.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 13
  • 14.
    LISTING 5.4 ThelinkStack.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 14
  • 15.
    LISTING 5.4 ThelinkStack.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 15
  • 16.
    Output: The main() routinecreates a stack object, pushes two items on it, displays the stack, pushes two more items, and displays the stack again. Finally, it pops two items and displays the stack a third time. Here’s the output: Stack (top-->bottom): 40 20 Stack (top-->bottom): 80 60 40 20 Stack (top-->bottom): 40 20 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 16
  • 17.
    The sortedList.java Program ThesortedList.java example shown in Listing 5.6 presents a SortedList class with insert(), remove(), and displayList() methods. Only the insert() routine is different from its counterpart in non-sorted lists. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 17
  • 18.
    The sortedList.java Program 4/4/2021PC-108 DATA STRUCTURE AND ALGORITHM 18
  • 19.
    The sortedList.java Program 4/4/2021PC-108 DATA STRUCTURE AND ALGORITHM 19
  • 20.
    Output: In main() weinsert two items with key values 20 and 40. Then we insert three more items, with values 10, 30, and 50. These values are inserted at the beginning of the list, in the middle, and at the end, showing that the insert() routine correctly handles these special cases. Finally, we remove one item, to show removal is always from the front of the list. After each change, the list is displayed. Here’s the output from sortedList.java: List (first-->last): 20 40 List (first-->last): 10 20 30 40 50 List (first-->last): 20 30 40 50 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 20
  • 21.
    LISTING 5.7 ThelistInsertionSort.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 21
  • 22.
    LISTING 5.7 ThelistInsertionSort.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 22
  • 23.
    LISTING 5.7 ThelistInsertionSort.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 23 This program displays the values in the array before the sorting operation and again afterward. Here’s some sample output: Unsorted array: 59 69 41 56 84 15 86 81 37 35 Sorted array: 15 35 37 41 56 59 69 81 84 86
  • 24.
    The doublyLinked.java Program 4/4/2021PC-108 DATA STRUCTURE AND ALGORITHM 24
  • 25.
    The doublyLinked.java Program 4/4/2021PC-108 DATA STRUCTURE AND ALGORITHM 25
  • 26.
    The doublyLinked.java Program 4/4/2021PC-108 DATA STRUCTURE AND ALGORITHM 26
  • 27.
    The doublyLinked.java Program 4/4/2021PC-108 DATA STRUCTURE AND ALGORITHM 27
  • 28.
    The doublyLinked.java Program 4/4/2021PC-108 DATA STRUCTURE AND ALGORITHM 28 In main() we insert some items at the beginning of the list and at the end, display the items going both forward and backward, delete the first and last items and the item with key 11, display the list again (forward only), insert two items using the insertAfter() method, and display the list again. Here’s the output: List (first-->last): 66 44 22 11 33 55 List (last-->first): 55 33 11 22 44 66 List (first-->last): 44 22 33 List (first-->last): 44 22 77 33 88
  • 29.
    Iterator Methods Additional methodscan make the iterator a flexible and powerful class. All operations previously performed by the class that involve iterating through the list, such as insertAfter(), are more naturally performed by the iterator. In our example the iterator includes the following methods: • reset()—Sets the iterator to the start of the list • nextLink()—Moves the iterator to the next link • getCurrent()—Returns the link at the iterator • atEnd()—Returns true if the iterator is at the end of the list 234 CHAPTER 5 Linked Lists • insertAfter()—Inserts a new link after the iterator • insertBefore()—Inserts a new link before the iterator • deleteCurrent()—Deletes the link at the iterator 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 29
  • 30.
    LISTING 5.9 TheinterIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 30
  • 31.
    LISTING 5.9 TheinterIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 31
  • 32.
    LISTING 5.9 TheinterIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 32
  • 33.
    LISTING 5.9 TheinterIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 33
  • 34.
    LISTING 5.9 TheinterIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 34
  • 35.
    LISTING 5.9 TheinterIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 35
  • 36.
    Output: 4/4/2021 PC-108 DATASTRUCTURE AND ALGORITHM 36 The main() routine inserts four items into the list, using an iterator and its insertAfter() method. Then it waits for the user to interact with it. In the following sample interaction, the user displays the list, resets the iterator to the beginning, goes forward two links, gets the current link’s key value (which is 60), inserts 100 before this, inserts 7 after the 100, and displays the list again:
  • 37.
    Summary A linked listconsists of one linkedList object and a number of Link objects. • The linkedList object contains a reference, often called first, to the first link in the list. • Each Link object contains data and a reference, often called next, to the next link in the list. • A next value of null signals the end of the list. • Inserting an item at the beginning of a linked list involves changing the new link’s next field to point to the old first link and changing first to point to the new item. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 37
  • 38.
    Summary 4/4/2021 PC-108 DATASTRUCTURE AND ALGORITHM 38 Stacks and queues are ADTs. They can be implemented using either arrays or linked lists. • In a sorted linked list, the links are arranged in order of ascending (or sometimes descending) key value. • Insertion in a sorted list takes O(N) time because the correct insertion point must be found. Deletion of the smallest link takes O(1) time. • In a doubly linked list, each link contains a reference to the previous link as well as the next link. • A doubly linked list permits backward traversal and deletion from the end of the list. • An iterator is a reference, encapsulated in a class object, that points to a link in an associated list. • Iterator methods allow the user to move the iterator along the list and access the link currently pointed to. • An iterator can be used to traverse through a list, performing some operation on selected links (or all links).