You are on page 1of 15

List is one of the most used graphical structures in Python, and what we will explain here is how to

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 []

❖ Search and Count:


# -- Search and Count: -- #
# Index
indexOf3 = list1.index(3) # indexOf3 = 3

# Count
countOf3 = list1.count(3) # countOf3 = 1

❖ Sorting and Revers:


# -- Sorting and Revers: -- #
# Sort
list2.sort() # list2 = ['a', 'd', 'e', 'g', 'm']

# Reverse
list2.reverse() # list2 = ['m', 'g', 'e', 'd', 'a']

❖ Copy and Concatenate:


# -- Copy and Concatenate: --#
# Copy
copiedList1 = list1.copy() # copiedList1 = [5, [1, 2], 8, 3, 'string', 9, 10]
# List Concatenation
listConcatenation = list1 + list2 # listConcatenation = [5, [1, 2], 8, 3, 'string', 9,
10, 'm', 'g', 'e', 'd', 'a']
EXAMPLE AS A WHOLE
# 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 -- #
# 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 []

# -- Search and Count: -- #


# Index
indexOf3 = list1.index(3) # indexOf3 = 3

# Count
countOf3 = list1.count(3) # countOf3 = 1

# -- Sorting and Revers: -- #


# Sort
list2.sort() # list2 = ['a', 'd', 'e', 'g', 'm']

# Reverse
list2.reverse() # list2 = ['m', 'g', 'e', 'd', 'a']

# -- Copy and Concatenate: --#


# Copy
copiedList1 = list1.copy() # copiedList1 = [5, [1, 2], 8, 3, 'string', 9, 10]

# List Concatenation
listConcatenation = list1 + list2 # listConcatenation = [5, [1, 2], 8, 3, 'string', 9,
10, 'm', 'g', 'e', 'd', 'a']

Implementation The List Using Linkedlist

❖ Implement List`s Class


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

❖ Implement Node`s Class


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

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

❖ Implement Length Function


def __len__(self):
return self.size

❖ Implement Append Function


def append(self, data):
newNode = self.Node(data)
if not self.head: #self.head ==None
self.head = newNode
self.size+=1
return
current = self.head
while current.next: #current.next !=None
current = current.next
current.next = newNode
self.size+=1

❖ Implement Insert Function


def insert(self, index, data):
new_node = self.Node(data)
current = self.head
if index == 0:
new_node.next = self.head
self.head = new_node
self.size+=1
return
for _ in range(index - 1):
if current:
current = current.next
if not current:
self.size+=1
return
new_node.next = current.next
current.next = new_node
self.size+=1

❖ Implement Extend Function


def extend(self, iterable):
if type(iterable) == str or type(iterable) == int or type(iterable) == float:
self.append(iterable)
elif type(iterable) == dict:
for key in iterable.keys():
self.append(key)
else:
for item in iterable:
self.append(item)

❖ Implement Pop Function


def pop(self, index=None):
if index is None:
if not self.head:
return None
# To poped last item if pop`s input = None
if not self.head.next:
poppedData = self.head.data
self.head = None
self.size-=1
return poppedData
current = self.head
while current.next.next:
current = current.next
poppedData=current.next.data
current.next=None
return poppedData

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

❖ Implement Clear Function


def clear(self):
self.head = None

❖ Implement Index Function


def index(self, data):
current = self.head
index = 0
while current:
if current.data == data:
return index
current = current.next
index += 1
raise ValueError(f"{data} is not in list")

❖ Implement Count Function


def count(self, data):
current = self.head
count = 0
while current:
if current.data == data:
count += 1
current = current.next
return count

❖ Implement Sort Function


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

❖ Implement Reverse Function


def reverse(self):
pre = None
cur = self.head
while cur:
nextNode = cur.next
cur.next = pre
pre = cur
cur = nextNode
self.head = pre

❖ Implement Copy Function


def copy(self):
newLinkedList = List()
current = self.head
while current:
newLinkedList.append(current.data)
current = current.next
return newLinkedList

❖ Implement Concatenation Function


def __add__(self, otherList):
if isinstance(otherList, List):
newList = List()

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 []

# -- Search and Count: -- #


# Index
indexOf3 = list1.index(3) # indexOf3 = 3

# Count
countOf3 = list1.count(3) # countOf3 = 1

# -- Sorting and Revers: -- #


# Sort
list2.sort() # list2 = ['a', 'd', 'e', 'g', 'm']
# Reverse
list2.reverse() # list2 = ['m', 'g', 'e', 'd', 'a']

# -- Copy and Concatenate: --#


# Copy
copiedList1 = list1.copy() # copiedList1 = [5, [1, 2], 8, 3, 'string', 9, 10]

# List Concatenation
listConcatenation = list1 + list2 # listConcatenation = [5, [1, 2], 8, 3, 'string', 9,
10, 'm', 'g', 'e', 'd', 'a']

Implementation the list using linkedlist with example as a whole

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

def append(self, data):


newNode = self.Node(data)
if not self.head: #self.head ==None
self.head = newNode
self.size+=1
return
current = self.head
while current.next: #current.next !=None
current = current.next
current.next = newNode
self.size+=1

def insert(self, index, data):


new_node = self.Node(data)
current = self.head
if index == 0:
new_node.next = self.head
self.head = new_node
self.size+=1
return
for _ in range(index - 1):
if current:
current = current.next
if not current:
self.size+=1
return
new_node.next = current.next
current.next = new_node
self.size+=1

def extend(self, iterable):


if type(iterable) == str or type(iterable) == int or type(iterable) == float:
self.append(iterable)
elif type(iterable) == dict:
for key in iterable.keys():
self.append(key)
else:
for item in iterable:
self.append(item)

def pop(self, index=None):


if index is None:
if not self.head:
return None
# To poped last item if pop`s input = None
if not self.head.next:
poppedData = self.head.data
self.head = None
self.size-=1
return poppedData
current = self.head
while current.next.next:
current = current.next
poppedData=current.next.data
current.next=None
return poppedData

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 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

def clear(self):
self.head = None

def index(self, data):


current = self.head
index = 0
while current:
if current.data == data:
return index
current = current.next
index += 1
raise ValueError(f"{data} is not in list")

def count(self, data):


current = self.head
count = 0
while current:
if current.data == data:
count += 1
current = current.next
return count

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

def __add__(self, otherList):


if isinstance(otherList, List):
newList = List()

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 []

# -- Search and Count: -- #


# Index
indexOf3 = list1.index(3) # indexOf3 = 3

# Count
countOf3 = list1.count(3) # countOf3 = 1

# -- Sorting and Revers: -- #


# Sort
list2.sort() # list2 = ['a', 'd', 'e', 'g', 'm']

# Reverse
list2.reverse() # list2 = ['m', 'g', 'e', 'd', 'a']

# -- Copy and Concatenate: --#


# Copy
copiedList1 = list1.copy() # copiedList1 = [5, [1, 2], 8, 3, 'string', 9, 10]

# List Concatenation
listConcatenation = list1 + list2 # listConcatenation = [5, [1, 2], 8, 3, 'string', 9,
10, 'm', 'g', 'e', 'd', 'a']

You might also like