You are on page 1of 35

CS212D: DATA

STRUCTURES

Week 5-6 Linked List 1


2
Outline

 Singly Linked Lists


 Doubly Linked Lists
 Recursions
3
Data Structures and Algorithms

 Data structures
 A data structure is a way of storing data in a computer so that
it can be used efficiently.
 Algorithms
 An algorithm is a finite sequence of well-defined instructions
for solving a problem and guaranteed to terminate in a finite
time.
 An algorithm does not necessarily need to be executable in a
computer, but can be converted into a program.
4
Algorithms versus Heuristics

 Heuristics
 A heuristic is a finite sequence of well-defined instructions for
partially solving a hard problem and guaranteed to terminate in
a finite time.
 A heuristic is not always guaranteed to find a solution while an
algorithm is.
 Descriptions of algorithms
 We will use Java-like pseudo code mixed with English to
describe algorithms.
5
Abstract Data Types

 An Abstract Data Type (ADT) is a specification of a set


of data and the set of operations that can be performed on
the data.
 Such a data type is abstract in the sense that it is independent of
various concrete implementations.
 The definition can be mathematical, or it can be
programmed as an interface.
 We need to implement an ADT by using a class to make it
usable.
LINKED LISTS

6
7
Singly Linked List

 linked list, in its simplest form, is a


collection of nodes that collectively next
form a linear sequence.
 In a singly linked list, each node
stores
 a reference to an object that is an
element of the sequence,
elem node
 as well as a reference to the next node
of the list.

A B C D
Singly Linked List (Example)
8

• The node’s element field refers to an object that is an


element of the sequence (the airport code MSP, in this
example),
• while the next field refers to the subsequent node of the
linked list (or null if there is no further node).

2 Dec 2023 Computer Science Department


Singly Linked List
9

 The linked list instance must keep a reference to


the first node of the list, known as the head.
 Without an explicit reference to the head, there
would be no way to locate that node.
 The last node of the list is known as the tail.
 The tail of a list can be found by traversing the
linked list

2 Dec 2023 Computer Science Department


Implementing a single linked list
10
class

2 Dec 2023 Computer Science Department


Implementing a single linked list
11
class cont.

2 Dec 2023 Computer Science Department


Implementing a single linked list
12
class cont.

2 Dec 2023 Computer Science Department


Inserting an Element at the Head of
13
Single Linked List(1/2)
1. Allocate a new node
2. Insert the new element
3. Have the new node point
to old head
4. Update head to point to
the new node
Inserting an Element at the Head of
14
Single Linked List (2/2)
Inserting an Element at the Tail of
15
Single Linked List (1/2)

1. Allocate a new node


2. Insert the new
element
3. Have the new node
point to null
4. Have the old last node
point to new node
5. Update tail to point to
new node
Inserting an Element at the Tail of
16
Single Linked List(2/2)
Removing an Element from the Head
17
of Single Linked List (1/2)
1. Update head to point to
next node in the list
2. Allow garbage collector
to reclaim the former first
node
Removing an Element from the Head
18
of Single Linked List(2/2)
19
Removing at the Tail (1/2)

 Removing the tail node of


a singly linked list cannot
be done in constant time.
 We need to traverse the
whole linked list to remove
the tail node, which takes
O(n) time, where n is the
number of nodes in the
linked list.
20
Removing at the Tail (2/2)

Algorithm removeLast()
{
if ( size = 0 )
Indicate an error: the list is empty;
else
{ if ( size = 1 ) // only one node
head = null;
tail=null;
else // at least two nodes
{ t = head; /* make t point to the head */
while ( t.getNext != null ) /* find the last node */
{ s = t;
t = t.getNext(); }
s.setNext(null); //null out the next pointer of the last node
}
Exercises
21

 Add a member method called Print to the Single


Linked List class, that prints all the list elements.
 State wither the following sentence is True or
False, explain your answer:
 we cannot easily delete the last node of a singly linked
list.

2 Dec 2023 Computer Science Department


22
Doubly Linked List
 A doubly linked list is a linked list in
which each node keeps an explicit
reference to the node before it and a prev next
reference to the node after it. Each node
stores:
 element
 link to the previous node “prev” elem node
 link to the next node “next”
 Special trailer and header nodes
Inserting with Double Linked List
23

 Every insertion into our doubly linked list


representation will take place between a pair of
existing nodes.

2 Dec 2023 Computer Science Department


Inserting at the Head of the Double
24
Linked List (1/2)

2 Dec 2023 Computer Science Department


Inserting at the Head of the Double
25
Linked List (1/2)

2 Dec 2023 Computer Science Department


Inserting an Element in the Middle
26
of the Double Linked List

2 Dec 2023 Computer Science Department


Inserting an Element at the Trailer
27

2 Dec 2023 Computer Science Department


Deletion with Double Linked List
28

 The deletion of the node proceeds in the opposite


fashion of an insertion.
 The two neighbors of the node to be deleted are
linked directly to each other, thereby bypassing the
original node.
 As a result, that node will no longer be considered
part of the list and it can be reclaimed by the
system.

2 Dec 2023 Computer Science Department


Removing in the Middle
29

2 Dec 2023 Computer Science Department


Implementing a Doubly Linked
30
List Class

2 Dec 2023 Computer Science Department


Implementing a Doubly Linked
31
List Class Cont.

2 Dec 2023 Computer Science Department


Implementing a Doubly Linked
32
List Class Cont.

2 Dec 2023 Computer Science Department


Implementing a Doubly Linked
33
List Class Cont.

2 Dec 2023 Computer Science Department


Exercises
34

 Add a member method called PrintOpposite to the


Double Linked List class, that prints all the double
linked list elements in an opposite order.

2 Dec 2023 Computer Science Department


35
References

1. Chapter 3, Data Structures and Algorithms by


Goodrich and Tamassia.
2. Garbage Collection by Bill Venners
(http://www.artima.com/insidejvm/ed2/gc.html).

You might also like