LinkedList(1)
LinkedList(1)
You are tasked with implementing a basic data structure: a singly linked list.
To accomplish this, you will create two classes, Node and LinkedList.
The Node class will represent an individual node within the linked list, while
the LinkedList class will manage the overall list structure.
def __init__(self,value):
self.value=value
self.next=None
class LinkedList:
def __init__(self,value):
new_node=Node(value)
self.head=new_node
self.tail=new_node
self.length=1
my_linked_list = LinkedList(4)
print('Head:', my_linked_list.head.value)
print('Tail:', my_linked_list.tail.value)
print('Length:', my_linked_list.length)
Linked List: Print List
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self, value):
new_node = Node(value)
self.head = new_node
self.tail = new_node
self.length = 1
def print_list(self):
temp=self.head
while temp is not None:
print(temp.value)
temp=temp.next
def make_empty(self):
self.head = None
self.tail = None
self.length = 0
def append(self, value):
new_node = Node(value)
if self.head is None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
self.length += 1
my_linked_list = LinkedList(1)
my_linked_list.append(2)
my_linked_list.append(3)
my_linked_list.print_list()
EXPECTED OUTPUT:
----------------
1
2
3
Linked List: Append
Implement the append method for the LinkedList class.
The append method should add a new node with a given value to the end of the linked
list, updating the tail attribute and the length attribute accordingly.
1. The method should handle the cases where the list is empty and where the list
already has one or more nodes.
2. The method should create a new node with the given value and add it to the end
of the list.
3. The method should update the tail attribute of the LinkedList correctly.
4. The method should update the length attribute of the LinkedList to reflect the
addition of the new node.
class Node:
self.value = value
self.next = None
class LinkedList:
new_node = Node(value)
self.head = new_node
self.tail = new_node
self.length = 1
def print_list(self):
temp = self.head
print(temp.value)
temp = temp.next
def make_empty(self):
self.head = None
self.tail = None
self.length = 0
new_node = Node(value)
if self.head==None:
self.head=new_node
self.tail=new_node
else:
self.tail.next=new_node
self.tail=new_node
self.length += 1
return True
my_linked_list = LinkedList(1)
my_linked_list.make_empty()
my_linked_list.append(1)
my_linked_list.append(2)
print('Head:', my_linked_list.head.value)
print('Tail:', my_linked_list.tail.value)
print('Linked List:')
my_linked_list.print_list()
EXPECTED OUTPUT:
----------------
Head: 1
Tail: 2
Length: 2
Linked List:
2
Linked List: Pop
Your task is to implement the pop method for the LinkedList class.
The pop method should remove the last node (tail) from the linked list and
return the removed node. If the list is empty, the method should return
None.
After the last node is removed, the second-to-last node should become
the new tail of the list.
Additionally, if the list becomes empty after the pop operation, both the
head and tail attributes should be set to None.
1. The method should handle the cases where the list is empty, has
only one node, or has multiple nodes.
2. The method should update the tail attribute of the LinkedList
correctly.
3. The method should update the length attribute of the LinkedList to
reflect the removal of the node.
4. The method should either return the removed node or None if the list
is empty.
Algorithm for pop Function in a Linked List
def pop(self):
if self.length == 0:
return None
temp=self.head
prev=self.head
while temp.next is not None:
prev=temp
temp=temp.next
self.tail=prev
self.tail.next=None
self.length-=1
if self.length==0:
self.head=None
self.tail=None
return temp.value
Linked List:: Prepend
Implement the prepend method for the LinkedList class.
The prepend method should add a new node with a given value to the
beginning of the linked list, updating the head attribute and the length
attribute accordingly.
1. The method should handle the cases where the list is empty and
where the list already has one or more nodes.
2. The method should create a new node with the given value and
add it to the beginning of the list.
3. The method should update the head attribute of the LinkedList
correctly.
4. The method should update the length attribute of the LinkedList
to reflect the addition of the new node.
5. The method should return True if the operation is successful.
def prepend(self,value):
new_node=Node(value)
if self.length==0:
self.head=new_node
self.tail=new_node
else:
new_node.next=self.head
self.head=new_node
self.length+=1
return True
LL: Pop First
Implement the pop_first method for the LinkedList class.
The pop_first method should remove the first node (the head) from
the linked list, update the head attribute and the length attribute
accordingly, and return the removed node.
1. The method should handle the cases where the list is empty and
where the list has one or more nodes.
2. The method should save a reference to the current head node
before updating the head attribute.
3. The method should update the head attribute to the second
node in the list.
4. The method should disconnect the removed node from the list
by setting its next attribute to None.
5. The method should update the length attribute of the LinkedList
to reflect the removal of the node.
6. If the list becomes empty after removing the node, the method
should set the tail attribute of the LinkedList to None.
7. The method should return the removed node, or None if the list
is empty.
Algorithm for pop_first Function
if self.length==0:
return None
temp=self.head
self.head=self.head.next
temp.next=None
self.length-=1
if self.length ==0:
self.tail=None
return temp.value
LinkedList: Get
1. The method should handle the cases where the index is out
of bounds.
2. The method should start at the head of the list and traverse
the list using the next attribute of the nodes.
3. The method should stop traversing the list when it reaches
the specified index and return the node at that position.
4. If the index is out of bounds, the method should return
None.
Algorithm for get Function
def get(self,index):
return None
temp=self.head
for _ in range(index):
temp=temp.next
return temp
LinkedList : Set
If the index is out of bounds, the method should return False. If the value
is successfully updated, the method should return True.
1. The method should utilize the get method to find the node at the
specified index.
2. The method should update the value of the node if the node is
found.
3. The method should return True if the value is successfully updated.
4. If the node is not found (i.e., the index is out of bounds), the method
should return False.
Algorithm for set_value Function
def set_value(self,index,value):
temp=self.get(index)
if temp:
temp.value=value
return True
return False
LinkedList: Insert
The insert method should take an integer index and a value as parameters
and insert a new node with the given value at the specified index in the
linked list.
If the index is out of bounds, the method should return False. If the new
node is successfully inserted, the method should return True.
1. The method should handle edge cases, such as inserting a new node
at the beginning or end of the list.
2. The method should utilize the prepend, append, and get methods for
handling these edge cases.
3. The method should create a new node with the given value and
insert it at the specified index.
4. The method should update the next attribute of the previous node to
point to the new node.
5. The method should increment the length attribute of the LinkedList
class.
6. The method should return True if the new node is successfully
inserted.
7. If the index is out of bounds, the method should return False.
Algorithm for insert Function
def insert(self,index,value):
if index<0 or index > self.length:
return False
if index==0:
return self.prepend(value)
if index==self.length:
return self.append(value)
new_node=Node(value)
temp=self.get(index-1)
new_node.next=temp.next
temp.next=new_node
self.length+=1
return True
LinkedList: Remove
The remove method should take an integer index as a parameter and remove the node
at the specified index in the linked list, returning the removed node.
1. The method should handle edge cases, such as removing a node at the
beginning or end of the list.
2. The method should utilize the pop_first() and pop() methods for handling
these edge cases.
3. The method should use the get() method to find the node previous to the one
to be removed.
4. The method should update the next attribute of the previous node to point to
the node after the removed one.
5. The method should decrement the length attribute of the LinkedList class.
6. The method should return the removed node if the removal is successful.
7. If the index is out of bounds, the method should return None.
Algorithm for remove Function
return None
if index==0:
return self.pop_first()
if index==self.length-1:
return self.pop()
prev=self.get(index-1)
temp=prev.next
prev.next=temp.next
temp.next=None
self.length-=1
return temp