Lecture03 Array LinkedList PythonList
Lecture03 Array LinkedList PythonList
1
Outline
• Array
• Linked list
• Singly linked list
• Doubly linked list
• Circular linked list
• Python list
2
Introduction
3
Introduction
4
Array
5
Array
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
8
Array Operations
9
Array Operation: access (read)
10
Array Operation: access (read)
12
Array Operation: delete
13
Array Operation: delete
14
Array Operation: insert
15
Array Operation: insert
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
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
• 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
25
Linked List Operations: Insert
NEW NODE
26
Linked List Operations: Insert
NEW NODE
27
Linked List Operations: Insert
NODE A NODE B
NEW NODE
28
Linked List Operations: Insert
• 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
35
Variants of Linked List
36
Singly Linked List
• The following is singly linked list that we learned
already
37
Doubly Linked List
38
Circular Linked List
39
Circular Linked List
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
43
Define LinkedList Class (cont.)
44
Define LinkedList Class (cont.)
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
51
Python List Operations
52
Python List Operations
53
Python List Operations
54
Python List vs Array
55
Python List vs Array
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
58