Data Structures and Algorithms
Data Structures and Algorithms
AND ALGORITHMS
1
Outline
● Sequential List using Arrays
● Linked List
● Operations
● Array vs. Linked List
○ Pros and Cons
● Types
2
Static vs Dynamic Data Structure
● An array is a structure of fixed-size data records such that each
element can be efficiently located by its index or (equivalently)
address.
3
Array List
● The array list is basically a self-resizing array or, in other words, a
dynamic array.
● This means that this data structure can grow as much as it needs —
compared to the classical static array which cannot because it has a
predefined maximum size.
● The need for this data structure is most obvious when you have the urge to
store items without knowing how much space you’ll need in advance.
4
● add(element)
● remove(element)
● resize(element)
5
Array’s Limitation
● Arrays
○ Simple,
○ Fast
but
○ Must specify size at construction time
○ Murphy’s law: Anything that can go wrong, will go wrong!
■ Construct an array with space for n
● n = twice your estimate of largest collection
■ Tomorrow you’ll need n+1
○ More flexible system?
6
Therefore there is need of a data structure that dynamically
allocates space for each element as needed
🡺 Linked List!
7
Linked List
● A linked list is a linear data structure, in which the elements
are not stored at contiguous memory locations. The elements
in a linked list are linked using pointers (entity that point to the
next element)
● In simple words, a linked list consists of nodes where each node
contains a data field and a reference (link) to the next node in
the list.
8
9
Working of Linked List
Linked list consists of nodes where each node contains a data field and a
reference (link) to the next node in the link
● Node/Link/Element/Object – Each node in the linked list consists of two
parts
1. Data
2. Link to the next node
● Next – This points to the next node in the
linked list (since they are not stored in the
contiguous memory locations)
10
Operations on Linked List
● Resize/Add
11
Operations on Linked List
● Insert
12
Operations on Linked List
● Remove
13
14
Linked List vs Arrays
● Advantages of Linked List over Arrays
1. Dynamic size
2. Ease of insertion/deletion
● Disadvantages of Linked List over Arrays
1. Random access is not allowed
2. Extra memory space for pointer
3. No cache friendly (array – contiguous)
15
Types of Linked List
16