You are on page 1of 27

CS 241

Data Structures

Chapter
Chapter3.2
3.2&&3.7:
3.7:Doubly
DoublyLinked
LinkedList
List

Dr. Bashir M. Ghandi


ghandib@ucj.edu.sa
Computer Science & Engineering Department
Jubail University College
Objectives
Discuss the following topics:
Doubly Linked List
 Motivation
 Insertion
 Deletion

Lists in java.util
 LinkedList
 ArrayList

CS 241: Data Structures Dr. Bashir M. Ghandi 2


Doubly Linked Lists

Motivation:
The method deleteFromTail() indicates a
problem inherent to singly linked lists:
 Nodes in such lists contain no references to their
predecessors. For this reason, we had to scan the
entire list to find the predecessor of the last node,
which is needed to delete the last node.
 This scanning process makes deleting from tail
inefficient especially for longer lists.
 This and similar problems can be solved by using a
doubly linked list.

CS 241: Data Structures Dr. Bashir M. Ghandi 3


What is Doubly Linked List?

A doubly linked list is a linked list in which


each node has two reference fields, one to the
successor and one to the predecessor

Figure 3.10 A Doubly Linked List

Any disadvantage?

CS 241: Data Structures Dr. Bashir M. Ghandi 4


Implementing a Doubly Linked List
Like SLL, the first step to implement a doubly
linked list is to write a class to represent its node:

Figure 3.10 An implementation of a generic doubly linked list


CS 241: Data Structures Dr. Bashir M. Ghandi 5
Implementing a Doubly Linked List …

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 6
Implementing a Doubly Linked List …
Case B: Non-Empty List

Case A: Empty List

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 7
Implementing a Doubly Linked List …
Case B: Non-Empty List

Case A: Empty List

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 8
Implementing a Doubly Linked List …
Case A: Empty List

Case C: More than One Node List

Case B: One-Node List

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 9
Implementing a Doubly Linked List …
Case A: Empty List

Case C: More than One Node List

Case B: One-Node List

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 10
Implementing a Doubly Linked List …

tmp

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 11
Implementing a Doubly Linked List …

tmp

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 12
Implementing a Doubly Linked List …

tmp

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 13
Implementing a Doubly Linked List …

tmp

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 14
Implementing a Doubly Linked List …

X
tmp

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 15
Implementing a Doubly Linked List …

tmp

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 16
Implementing a Doubly Linked List …

Figure 3.10 (continued)


CS 241: Data Structures Dr. Bashir M. Ghandi 17
Lists in java.util
LinkedList
LinkedList class in the java.util package is an
implementation of various operations of a linked list.
The LinkedList class implements a list as a generic
doubly linked list with references to the head and
the tail.
An instance of such a list that stores integers is
presented in Figure 3.9.

Figure 3.9 A doubly linked list


CS 241: Data Structures Dr. Bashir M. Ghandi 18
Lists in java.util …
Table 3.3 Methods of LinkedList
Method Operation
boolean add(T ob) Insert object ob at the end of the linked list.
void add(int pos, T ob) Insert object ob at position pos after shifting elements at positions
following pos by one; throw IndexOutOfBoundsException if pos is
out of range
void addFirst(T ob) Insert object ob at the beginning of the linked list.
void addLast(T ob) Insert object ob at the end of the linked list; same as add(ob)
void clear() Remove all the objects from the linked list.
boolean contains(Object ob) Return true if the linked list contains the object ob
T get(int pos) Return the object at position pos, throw
IndexOutOfBoundsException if pos is out of range.
T getFirst() Return the first object in the linked list, throw
NOSuchElementException if the linked list is empty.
T getLast() Return the last object in the linked list, throw
NoSuchElemeritException if the linked list is empty.
int indexOf(Object ob) Return the position of the first occurrence of object ob in the
linked list; return -1 if ob is not found.
boolean isEmpty() Return true if the linked list contains no elements, false otherwise

CS 241: Data Structures Dr. Bashir M. Ghandi 19


Lists in java.util …
Table 3.3 Methods of LinkedList (Continued)
T remove() Remove and return the first element on this list.
boolean remove(Object ob) Remove the first Occurrence of ob in the linked list and return
true if ob was in the linked list.
T remove(int pos) Remove the object at position pos; throw
IndexOutOfBoundsException if pos is out of range
T removeFirst() Remove and return the first object on the linked list; throw
NoSuchElementException if the linked list is empty.
T removeLast() Remove and return the last object on the linked list; throw
NoSuchElementException if the linked list is empty.
T set(int pos, T ob) Assign object ob to position pos and return the object that
occupied this position before the assignment; throw
IndexOutOfBoundsException if pos is out of range.
int size() Return the number of objects in the linked list.
Object[] toArray() Copy all objects from the linked list to a newly created array and
return the array.
String toString() Return a string representation of the linked list that contains the
string representation of all the objects.

CS 241: Data Structures Dr. Bashir M. Ghandi 20


Lists in java.util (continued)

Figure 3.22 program demonstrating LinkedList methods


Lists in java.util (continued)

Figure 3.22 program demonstrating LinkedList methods (Continued)


Lists in java.util
ArrayList
The ArrayList class is an array implementation of a list.
ArrayList is equivalent of the class Vector except that
the methods in the Vector class are synchronized
(meaning only one method operates on the list at a
time), whereas methods in the ArrayList class are not.
Like a vector, an array list is a flexible array that is
automatically extended when required by insertion
operations.
Selected list of methods of the ArrayList class is given
in Table 3.4 and an example of application of some
methods is given in Figure 3.23
CS 241: Data Structures Dr. Bashir M. Ghandi 23
Lists in java.util …
Table 3.4 Methods of ArrayList
Method Operation
boolean add(T el) Insert element el at the end of the array list.
void add(int pos, T el) Insert element el at position pos after shifting elements
at positions following pos by one position; throw
IndexOutOfBoundsException if pos is out of range

void clear() Remove all the objects from the array list.
boolean contains(Object ob) Return true if the array list contains the object ob.
void ensureCapacity(int cap) If necessary, increase the capacity of the array list to
accommodate at least cap elements.
T get(int pos) Return the element at position pos; throw
IndexOutOfBoundsException if pos is out of range
int indexOf(Object ob) Return the position of the first occurrence of object ob
in the array list; return -1 if ob is not found.
boolean isEmpty() Return true if the array list contains no elements, false
otherwise

CS 241: Data Structures Dr. Bashir M. Ghandi 24


Lists in java.util …
Table 3.4 Methods of ArrayList (Continued)
Method Operation

boolean remove(Object ob) Remove the first Occurrence of ob in the array list and
return true if ob was in the array list.
T remove(int pos) Remove the object at position pos; throw
IndexOutOfBoundsException if pos is out of range

T set(int pos, T el) Assign el to position pos and return the object that
occupied this position before the assignment; throw
IndexOutOfBoundsException if pos is out of range.

int size() Return the number of objects in the array list.


Object[] toArray() Copy all objects from the array list to a newly created
array and return the array.
void trimToSize() Trim the capacity of this array list to the list's current size.
String toString() Return a string representation of the array list that
contains the string representation of all the objects.

CS 241: Data Structures Dr. Bashir M. Ghandi 25


Lists in java.util …

Figure 3.23 program demonstrating ArrayList methods


CS 241: Data Structures Dr. Bashir M. Ghandi 26
Lists in java.util …

Figure 3.23 program demonstrating ArrayList methods

CS 241: Data Structures Dr. Bashir M. Ghandi 27

You might also like