Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Lecture03 Array LinkedList PythonList

This document provides an overview of arrays and linked lists. It discusses: - Arrays store elements in contiguous memory locations while linked lists store elements at arbitrary locations linked through references. - Common operations on arrays like access, modify, delete and insert have different runtimes - access and modify are O(1) while delete and insert are O(n). - Linked lists access a node by traversing the links, so access is O(n) in general but O(1) for the first/last node if the reference is known.

Uploaded by

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

Lecture03 Array LinkedList PythonList

This document provides an overview of arrays and linked lists. It discusses: - Arrays store elements in contiguous memory locations while linked lists store elements at arbitrary locations linked through references. - Common operations on arrays like access, modify, delete and insert have different runtimes - access and modify are O(1) while delete and insert are O(n). - Linked lists access a node by traversing the links, so access is O(n) in general but O(1) for the first/last node if the reference is known.

Uploaded by

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

MH1403 Algorithms and Computing

Lecture 3 Array, Linked List and


Python List

1
Outline

• Array
• Linked list
• Singly linked list
• Doubly linked list
• Circular linked list
• Python list

2
Introduction

• In this course, we will learn data structures


and algorithms
• Data structure is used for efficiently storing,
accessing, and modifying data

3
Introduction

• In this lecture, we learn two data structures


• Array
• Linked list
• Array and linked list are used to store a
sequence of data
• The elements in an array are stored at contiguous
memory locations
• However, the elements in a linked list are stored at
arbitrary memory locations
• Array and linked-list have their own pros and cons.

4
Array

5
Array

• Array is the simplest and the most widely used


data structure
• Array is used in many programming languages
• C: array, dynamic array
• C++: array, dynamic array, vector
• Java: array, vector
• Python: NumPy array,
Python array module (not commonly used)

6
Array
• The elements of an array are stored at contiguous
memory locations
• The elements of an array are of the same data type.
Each element occupies the same amount of memory
space, so that it is easy to compute the address of i-th
element in the array (when the starting address of the
array is known)
• Example: all elements are 64-bit integers, each element
occupies 8 bytes (Note that in Python, by default there is no
restriction on the memory space used by an integer. But the
integers in NumPy are 32-bit or 64-bit.)
• Example: all elements are 64-bit floating point number, such
as 1.234, each element occupies 8 bytes
• In this course, we consider that the array size can
increase or decrease
• For the strict definition of array, the array size is fixed.

7
Array Example

• Array example: an array A with 8 elements,


each element is the score of a student
Element 65 87 93 72 82 63 72 85
Index 0 1 2 3 4 5 6 7

• The index of the first element is 0, A[0] is 65


• The index of the second element is 1, A[1] is 87
•…
• The index of the last element is 7, A[7] is 85
• We can modify an element easily. For example, to
change the second score to 83, we do: A[1] = 83

8
Array Operations

• There are four basic operations on array


• Access (read) an element on the array
• Modify (write) an element on the array
• Delete an element from the array
• Insert an element into the array

9
Array Operation: access (read)

• On the modern computers, reading the data at any


arbitrary memory address is fast
• We are using random-access memory (RAM) in the
modern computers
• Reading/writing any address in RAM takes almost the
same amount of time
• Access (read) an element of an array is fast
• The memory address of the starting position of the array
is known to the program
• To access the ith element, the program can easily
calculate the memory address of that element (since the
elements are stored in contiguous way and each array
element occupies the same amount of memory space),
then we can read/write that memory address
• The running time of accessing an element of an array with
n elements is constant ➔ O(1)

10
Array Operation: access (read)

• Example: Given an array A


Element 65 87 93 72 82 63 72 85
Index 0 1 2 3 4 5 6 7
• The computer program knows the starting memory
address of array A
• We denote the starting address of A as addrA
• Suppose that each element occupies 8 bytes (int64).
The program can easily calculate the memory address of
any element:
• The memory address of the first element is addrA
• The memory address of the second element is addrA+8
• ……
• The memory address of the last element is addrA+56
• Then the program can access (read) any element quickly
11
Array Operation: modify (write)

• On the modern computers, writing data at any


arbitrary memory address is fast
• Modify (write) an element of an array is fast
(similar to read)
• The memory address of the starting position of the
array is known to the program
• To access the ith element, the program can
calculate the memory address of that element
easily, then we can write data at that memory
address to modify that element
• The running time of modifying an element of an
array with n elements is constant ➔ O(1)

12
Array Operation: delete

• Deleting an element from an array is normally


expensive
• The array elements are stored at contiguous
memory locations
• After deleting the ith element, the program should
shift the original (i+1)th element to the ith position,
shift the original (i+2)th element to the (i+1)th
position, …..
• The worst and average running time of removing
an element of an array with n elements is O(n)
• Deleting the last element is the fastest: O(1)
• Deleting the first element is the slowest: O(n)

13
Array Operation: delete

• Example: Remove A[3] from array A


Element 65 87 93 72 82 63 72 85
Index 0 1 2 3 4 5 6 7

• After removing the element A[3] (which is 72), the


elements A[4], A[5], A[6] and A[7] should be shifted
forward
• The array after removing A[3]:
Element 65 87 93 82 63 72 85
Index 0 1 2 3 4 5 6

14
Array Operation: insert

• Inserting an element into an array is normally


expensive
• The array elements are stored at contiguous
memory locations
• After inserting data to the ith element position, the
program should shift the original ith element to the
(i+1)th position, shift the original (i+1)th element to
the (i+2)th position, …..
• The running time of inserting an element of an
array with n elements is O(n)
• Append data to the last element position is the fastest
• Prepend data to the first element positon is the slowest

15
Array Operation: insert

• Example: Insert 88 at the index 3


Element 65 87 93 72 82 63 72 85
Index 0 1 2 3 4 5 6 7

• Before inserting 88 to the element A[3], the elements


A[3], A[4], A[5], A[6] and A[7] should be shifted
backward
• The array after the insertion:
Element 65 87 93 88 72 82 63 72 85

Index 0 1 2 3 4 5 6 7 8

16
Do I need to use array in an
algorithm/program?
• Advantages of using array
• Fast to access (read) an element
• Fast to modify (write) an element
• Disadvantages of using array
• Not fast to remove an element (except near the end)
• Not fast to insert an element (except near the end)
• In an algorithm, if a long sequence of elements are
seldom removed or inserted, we may use array
• In an algorithm, if a sequence of elements are
frequently removed or inserted, it is expensive to
use array
• Linked list and tree may perform better than array in
some applications for the changing data

17
Linked List

18
Linked List

• A linked list is a series of connected elements.


• An element is called node in linked list
• Each node is stored at arbitrary memory location
• Array elements are stored at contiguous memory locations
• Each node contains at least
• data: A piece of data (any type)
(for example, the name, matri number, scores of a student)
• Next: The reference to the next node (“linked”)
(“Next” is the information that can be used to find the next node )
19
Linked List (cont.)

• The entry point into a linked list is called the


head of the linked list.
• Head is the reference to the first note
• Head is not a node
• The last node points to NULL
• In Python, we can use the term None as NULL
(In C and C++, NULL simply means the value 0)
20
Linked List Operations

• We will learn a few linked list operations


• Access the ith node
• Find a node which contains some particular value
• Insert a node
• Delete a node which contains some particular value

21
Linked List Operations: access

• Access a node
• To access the ith node,
• Use the head information, access the first node
• Use the first node information, access the second node
• Use the second node information, access the third node
• ……

22
Linked List Operations: access

• The running time of accessing a node


• O(1) for accessing the first node
• O(n) for accessing the last node if the reference to
last node is unknown
O(1) for accessing the last node if the reference to
last node is known.
• In general, accessing an arbitrary node takes
O(n) running time
• We need to take into account this running time
when we are choosing which data structure to
use
• Note that accessing an arbitrary array element
is fast: O(1)
23
Linked List Operations: find

• Find a node
• To find a node containing some particular value,
• Use the head information, access the first node, if the
data of the first node satisfies the condition, return the
reference to the first node; otherwise, continue
• Use the first node information, access the second node,
if the data of the second node satisfies the condition,
return the reference to the second node; otherwise,
continue
• ……

24
Linked List Operations: find

• The running time of finding a node in a linked


list containing some particular value: O(n)
• Note that the running time of finding an
element in an array that satisfies some
condition is O(n).
• However, in an sorted array, finding an element that
satisfies some condition is O(log n)

25
Linked List Operations: Insert

• Insert a new node between Node A and Node B


• Step 1: if the new node does not exist, we need to
create a new node (at any free memory location)
NODE A NODE B

NEW NODE

26
Linked List Operations: Insert

• Insert a new node between two nodes


• Step 2: The “Next” of Node A is changed to the
reference to the new node
• Step 3: The “Next” of the new node is changed to
the reference to Node B
NODE A NODE B

NEW NODE
27
Linked List Operations: Insert

• Insert a new node between two nodes


• After inserting the new node, the linked list now
looks like

NODE A NODE B

NEW NODE

28
Linked List Operations: Insert

• The running time of inserting a node


• If we are currently accessing Node X, then inserting
a new node after Node X is always fast
• Normally the time is mainly on accessing the node
before the inserted node
• For example, in the example in the previous slides, we
need to access Node A before we can insert the new
node between Node A and Node B
• If Node A is not near the first node, it is not fast the
access Node A
• Note that inserting any array element is slow:
O(n)
29
Linked List Operations: Delete

• Delete a node
• Step 1. Suppose that we want to delete the
following target node, we need to access the node
before the target node (here, it is Node A)

NODE A NODE B

Target NODE

30
Linked List Operations: Delete

• Delete a node
• Step 2: Change the “Next” of Node A to the
reference to Node B (the node after the target
node)

NODE A NODE B

Target NODE

31
Linked List Operations: Delete

• Delete a node
• Step 3: Change the “Next” of Target Node to NULL
The deleted target node can be kept in memory if
we need it later in the program; otherwise, it can be
completely wiped off from the memory

NODE A NODE B

Target NODE

32
Linked List Operations: Delete

• Delete a node
• After deleting the target node, the linked list looks
like

NODE A NODE B

33
Linked List Operations: Delete

• The running time of deleting a node


• If we are currently accessing Node X, then deleting
a node after Node X is always fast
• The time is mainly on accessing the node in front of
the node being deleted
• For example, in the example in the previous slides, we
need to access Node A before we can delete the target
which is after Node A
• If Node A is not near the first node, it is not fast the
access Node A
• Note that deleting any array element is slow:
O(n)
34
Variants of Linked List

35
Variants of Linked List

• We will learn three types of linked list


• Singly linked list (we learned just now)
• Doubly linked list
• Circular linked list

36
Singly Linked List
• The following is singly linked list that we learned
already

• If we are accessing a node on the singly linked list, we


can access the next node, but we cannot access the
previous node (can only go forward on the linked list)
• If we want to go backward, doubly linked list is
needed

37
Doubly Linked List

• The following is a doubly linked list


• In each node, there is an additional reference “Prev” which
points to the previous node
• From each node, we can access the previous node or the
next node (we can go forward and backward on the list)

38
Circular Linked List

• The following is a circular singly linked list


• The “Next” of the last node contains the reference to the
first node

39
Circular Linked List

• The following is a circular doubly linked list


• The “Next” of the last node contains the reference to the
first node
• The “Prev” of the first node contains the reference to the
last node

40
Implementing Linked List in Python

41
Linked list in Python
• We give part of the Python implementation of the
singly linked list
• The code is available in NTULearn: linkedlist.py
• We will use two classes in the implementation:
Node class and LinkedList class
• Define a class Node, so an object of Node contains
instance variables data and next

class Node:
def __init__(self, inputdata):
self.data = inputdata
self.next = None

42
Define LinkedList Class
class LinkedList:
def __init__(self):
self.head = None
self.size = 0

# add a new node to the beginning of linked list


def addNode(self, inputdata):
newNode = Node(inputdata) # create a new node
newNode.next = self.head
self.head = newNode
self.size+=1

43
Define LinkedList Class (cont.)

# find a node which contains the data matching


# the input value
def findNode(self, value):
curr = self.head
while curr:
if curr.data == value:
return curr
curr = curr.next
return curr

44
Define LinkedList Class (cont.)

# print the data in all the nodes


def printNode(self):
curr = self.head
while curr:
print(curr.data)
curr = curr.next

45
Use the LinkedList class

myList = LinkedList()

for i in range(5):
myList.addNode(2*i) The output:
8
myList.printNode() 6
4
2
0

46
Implement doubly linked list in Python
• To implement the doubly linked list, we can use the
following Node class:

class Node:
def __init__(self, inputdata):
self.data = inputdata
self.prev = None
self.next = None

47
Python list

48
Python list
• Python list is a Python class
• Example: A = [2, 3, 4]
A is an object of Python list. More accurately,
A is the reference to an object of Python list.
• Python list is implemented using array and
objects
• Each element of a Python list is stored in an object
located at some arbitrary memory location
• Python list contains an array storing memory
addresses (references)
• The ith element of the array stores the memory address
of the object of the ith element of the Python list
• It means that the ith element of the array is the reference
to the object of the ith element of the Python list
49
• Example: Python list [2.0, 5, “ABC”]
• Four objects are used to store this Python list
• the object of this Python list, containing an array of
references (memory addresses)
• element 2.0 is stored in a floating-point number object
• element 5 is stored in an integer number object
• element “ABC” is stored in a string object
List object
0x945678763424 0x4878748378 0x259797893890

string object
floating-point object ABC
2.0
int object
5
50
Python List Operations

• There are four basic operations on Python list


• Access (read) an element of the Python list
• Modify (write) an element of the Python list
• Delete an element from the Python list
• Insert an element into the Python list

51
Python List Operations

• The running time of the four operations on Python list are


close to (somehow more expensive than) the running time
of the respective operations on array
• Read and write operations on Python list are fast
• To read an element of Python list, the reference of the object of the
ith element of the list can be found quickly from the array of
references, then we can read that object
• To write to the ith element of Python list, for example, if the old and
new ith element are integer, then a new object containing the new
element is created, and its address is written as the ith element of
the array
• Delete and insert operations on a long Python list are slow
if not near the end of the list
• To delete ith element from Python list, the ith element of the array
of references need to be deleted
• To insert an element into Python list at index i, a reference needs to
be inserted at the index i in the array of references

52
Python List Operations

• There are three possible ways to delete an element


from a Python list
• Python list method pop()
• Syntax: listName.pop(i) , it returns the element at index i, then
delete the element at index i
• Example:
listA = [2, 3, 4]
data = listA.pop(1) # data is 3, listA becomes [2, 4]
• Python list method remove()
• Syntax: listName.remove(data), it removes the first element in
the list that matches the input data
Example: listA = [2, 3, 5, 3, 8]
listA.remove(3) # listA becomes [2, 5, 3, 8]
• Python statement del
• Syntax: del listName[i] , it deletes the element at index i
Example: listA = [2, 3, 4, 5, 6]
del listA[2] # listA becomes [2, 3, 5, 6]

53
Python List Operations

• To insert an element into the Python list, use list


method insert()
• Syntax: listName.insert(i, data) , it inserts data at index i
• Example:
listA = [2, 3, 5, 7]
listA.insert(2, 8) # insert 8 at index 2
print(listA) # print [2, 3, 8, 5, 7]

54
Python List vs Array

• In Python programming, when we need to


use an array to store and process data,
normally Python list is used
• There is Python array, but it is rarely used
• Comparison between Python list and array
• Python list has the advantage that the elements
can be of different data types; while the array
elements must be of the same data type
• Python list needs more memory to store data
comparing to array
• The operations on Python list are more expensive
than those on array

55
Python List vs Array

• In Python programming, the Numpy library


(built using C programming language) is
commonly used for scientific and numeric
computing
• In Numpy, the Numpy array is used extensively for
fast computing in Python

56
Summary
• Array is the simplest and most commonly used data
structure
• Elements should be of the same data type
• Fast to access any element in an array
• Insertion and deletion is slow if not near the end of array
• Linked List
• Elements may be of different data type
• Allows only sequential access
• Insertion and deletion is fast if near the beginning or end of a
linked list (suppose that we use an additional instance variable
self.tail in the linked list class as reference to the last element)
• Insertion and deletion is fast if near a node which is currently
accessed
• Python list
• In Python, when we need array, we normally use Python list

57
Programming Practice on Array and Linked List

• At the following website, in the “category”, choose “array” or


“linked list” for the programming exercises:
https://www.techiedelight.com/data-structures-and-algorithms-
problems/
• Exercises on Python list:
https://www.w3resource.com/python-exercises/list/
• Exercises on linked list:
https://www.w3resource.com/python-exercises/data-structures-and-
algorithms/python-linked-list.php

58

You might also like