You are on page 1of 36

Welcome to new class !

COMP 251: Data Structures and Algorithms.

Omer Waqar (Ph.D., P.Eng., SMIEEE)


COMP 251: DATA STRUCTURES &
ALGORITHMS
REFERENCE: MOST OF THE FIGURES, TABLES, EQUATIONS AND SOME PORTION OF TEXT HAVE BEEN TAKEN
FROM BOOK: DATA STRUCTURES & ALGORITHMS BY M. T. GOODRICH, R. TAMASSIA AND M. H. GOLDWASSER
(SIXTH EDITION).
NOTE: THESE SLIDES ARE ONLY FOR LEARNING AND ACADEMIC PURPOSES DURING THE COURSE, THEREFORE
MUST NOT BE SHARED WITH ANYONE OUTSIDE THE CLASS, IN WHOLE OR IN PART.
Chapter 3: Fundamental Data
Structures
 Arrays
 Singly Linked Lists
 Circularly Linked Lists
 Doubly Linked Lists
 Equivalence Testing

© 2014 Goodrich, Tamassia, Goldwasser Java Primer 3


Array Definition
 An array is a sequenced collection of
variables all of the same type. Each
variable, or cell , in an array has an index ,
which uniquely refers to the value stored in
that cell. The cells of an array, A, are
numbered 0, 1, 2, and so on.
 Each value stored in an array is often called
an elem ent of that array.
A
0 1 2 i n
© 2014 Goodrich, Tamassia, Goldwasser Arrays 4
Array Length and Capacity
 Since the length of an array determines the
maximum number of things that can be stored in
the array, we will sometimes refer to the length of
an array as its capacity .
 In Java, the length of an array named a can be
accessed using the syntax a.length. Thus, the cells
of an array, a, are numbered 0, 1, 2, and so on, up
through a.length−1, and the cell with index k can
be accessed with syntax a[k].
a
0 1 2 k n
© 2014 Goodrich, Tamassia, Goldwasser Arrays 5
Declaring Arrays (first way)
 The first way to create an array is to use an
assignment to a literal form when initially declaring
the array, using a syntax as:

 The elem entType can be any Java base type or


class name, and arrayName can be any valid Java
identifier. The initial values must be of the same type
as the array.

© 2014 Goodrich, Tamassia, Goldwasser Arrays 6


Declaring Arrays (second way)
 The second way to create an array is to use
the new operator.
 However, because an array is not an instance of a
class, we do not use a typical constructor. Instead
we use the syntax:
new elementType[length]
 length is a positive integer denoting the length
of the new array.
 The new operator returns a reference to the
new array, and typically this would be
assigned to an array variable.
© 2014 Goodrich, Tamassia, Goldwasser Arrays 7
Arrays of Characters or
Object References
 An array can store primitive elements, such
as characters.

 An array can also store references to objects.

© 2014 Goodrich, Tamassia, Goldwasser Arrays 8


Java Example: Game Entries
 A game entry stores the name of a player and her best score so far in a game

© 2014 Goodrich, Tamassia, Goldwasser Arrays 9


Java Example: Scoreboard (1)
 Keep track of players and their best scores in an array, board
 The elements of board are objects of class GameEntry
 Array board is sorted by score

© 2014 Goodrich, Tamassia, Goldwasser Arrays 10


Java Example: Scoreboard (2)

© 2014 Goodrich, Tamassia, Goldwasser Arrays 11


Adding an entry

© 2014 Goodrich, Tamassia, Goldwasser Arrays 12


Java Example

© 2014 Goodrich, Tamassia, Goldwasser Arrays 13


Java Example

© 2014 Goodrich, Tamassia, Goldwasser Arrays 14


Singly Linked List
 A singly linked list is a
concrete data structure next
consisting of a sequence
of nodes, starting from a
head pointer
 Each node stores element node
 element
 link to the next node
head

A B C D
Singly Linked Lists 15
Singly Linked List vs. Array
• An important property of a linked list is that it
does not have a predetermined fixed size; it uses
space proportional to its current number of
elements.

• The elements of array are of the same data type,


whereas Linked-List can have elements of
different data types.

Singly Linked Lists 16


A Nested Node Class

Singly Linked Lists 17


Accessor Methods

Singly Linked Lists 18


Inserting at the Head
• Allocate new
node
• Insert new
element
• Have new
node point
to old head
• Update
head to
point to new
node
Singly Linked Lists 19
Inserting at the Tail
• Allocate a new
node
• Insert new
element
• Have new node
point to null
• Have old last node
point to new node
• Update tail to
point to new node

Singly Linked Lists 20


Java Methods

Singly Linked Lists 21


Removing at the Head
• Update head
to point to
next node in
the list
• Allow
garbage
collector to
reclaim the
former first
node

Singly Linked Lists 22


Java Method

Singly Linked Lists 23


Removing at the Tail
• Removing at the tail of a singly linked list is
not efficient!
• There is no constant-time way to update the
tail to point to the previous node

Singly Linked Lists 24


Circularly Linked Lists (1)
• Circularly Linked Lists have
applications in operating systems
(round-robin scheduling)

• A round-robin scheduler could be


implemented with a traditional linked
list, by repeatedly performing the
following steps on linked list L (see
Figure 3.15):

1. process p = L.removeFirst( )
2. Give a time slice to process p
3. L.addLast(p)

© 2014 Goodrich, Tamassia, Goldwasser Circularly Linked Lists 25


Circularly Linked Lists (2)
• We use this model to design and implement a new CircularlyLinkedList class, which
supports all of the public behaviors of our SinglyLinkedList class and one additional update
method:
rotate( ): Moves the first element to the end of the list.
With this new operation, round-robin scheduling can be efficiently implemented by repeatedly
performing the following steps on a circularly linked list C:
1. Give a time slice to process C.first( )
2. C.rotate( )

© 2014 Goodrich, Tamassia, Goldwasser Circularly Linked Lists 26


Rotate()

© 2014 Goodrich, Tamassia, Goldwasser Circularly Linked Lists 27


Doubly Linked List
 A doubly linked list can be traversed prev next
forward and backward
 Nodes store:
 element
link to the previous node element

node
 link to the next node
 Special trailer and header nodes

header nodes/positions trailer

elements
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 28
Insertion
 Insert a new node, q, between p and its successor.
p

A B C
p

A B q C

X
p q

A B X C
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 29
Deletion
 Remove a node, p, from a doubly linked list.
p

A B C D

A B C p

A B C
© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 30
Doubly-Linked List in Java

© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 31


Doubly-Linked List in Java, 2

© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 32


Doubly-Linked List in Java, 3

© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 33


Doubly-Linked List in Java, 4

© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 34


Equivalence Testing with Arrays

© 2014 Goodrich, Tamassia, Goldwasser Equivalence Testing 35


Equivalence Testing with Linked-List

© 2014 Goodrich, Tamassia, Goldwasser Equivalence Testing 36

You might also like