List Impelemantation
List Impelemantation
List Impelemantation
implementation this list using linkedlist in python .Before starting the implementation process, let's take
a quick and comprehensive overview of list in Python and its functions that we will be implementation
it.
❖ Creating List
❖ Accessing Elements :
Index access by index
Slicing to extract a portion of a list
❖ List Length by use The len() function to give us the number of items in a list.
❖ Adding Elements:
append(item): adds an item to the end of the list.
insert(index, item): inserts an item at a specific index.
Extend(): appending elements from the iterable.
❖ Removing Elements:
pop(index): removes and returns the item at the specified index
remove(item): removes the first occurrence of the specified item
Clear to removes all elements from the list
❖ Search and Count:
index(item): to find and returns the index of the first occurrence of the item.
count(item): to returns the number of occurrences of the item (number of item in list)
❖ Sorting and Revers:
sort(): to sorts the list in ascending order.
reverse(): to reverses the order of the list.
❖ Copy and Concatenate:
Copying List: to Return a shallow copy of the list.
Concatenation: to combine two lists using the "+" operator.
EXAMPLE:
❖ Creating List
# Creating a list1
list1 = [5, 2, 8, 1, 3]
list2 = ['m', 'a', 'g', 'e', 'd']
list3 = [1, 2.3, 'string', {'dicKey':'dicValue'}, [], True]
❖ Accessing Elements :
# -- Accessing Elements -- #
# index
list1[0] # Return 5
list1[-1] # Return 3
# We can change the value as list1[0] = 7 and so on …
# Slicing
list1[2:5] # Return [8, 1, 3]
# We can change the value as list1[2:5] = [1, 'a', 2]
list1[2:5:2] # Return [8, 3]
❖ List Length
# -- Length --#
len(list1) # Return 5
❖ Adding Elements:
# -- Adding Elements: -- #
# Append
list1.append('string') # Return [5, 2, 8, 1, 3, 'string']
# Insert
list1.insert(2, [1,2]) # Return [5, 2, [1, 2], 8, 1, 3, 'string']
# Extend
list1.extend([9, 10]) # Return [5, 2, [1, 2], 8, 1, 3, 'string', 9, 10]
❖ Removing Elements:
# -- Removing Elements: -- #
# Pop
poppedElement = list1.pop(4) # poppedElement = 1, list1 = [5, 2, [1, 2], 8, 3, 'string']
# Remove
list1.remove(2) # list1 = [5, [1, 2], 8, 3, 'string']
# Clear
list3.clear() # Return []
# Count
countOf3 = list1.count(3) # countOf3 = 1
# Reverse
list2.reverse() # list2 = ['m', 'g', 'e', 'd', 'a']
# -- Accessing Elements -- #
# index
list1[0] # Return 5
list1[-1] # Return 3
# Slicing
list1[2:5] # Return [8, 1, 3]
list1[2:5:2] # Return [8, 3]
# -- Length --#
len(list1) # Return 5
# -- Adding Elements: -- #
# Append
list1.append('string') # list1 = [5, 2, 8, 1, 3, 'string']
# Insert
list1.insert(2, [1,2]) # list1 = [5, 2, [1, 2], 8, 1, 3, 'string']
# Extend
list1.extend([9, 10]) # list1 = [5, 2, [1, 2], 8, 1, 3, 'string', 9, 10]
# -- Removing Elements: -- #
# Pop
poppedElement = list1.pop(4) # poppedElement = 1, list1 = [5, 2, [1, 2], 8, 3, 'string',
9, 10]
# Remove
list1.remove(2) # list1 = [5, [1, 2], 8, 3, 'string', 9, 10]
# Clear
list3.clear() # Return []
# Count
countOf3 = list1.count(3) # countOf3 = 1
# Reverse
list2.reverse() # list2 = ['m', 'g', 'e', 'd', 'a']
# List Concatenation
listConcatenation = list1 + list2 # listConcatenation = [5, [1, 2], 8, 3, 'string', 9,
10, 'm', 'g', 'e', 'd', 'a']
Note: we deal with None as a false & with not None as true in condition
❖ Implement Display Method
def __str__(self):
values = []
current = self.head
while current:
values.append(current.data)
current = current.next
return str(values)
❖ Implement Accessing Elements Functions (getitem & setitem)
# To Access Element
def __getitem__(self, index):
if isinstance(index, slice):
start, stop, step = index.indices(len(self))
values = []
current = self.head
i = 0
while i < stop:
if i >= start:
values.append(current.data)
for _ in range(step):
if current.next is None:
break
current = current.next
i += step
return values
else:
if index < 0:
index = len(self) + index
if index < 0 or index >= len(self):
raise IndexError("Index out of range")
current = self.head
for _ in range(index):
current = current.next
return current.data
# To Assignment Element
def __setitem__(self, index, value):
if isinstance(index, slice):
raise NotImplementedError("Slice assignment is not supported.")
if index < 0 or index >= len(self):
raise IndexError("Index out of range")
current = self.head
for _ in range(index):
current = current.next
current.data = value
if index == 0:
if not self.head:
return None
poppedData = self.head.data
self.head = self.head.next
return poppedData
current = self.head
for _ in range(index - 1):
if current:
current = current.next
if not current or not current.next:
return None
poppedData = current.next.data
current.next = current.next.next
return poppedData
❖ Implement Remove Function
def remove(self, data):
if not self.head:
return
if self.head.data == data:
self.head = self.head.next
self.size-=1
return
current = self.head
while current.next:
if current.next.data == data:
current.next = current.next.next
self.size-=1
return
current = current.next
current = self.head
while current:
newList.append(current.data)
current = current.next
current = otherList.head
while current:
newList.append(current.data)
current = current.next
return newList
else:
raise TypeError("Unsupported operand type(s)")
EXAMPLE:
# Creating a lists
list1 = List([5, 2, 8, 1, 3])
list2 = List(['m', 'a', 'g', 'e', 'd'])
list3 = List([1, 2.3, 'string', {'dicKey':'dicValue'}, [], True])
list4 = List();
# -- Accessing Elements -- #
# index
list1[0] # Return 5
list1[-1] # Return 3
# Slicing
list1[2:5] # Return [8, 1, 3]
list1[2:5:2] # Return [8, 3]
# -- Length --#
len(list1) # Return 5
# -- Adding Elements: -- #
# Append
list1.append('string') # list1 = [5, 2, 8, 1, 3, 'string']
# Insert
list1.insert(2, [1,2]) # list1 = [5, 2, [1, 2], 8, 1, 3, 'string']
# Extend
list1.extend([9, 10]) # list1 = [5, 2, [1, 2], 8, 1, 3, 'string', 9, 10]
# -- Removing Elements: -- #
# Pop
poppedElement = list1.pop(4) # poppedElement = 1, list1 = [5, 2, [1, 2], 8, 3, 'string',
9, 10]
# Remove
list1.remove(2) # list1 = [5, [1, 2], 8, 3, 'string', 9, 10]
# Clear
list3.clear() # Return []
# Count
countOf3 = list1.count(3) # countOf3 = 1
# List Concatenation
listConcatenation = list1 + list2 # listConcatenation = [5, [1, 2], 8, 3, 'string', 9,
10, 'm', 'g', 'e', 'd', 'a']
class List:
def __init__(self, values = None):
self.head = None
self.size = 0
if values:
for value in values:
self.append(value)
# All code to be written is located here
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __str__(self):
values = []
current = self.head
while current:
values.append(current.data)
current = current.next
return str(values)
# To Access Element
def __getitem__(self, index):
if isinstance(index, slice):
start, stop, step = index.indices(len(self))
values = []
current = self.head
i = 0
while i < stop:
if i >= start:
values.append(current.data)
for _ in range(step):
if current.next is None:
break
current = current.next
i += step
return values
else:
if index < 0:
index = len(self) + index
if index < 0 or index >= len(self):
raise IndexError("Index out of range")
current = self.head
for _ in range(index):
current = current.next
return current.data
# To Assignment Element
def __setitem__(self, index, value):
if isinstance(index, slice):
raise NotImplementedError("Slice assignment is not supported.")
if index < 0 or index >= len(self):
raise IndexError("Index out of range")
current = self.head
for _ in range(index):
current = current.next
current.data = value
def __len__(self):
return self.size
if index == 0:
if not self.head:
return None
poppedData = self.head.data
self.head = self.head.next
return poppedData
current = self.head
for _ in range(index - 1):
if current:
current = current.next
if not current or not current.next:
return None
poppedData = current.next.data
current.next = current.next.next
return poppedData
def clear(self):
self.head = None
def sort(self):
if not self.head or not self.head.next:
return
# By Bubble Sort Algorithem
swap = True
while swap:
swap = False
cur = self.head
while cur.next:
if cur.data > cur.next.data:
cur.data, cur.next.data = cur.next.data, cur.data
swap = True
cur=cur.next
def reverse(self):
pre = None
cur = self.head
while cur:
nextNode = cur.next
cur.next = pre
pre = cur
cur = nextNode
self.head = pre
def copy(self):
newLinkedList = List()
current = self.head
while current:
newLinkedList.append(current.data)
current = current.next
return newLinkedList
current = self.head
while current:
newList.append(current.data)
current = current.next
current = otherList.head
while current:
newList.append(current.data)
current = current.next
return newList
else:
raise TypeError("Unsupported operand type(s)")
# ---------------------------------------------
# Example
# ---------------------------------------------
# Creating a lists
list1 = List([5, 2, 8, 1, 3])
list2 = List(['m', 'a', 'g', 'e', 'd'])
list3 = List([1, 2.3, 'string', {'dicKey':'dicValue'}, [], True])
list4 = List();
# -- Accessing Elements -- #
# index
list1[0] # Return 5
list1[-1] # Return 3
# Slicing
list1[2:5] # Return [8, 1, 3]
list1[2:5:2] # Return [8, 3]
# -- Length --#
len(list1) # Return 5
# -- Adding Elements: -- #
# Append
list1.append('string') # list1 = [5, 2, 8, 1, 3, 'string']
# Insert
list1.insert(2, [1,2]) # list1 = [5, 2, [1, 2], 8, 1, 3, 'string']
# Extend
list1.extend([9, 10]) # list1 = [5, 2, [1, 2], 8, 1, 3, 'string', 9, 10]
# -- Removing Elements: -- #
# Pop
poppedElement = list1.pop(4) # poppedElement = 1, list1 = [5, 2, [1, 2], 8, 3, 'string',
9, 10]
# Remove
list1.remove(2) # list1 = [5, [1, 2], 8, 3, 'string', 9, 10]
# Clear
list3.clear() # Return []
# Count
countOf3 = list1.count(3) # countOf3 = 1
# Reverse
list2.reverse() # list2 = ['m', 'g', 'e', 'd', 'a']
# List Concatenation
listConcatenation = list1 + list2 # listConcatenation = [5, [1, 2], 8, 3, 'string', 9,
10, 'm', 'g', 'e', 'd', 'a']