Professional Documents
Culture Documents
changing.
In this case, we can use list data structure to represent Maria’s list of
grocery items.
List is
Operation Description
List can be implemented using array or linked list. Let’s begin with the array
implementation.
Try out the code given in the code pane for creating an empty list and
observe the result.
import sys
'''This function provides the capacity, size and space left in the list.
def list_details(lst):
print("Capacity:", (sys.getsizeof(lst)-36)//4)
print("Size:", len(lst))
marias_lst=[]
print("List details:")
list_details(marias_lst)
OUTPUT:
List details:
Capacity: 0
Size: 0
Space Left: 0
Now Maria wants to add the first item, Sugar to the list.
Next
Algorithm steps:
add(element):
1. When the list is initially created, it is created
with a certain capacity.
2. While adding the elements, if the list is filled
to the capacity,
a. Create a new list with increased capacity
b. Copy the elements of initial list to the new list
3. Add the element to the end of the existing elements
in the list
Next
Algorithm steps:
insert(pos, element):
1. If the list is filled to capacity
a. Create a new list with increased capacity
b. Copy the elements of initial list to the new list
2. Shift right all the existing elements from index
position (pos) by 1 position
3. Insert the element at index position (pos)