DSA610S Theory 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Namibia University of Science and

Technology
Faculty of Computing and Informatics
Department of Computer Science

DSA610S Data Structures and Algorithms


DSA610S – Theory-2
Semester 1, 2018
 
Dynamic Data Structures
In the previous lecture we looked at data structures in the
form of list, stack and queue
In this lecture we shall be looking at the following Data
Structures:

• Singly Linked List


• Doubly Linked List

2
Objectives
•To understand the nature of dynamic data
structures in the form of singly-linked list,

•To understand the nature of dynamic data


structures in the form of doubly-linked list.

•Deduce that the idea of linked list can be used to


implement linked queues, linked stacks, etc

3
Rationale
• We need to understand these data structures in order
to be able to represent data in a form that we can
operate on them
• Once we have represented these data, then it becomes
easier to develop an algorithm focusing on the given
problem using the chosen data structure
• To be able to select the appropriate data structure for a
given problem

4
Nature of Dynamic Data Structures
• Dynamic data structures can grow and they can shrink
one element at a time

• They are usually composed of objects and links

• Theoretically, there is no limit to the size of a dynamic


data structure

5
List
A list is an ordered collection of elements
supporting random access to each element, much
like an array
• Like arrays, lists make no attempt to preserve the
uniqueness of values, meaning a list may contain
duplicate values
• For example, if you had a list containing the values
“Windhoek”, “Swakopmund”, and “Walvis Bay” and you
were to add “Windhoek” again, you would now find
that the list had grown to include two copies of
“Windhoek”
6
List
These days it is usual to indicate the start of an
index from 0; using java language style of
indexing as follows
Index (memory Value
location)
0 Windhoek
1 Swakopmund
2 Walvis Bay
3 Windhoek

7
Components of a Linked List
• Nodes/Objects:
-Contain data values and object references
• Object references:
-Variables that store the addresses of objects
-Pointer is another name for object reference
-object references can be used to create links between
objects

-Below is a graphical depiction of a singly linked list node

8
Components of a Singly Linked List (continued…)
The Node

Data

Address to next
Data value(s) node

9
Components of a Singly Linked List
(continued…)
Example of Singly Linked List

7 9 5

Head Tail

10
Operations on a List
Operation Description
Add Adds a value to the end of the list. The size of the list
will increase by one
Insert Inserts a value into a list at a specified position (0, 1,
2, . ., n). The size of the list will increase by one. This
means the position must be valid (exists in the current
list)
Delete Deletes the value at a specified position (0, 1, 2, . ., n) in
a list and returns whatever value was contained therein.
The size of the list will decrease by one
Get Obtains the value at a specified position (0, 1, 2, . ., n) in
the list
Size Obtains the number of elements in the list

11
Operations on a Singly Linked List:
Example for Delete Operation
Delete node at the tail position

Head Tail

7 9 5

1 2
Pointer to tail
Pointer to head node
node

Delete address to tail node, by pointing to NULL from the node


with the value 9 in this example.

12
Operations on a Singly Linked List:
Example for Delete Operation (cont…)
Delete node at the head position
Head Tail

7 9 5

2
1
Pointer to tail
Pointer to head node
node

Delete address to next node from the head node, by pointing to


NULL from the node with the value 7 in this example.

13
Operations on a Singly Linked List:
Example for Delete Operation (cont…)
Delete node at a middle position

7 9 5

Head Tail

Delete address to last node, by pointing to NULL from the node


with the value 9 in this example.

14
Operations on a Singly Linked List:
Code-Oriented Examples
Operation on the List
Current ‘students’ List Operation Description
Index Value students.add(“Moses”) It will add Moses at index
0 Johannes 5
1 Hosea students.insert(2, “Moses”) It will insert Moses just
after Hosea; at index 2,
2 Maria this means items above
it will all shift one space
3 Mary down
4 Paul students.delete(4) It will delete Paul
students.get(3) It will returns the student
at index 3; which is Mary
students.size() returns 5

The list is called ‘students’.


Note that the above example has ignored any previous step. 15
Doubly Linked List
The Node

Data

Address to Address to
adjacent node adjacent node

Data value(s)

16
Components of a Doubly Linked List
The Node, the Data, and the Reference

Data

Address to Address to
adjacent node adjacent node

Data value(s)

17
Components of a Doubly Linked List
(continued…)
Example of Doubly Linked List

7 9 5

18
Operations on a Doubly Linked List
Delete node at the tail of the list

Head Tail

7 9 5

1 2
Pointer to tail
Pointer to head node
node

Delete address to tail node, by pointing to NULL from the node


with the value 9 in this example. Also delete address to node
adjacent to tail node (i.e. node with the value 5 in this example),
by making its reference point to NULL.

19
Operations on a Doubly Linked List
(continued…)
Delete node at the head position
Head Tail

7 9 5

2
1
Pointer to tail
Pointer to head node
node

Delete address to next node from the head node, by pointing to


NULL from the node with the value 7 in this example. Also,
delete address to the head node from the adjacent node (i.e.
node with value 9 in this example).
20
Operations on a Doubly Linked List
(continued…)
Delete node at a middle position

7 9 5

Head Tail

Delete all addresses to the node targeted for deletion from


references of its adjacent nodes. Addresses to adjacent nodes
that the target node has MUST also be deleted. In this example
the node containing the value 9 is deleted.

21
Operations on a Linked Lists

Addition and Insertion of Nodes to the List


• First create the node to be added/inserted to the linked list
• Link the node at the desired position by allowing node(s) in the
list to reference (point to) it and allowing the new node to
reference (point to) nodes in the list.
• NOTE: Addition means you are adding the new node to the end
of the linked list.
• NOTE: Insertion means you are putting the new node between
existing nodes.

22
Operations on a Linked Lists

Example: Inserting a New Node to the List

7 5

Newly created
Node
23
Operations on a Linked Lists
(Continued…)

Example: Inserting a New Node to the List (continued..)

7 9 5

Linked List After Inserting a New Node

24
Questions/Comments

?!
25
Take Home Exercise

1. Write a java program that creates a singly


linked list and performs the operations
explained in this lecture.
2. Write a java program that creates a singly linked
list and performs the operations explained in
this lecture.

26
End of Lecture

27

You might also like