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

Lecture 3 - Physical Data Structures

This document discusses various physical data structures including arrays, array lists, linked lists, and their implementations in Java. It summarizes that arrays are fixed size while array lists dynamically increase their size. Linked lists have faster insertion and removal at the beginning and end by linking nodes through references rather than contiguous memory. The document recommends linked lists for frequent first/last element manipulation and array lists for accessing elements by index.

Uploaded by

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

Lecture 3 - Physical Data Structures

This document discusses various physical data structures including arrays, array lists, linked lists, and their implementations in Java. It summarizes that arrays are fixed size while array lists dynamically increase their size. Linked lists have faster insertion and removal at the beginning and end by linking nodes through references rather than contiguous memory. The document recommends linked lists for frequent first/last element manipulation and array lists for accessing elements by index.

Uploaded by

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

ALGORITHMS AND DATA STRUCTURES

LECTURE 3 - PHYSICAL DATA STRUCTURES


Askar Khaimuldin
[email protected]
CONTENT
1. Array
2. ArrayList
3. ArrayList<T>
4. LinkedList
5. LinkedList<T>
6. Performance difference
ARRAY
Array is a data structure of related data items
Static entity (same size throughout program)
A set of variables of the same type
Each element is referred to through a common name
Specific element is accessed using index
The set of data is stored in contiguous memory locations
in index order
ARRAY: DISADVANTAGES
Array is fixed size data structure
It cannot increase by itself
Additional data requires an increase in
size
Why not to create a class that could take
care of it

The complexity of increasing in size is O(N)


Should it be required for each insert?
ARRAYLIST
ArrayList is a variable length Collection class
A class that provides a better API for working
with array
• Insertion
• Deletion
• Altering
• Searching
• Etc.

Increasing process does not happen on every


insert, since we use capacity (buffer)
The capacity is increased according to some
formula, which is completely chosen by the
designer
ARRAYLIST<T>
ArrayList can also be of generic type <T>
In this case, array references must be of more abstract
type Object (language-specific)
It is needed to cast data item to type T for retrieving

The object creation for MyArrayList<T> is as follows:

The type for which you are creating an ArrayList must be


a reference type
Since, it is not possible to handle primitives when using
generics (language-specific)
ARRAYLIST<T>:ITERATORS
An Iterator is an object that can be used to loop through
collections
MyArrayList can also implement an Iterable<T>
interface
It needs iterator() method to be implemented
That method should return an instance of Iterator
Create a private class which implements Iterator<T>
Implement hasNext() and next() methods
See next slide for results…
ARRAYLIST<T>:ITERATORS
ARRAYLIST<T>:DISADVANTAGES
Imagine that you need to add a new item at position 0
Its complexity is O(N), because we need to shift all
elements to the right

The same happens for removing an item at position 0


(shifting all elements to the left)

Hint: There is no need to reduce the capacity (buffer)

Any other solution?


LINKEDLIST
A linked list is a series of connected nodes
Each node contains at least
 A piece of data (any type)
 Reference (or pointer) to the next node in the list

A node is an object that must hold a value and some


references to other nodes
Depending on that, the LinkedList has 3 types
 Singly-linked (each node points to the next node)
 Doubly-linked (each node points to the next and previous nodes)
 Circular-linked (Last node points to the first)
LINKEDLIST
A linked list must have its head (entry point) and
sometimes tail (last element) for better performance
The head must refer to NULL when the linked list is just
created
In order to iterate to the next element, we use:

Adding a new node to the end (when tail is not stored)


Adding a new node when tail is stored
 (complexity is O(1))
LINKEDLIST<T>
LinkedList can also be of generic type <T>
In this case, each node`s data item must be of type T

Then the Linked list can be created for any type:


LINKEDLIST<T>:COMPLEXITIES
Adding an element to the end – O(N)

Adding an element to the end (with tail) – O(1)

Adding an element at the beginning – O(1)


LINKEDLIST<T>:COMPLEXITIES
Removing the last element – O(N)

Removing the last element (with previous) – O(1)

Removing the first element – O(1)


LINKEDLIST<T>
Missed something?
 It is better to check before the action
 It is better to return an element that is removed

get(int index) method`s complexity:


 O(1) for ArrayList
 O(N) for LinkedList

LinkedList is a linear collection of data elements whose order is not given by their physical placement
in memory
LinkedList is our choice when the access to the element at specific position is rarely used
The performance enhancement is revealed during the frequent insertion, deletion and retrieval of both
the first and the last elements ONLY
LINKEDLIST<T>:ITERATORS
MyLinkedList can also implement an Iterable<T>
interface
It needs iterator() method to be implemented
That method should return an instance of Iterator
Create a private class which implements Iterator<T>
Implement hasNext() and next() methods
See next slide for results…
LINKEDLIST<T>:ITERATORS

𝑂(𝑁 2 )
𝑂(𝑁)

𝑂(𝑁)
PERFORMANCE DIFFERENCE
ArrayList`s advantages over LinkedList:
 Accessing an element at specific position
 Less memory usage
 ArrayList is better for storing and accessing data

LinkedList`s advantages over ArrayList:


 Increasing the size
 Adding and removing an element comparatively at
the beginning (also at the end when it is doubly-
linked)
 LinkedList is better for manipulating data
FURTHER MODIFICATIONS (HOMEWORK)
MyArrayList: public void add(T newItem, int index)
MyArrayList: public int find(T keyItem) – returns index or -1
MyArrayList: public T remove(int index) – returns removed element
MyArrayList: public void reverse() – reverses the ArrayList (1,2,3,4 becomes 4,3,2,1)

MyLinkedList: public void add(T newItem, int index)


MyLinkedList: public int find(T keyItem) – returns index or -1
MyLinkedList: public T remove(int index) – returns removed element
MyLinkedList: public void reverse() – reverses the LinkedList

Implement everything (including example methods from this lecture) for MyDoublyLinkedList<T>
LITERATURE
Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne, Addison-Wesley
 Chapter 1.3

Grokking Algorithms, by Aditya Y. Bhargava, Manning


 Chapter 2 – Arrays and Linked Lists
GOOD LUCK!

You might also like