Question: class Node(object): Doubly linked node which stores an object def __init__(self, element, next_node=None, previous_node=None): # The underscores are to prevent overwriting the variables if inherited
class Node(object): """Doubly linked node which stores an object"""
def __init__(self, element, next_node=None, previous_node=None): # The underscores are to prevent overwriting the variables if inherited self.__element = element self.__next_node = next_node self.__previous_node = previous_node
def get_element(self): """Returns the element stored in this node""" return self.__element
def get_previous(self): """Returns the previous linked node""" return self.__previous_node
def get_next(self): """Returns the next linked node""" return self.__next_node
def set_element(self, element): """Sets the element stored in this node""" self.__element = element
def set_previous(self, previous_node): """Sets the previous linked node""" self.__previous_node = previous_node
def set_next(self, next_node): """Sets the next linked node""" self.__next_node = next_node
class DoublyLinkedList(object):
def __init__(self): self.__size = 0 self.__header = Node('Header') self.__trailer = Node('Trailer') self.__header.set_next(self.__trailer) self.__trailer.set_previous(self.__header) self.__current = None
def __iter__(self): self.__current = None return self
def __next__(self): if self.is_empty() or self.__current == self.__trailer: raise StopIteration() elif self.__current is None: self.__current = self.__header self.__current = self.__current.get_next() return self.__current
def map(self, function): for node in self: if node != self.__trailer and node != self.__header: node.set_element(function(node.get_element()))
def size(self): return self.__size
def is_empty(self): return self.__size == 0
def get_first(self): if self.is_empty(): raise Exception("List is empty") else: return self.__header.get_next()
def get_last(self): if self.is_empty(): raise Exception("List is empty") else: return self.__trailer.get_previous()
def get_previous(self, node): if node == self.__header: raise Exception("Not able to get the element before the header of this list") else: return node.get_previous()
def get_next(self, node): if node == self.__trailer: raise Exception("Not able to get the element after the trailer of this list") else: return node.get_next()
def add_before(self, new, existing): previous_existing = self.get_previous(existing) new.set_previous(previous_existing) new.set_next(existing) existing.set_previous(new) previous_existing.set_next(new) self.__size += 1
def add_after(self, new, existing): next_existing = self.get_next(existing) new.set_previous(existing) new.set_next(next_existing) existing.set_next(new) next_existing.set_previous(new) self.__size += 1
def add_first(self, new): self.add_after(new, self.__header)
def add_last(self, new): self.add_before(new, self.__trailer)
def remove(self, node): before = self.get_previous(node) after = self.get_next(node) before.set_next(after) after.set_previous(before) node.set_next(None) node.set_previous(None) self.__size -= 1
So I have two classes Node, Doublylinkedlist and I just need an example for my def under Doubliylinedlist class. I need to prove that my functions work fine but I have no idea how to test my functions. Like for example, I did DoublyLinkedList.is_empty(doubleList) for my empty function and the result came out as what I expected, but the functions below, I have no idea how to do it.
get_First&get_Last, get_Previous&get_Next, add_before&add_after, add_first&add_last, remove, map, iter&next
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
