0% found this document useful (0 votes)
3 views

Slide02a - Data Structures and Algorithms 1

Data Structures and Algorithms - Part 1

Uploaded by

hdri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Slide02a - Data Structures and Algorithms 1

Data Structures and Algorithms - Part 1

Uploaded by

hdri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Data Structures and Algorithms

TCC236/05

Week 2 (Part 1): Lists and Possible Operations

(WOU) Data Structures and Algorithms 1 / 44


Recall

Table of Contents

3 Unit Objectives
1 Recall
4 Linked list
2 What’s new?

(WOU) Data Structures and Algorithms 2 / 44


Recall

Recall

General issues of ADT implementation


An introduction to ADT implementation with Java

(WOU) Data Structures and Algorithms 3 / 44


What’s new?

Table of Contents

3 Unit Objectives
1 Recall
4 Linked list
2 What’s new?

(WOU) Data Structures and Algorithms 4 / 44


What’s new?

What’s new?

List
Linked lists

(WOU) Data Structures and Algorithms 5 / 44


Unit Objectives

Table of Contents

3 Unit Objectives
1 Recall
4 Linked list
2 What’s new?

(WOU) Data Structures and Algorithms 6 / 44


Unit Objectives

Objectives

Conceptualize linear lists.


Create and discuss various operations of linked lists.
Implement ADT in linked lists and arrays.
Describe double linked lists and its operations.
Describe circular linked lists and its operations.

(WOU) Data Structures and Algorithms 7 / 44


Unit Objectives

Introduction

In our daily life, we keep lists for different purposes. E.g.:


list of names of our friends,
list for chapters that have already been studied or covered,
list for files which were already stored,
list for songs played,
list for items to be purchased, etc.
Linear collection of data items.
Each list contains the first, second, ... last elements.

(WOU) Data Structures and Algorithms 8 / 44


Unit Objectives

List concepts

A collection of items to keep together.


Examples:
Days of the week:
(Mon, Tue, Wed, Thu, Fri, Sat, Sun)
List of months:
(Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
List of prime numbers less than 15:
(1, 2, 3, 5, 7, 11, 13)
Odd numbers list:
(1, 3, 5, 7, ...)
Operations:
add
delete
update
search
sort
(WOU) Data Structures and Algorithms 9 / 44
Unit Objectives

List concepts (cont.)

They can be represented:


unordered / random
ordered
Linear list could be linked together → linked list.
Linked list:
Each data is embedded in a link.
Each link object contains a reference to the next link in the list of
items.

(WOU) Data Structures and Algorithms 10 / 44


Unit Objectives

Basic Types of Linked Lists

Single linear linked list


Single circular linked list
Double linear linked list
Double circular linked list

(WOU) Data Structures and Algorithms 11 / 44


Unit Objectives

Linear lists
ADT = linear list
A group of items arranged in a linear order.
Operations:
insertion
deletion
reading an item
Defined by its interface (specific methods used to interact with it)
Can be implemented using:
arrays
linked list
A list can be empty or represented as
(e1 , e2 , e3 , e4 , ..., en )
where ei are atoms from some set S, n ≥ 0 is finite and the list size is n
If the list is as follows:
L = (e0 , e1 , e3 , e4 , ..., en−1 )
There are relationships such as:
e0 is the zero-th (front) element
en−1 is the last element
(WOU) Data Structures
e immediately precedes e and Algorithms 12 / 44
Unit Objectives

Linear lists (cont.)

ADT operations:
Creating an empty list.
Checking if other lists are empty or not.
Adding an element in the list at a given position, i-th.
Shift i + 1, i + 2, ...n-th element to i, i + 1, i + 2, ...n − 1-th position.
Then, insert at i-th position.
Removing the element, i-th from the list.
Shift i + 1, i + 2, ...n-th element to i, i + 1, i + 2, ...n − 1-th position.
Getting the element at a particular position
Retrieve the i-th element.
Getting the number of elements in the list
Find the length of list(n).
Traversing a list (left → right or right → left).

(WOU) Data Structures and Algorithms 13 / 44


Unit Objectives

Linear lists (cont.)


Operations Functions
create() Creates an empty list
isEmpty() Checks if the list is empty or not
insert(index, element) Adds the element at index position and shift elements
that are at index to n position by one position next, i.e.
index+1 to n+ 1 position
remove(index) Removes the element that is at index position and shift
the elements from index + 1 to n position by one
position below, i.e.index to n-1 position.
get(index) Gets the element at index position.
size() Gives the number of elements of the list.
store (index, element) Stores element as the index-th element and replaces the
previous index-th element if presents with new value.
traverse() Display the list elements from left to right.

Using the operations listed above, we can design an ADT which has
its instance and operation.
E.g. in a student list:
list of names ≡ instance
store the name, remove the name, etc. ≡ operation
(WOU) Data Structures and Algorithms 14 / 44
Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()

(WOU) Data Structures and Algorithms 15 / 44


Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)

(WOU) Data Structures and Algorithms 15 / 44


Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)
= a, b, c, d, e
Traverse right to left: L.traverse reverse()

(WOU) Data Structures and Algorithms 15 / 44


Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)
= a, b, c, d, e
Traverse right to left: L.traverse reverse()
= e, d, c, b, a
Get element with a given index: L.get(index)

(WOU) Data Structures and Algorithms 15 / 44


Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)
= a, b, c, d, e
Traverse right to left: L.traverse reverse()
= e, d, c, b, a
Get element with a given index: L.get(index)
L.get(0)

(WOU) Data Structures and Algorithms 15 / 44


Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)
= a, b, c, d, e
Traverse right to left: L.traverse reverse()
= e, d, c, b, a
Get element with a given index: L.get(index)
L.get(0)
=a
L.get(2)

(WOU) Data Structures and Algorithms 15 / 44


Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)
= a, b, c, d, e
Traverse right to left: L.traverse reverse()
= e, d, c, b, a
Get element with a given index: L.get(index)
L.get(0)
=a
L.get(2)
=c
L.get(4)

(WOU) Data Structures and Algorithms 15 / 44


Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)
= a, b, c, d, e
Traverse right to left: L.traverse reverse()
= e, d, c, b, a
Get element with a given index: L.get(index)
L.get(0)
=a
L.get(2)
=c
L.get(4)
=e
L.get(-3)

(WOU) Data Structures and Algorithms 15 / 44


Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)
= a, b, c, d, e
Traverse right to left: L.traverse reverse()
= e, d, c, b, a
Get element with a given index: L.get(index)
L.get(0)
=a
L.get(2)
=c
L.get(4)
=e
L.get(-3)
= error
L.get(10)
(WOU) Data Structures and Algorithms 15 / 44
Unit Objectives

ADT list operations example

L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)
= a, b, c, d, e
Traverse right to left: L.traverse reverse()
= e, d, c, b, a
Get element with a given index: L.get(index)
L.get(0)
=a
L.get(2)
=c
L.get(4)
=e
L.get(-3)
= error
L.get(10)
= error
(WOU) Data Structures and Algorithms 15 / 44
Unit Objectives

ADT list operations example (cont.)


Store an element so that the new element has a specified index:
L.store(index, element)
L.store(0,h) → L = (h, b, c, d, e, f, g)
L.store(2,h) → L = (a, b, h, d, e, f, g)
L.store(8,h) → error
L.store(−3,h) → error
Insert an element so that the new element has a specified index:
L.insert(index, element)
L.insert(0,h) → L = (h, a, b, c, d, e, f, g)
L.insert(2,h) → L = (a, b, h, c, d, e, f, g)
L.insert(8,h) → error
L.insert(−3,h) → error
Delete and return an element with a given index: L.remove(index)
L.remove(2) → c and L becomes (a, b, d, e, f, g)
L.remove(−3) → error
L.remove(8) → error
(WOU) Data Structures and Algorithms 16 / 44
Linked list

Table of Contents

3 Unit Objectives
1 Recall
4 Linked list
2 What’s new?

(WOU) Data Structures and Algorithms 17 / 44


Linked list

Objectives

Practice the linear linked list concept.


Apply the various operations on linear linked list such as insert,
delete, traverse, etc.

(WOU) Data Structures and Algorithms 18 / 44


Linked list

How to store a linear list?


1 Array

2 Linked List

(WOU) Data Structures and Algorithms 19 / 44


Linked list

Linked organization

Data can be placed anywhere in the memory


Random access of a data element is not possible.
Insertion and deletion do not require data movement.
Methods of linked organization:
Reference based (dynamic allocation)
Using a link field in an array
Operations performed on a linked list:
Insert new node
Update existing node
Delete node

(WOU) Data Structures and Algorithms 20 / 44


Linked list

(A) Implementation using arrays


Let the list of friends L = (Tom, Mak, Jerry, John)

The link 0 or null indicates the end of the list.


(WOU) Data Structures and Algorithms 21 / 44
Linked list

(A) Implementation using arrays (cont.)

For this example the first element ’Tom’ is stored at the position 5. Index
5 is the start of the list. We can move to the next record by position using
link field. The second record is at the first location, that is at index 1
whose record is ’Mak’. The following is the record at index position 7
which is ’Jerry’ and the last record in the list is ’John’. The link 0 or a null
indicates the end of the list.

(WOU) Data Structures and Algorithms 22 / 44


Linked list

(A) Implementation using arrays (cont.)

Insert Sam between Mak and Jerry, L = (Tom, Mak, Sam, Jerry,
John)
Insert Sam to any empty space.
Set the link for Sam to point where Mak is pointing.
Change the link for Mak to point to Sam.

(WOU) Data Structures and Algorithms 23 / 44


Linked list

(A) Implementation using arrays (cont.)

(WOU) Data Structures and Algorithms 24 / 44


Linked list

(A) Implementation using arrays (cont.)

Delete Jerry from the list L = (Tom, Mak, Sam, Jerry, John)
Follow the link that points to Jerry, i.e Sam
Change Sam’s link to where Jerry is pointing, e.g. 3

(WOU) Data Structures and Algorithms 25 / 44


Linked list

(A) Implementation using arrays (cont.)

(WOU) Data Structures and Algorithms 26 / 44


Linked list

Advantages and Disadvantages of Array Representation

Advantages Disadvantages
Elements can be accessed Fixed size (number of
directly and simply by index elements).
value
Variables are stored in Cannot be dynamically resized
contiguous memory most of the time.
Easy to handle large no. of Wastage if empty locations are
variables of the same data not refilled.
type.

(WOU) Data Structures and Algorithms 27 / 44


Linked list

(B) Implementation using linked list

Java allows an object to be referenced to another.


When you declare a variable in Java, it refers to an object of a given
class, or you are creating a reference to the object.
You have to apply a new operator to create an object reference.
A reference contains the location or address in the memory of an
object.

Figure: Structure of a linked list node

(WOU) Data Structures and Algorithms 28 / 44


Linked list

(B) Implementation using linked list: Declaration

1 //Declaring iref is a reference variable used for locating


2 //Integer object whose value is null
3 Integer iref;
4
5 //Allocating the object with value 10
6 iref = new Integer(10);

(WOU) Data Structures and Algorithms 29 / 44


Linked list

(B) Implementation using linked list: Creating Nodes and


References

(WOU) Data Structures and Algorithms 30 / 44


Linked list

(B) Implementation using linked list: Null assignment

(WOU) Data Structures and Algorithms 31 / 44


Linked list

(B) Implementation using linked list: Changing reference

(WOU) Data Structures and Algorithms 32 / 44


Linked list

(B) Implementation using linked list: Operations


Create
1 Create new node head.
2 Initialize head as null (empty list)
3 Create new node curr.
4 Set data for curr.
5 If head==null, assign first node, i.e. curr to head and assign the same node to prev. This
prev is used to attach new node with previous node:
1 head = curr
2 prev = curr
6 Loop n1 to nx
1 Create new node.
2 Set data.
3 Attach node to previous node for creating link.
4 prev = nx
End loop
7 Create new node End.
8 End = null
9 Attach node to previous node for creating link.
(WOU) Data Structures and Algorithms 33 / 44
Linked list

(B) Implementation using linked list: Operations

Traverse
1 Get the address of the first node into temp

2 Repeat while temp is not null:

1 Process the data of temp


2 Move to the next node by taking address of next node in temp.

(WOU) Data Structures and Algorithms 34 / 44


Linked list

(B) Implementation using linked list: Operations

Insertion of a node in the MIDDLE

Suppose the user wants to insert data 44 between 10 and 23

(WOU) Data Structures and Algorithms 35 / 44


Linked list

(B) Implementation using linked list: Operations

Insertion of a node at the BEGINNING

Suppose the user wants to insert data 44 before 66

(WOU) Data Structures and Algorithms 36 / 44


Linked list

(B) Implementation using linked list: Operations

Insertion of a node at the END

Suppose the user wants to insert data 44 at the end of the list

(WOU) Data Structures and Algorithms 37 / 44


Linked list

(B) Implementation using linked list: Operations

Deletion of a node in the MIDDLE

Suppose the user wants to delete data 23 from the list

(WOU) Data Structures and Algorithms 38 / 44


Linked list

(B) Implementation using linked list: Operations

Deletion of a FIRST
node

Suppose the user wants to delete data 66

(WOU) Data Structures and Algorithms 39 / 44


Linked list

(B) Implementation using linked list: Operations

Deletion of a LAST
node

Suppose the user wants to delete data 77

(WOU) Data Structures and Algorithms 40 / 44


Linked list

Single LL vs Array Implementation

Single LL Array
Elements are stored in linear Elements are stored in linear
order and accessible by order and accessible by index.
pointers
Dynamic size Fixed size
Cannot access the previous Can access the previous
element directly element easily.

(WOU) Data Structures and Algorithms 41 / 44


Linked list

Advantages of LL

Dynamic - can grow or shrink accordingly.


Non-contiguous - the logical sequence of items in the structure is
decoupled from any physical ordering in memory (flexible)
Insertion and deletion can be easy.
Efficient memory utilization - no need to pre-allocate memory.
A node can be easily deleted without moving the other nodes. In
array, we may need to move the elements in order to conserve space.

(WOU) Data Structures and Algorithms 42 / 44


Linked list

Disadvantages of LL

Need additional memory to store pointers.


Nodes are usually read in order from the beginning of the list.
Difficulties arise when traversing in reverse order. Any solution?!

(WOU) Data Structures and Algorithms 43 / 44


Linked list

Applications of LL

Efficient for sorted list that will go through many insertions and
deletions.
E.g. LL can be used to hold the records of students in a school. In
every semester, there are possibilities of new enrolment and graduates
who enter and leave the school system, respectively.

(WOU) Data Structures and Algorithms 44 / 44

You might also like