Slide02a - Data Structures and Algorithms 1
Slide02a - Data Structures and Algorithms 1
TCC236/05
Table of Contents
3 Unit Objectives
1 Recall
4 Linked list
2 What’s new?
Recall
Table of Contents
3 Unit Objectives
1 Recall
4 Linked list
2 What’s new?
What’s new?
List
Linked lists
Table of Contents
3 Unit Objectives
1 Recall
4 Linked list
2 What’s new?
Objectives
Introduction
List concepts
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
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).
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
L = (a, b, c, d, e)
Find the length of the list: L.size()
L = (a, b, c, d, e)
Find the length of the list: L.size()
=5
Traverse left to right: L.traverse(n)
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()
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 = (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)
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)
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)
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)
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
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
Table of Contents
3 Unit Objectives
1 Recall
4 Linked list
2 What’s new?
Objectives
2 Linked List
Linked organization
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.
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.
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
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.
Traverse
1 Get the address of the first node into temp
Suppose the user wants to insert data 44 at the end of the list
Deletion of a FIRST
node
Deletion of a LAST
node
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.
Advantages of LL
Disadvantages of LL
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.