def push(self, data): node = SLLNode(data) if self.top is None: self.top = node else: node.next = self.top self.top = node self.size += 1
def prepend(self, item): if self._count == 0: # If list is empty, create a new node a point _first and _last to it self._first = Node(item) self._last = self._first else: # Add new node to front of the chain self._first = Node(item, self._first) # Increment node count self._count += 1
def append(self, data): node = SLLNode(data) if self.head is None: self.head = self.tail = node else: self.head.next = node self.head = node self.size += 1
def enqueue(self, data): node = SLLNode(data) if self.head is None: self.head = self.tail = node else: self.tail.next = node self.tail = node self.size += 1
def append(self, item): if self._count == 0: # If list is empty, create a new node a point _first and _last to it self._first = Node(item) self._last = self._first else: # Make current last node's 'other' point to a new node and point _last to it self._last.setOther(Node(item)) self._last = self._last.getOther() # Increment node count self._count += 1
class SinglyLinkedList(object): """CzarTypes: Pure Python Singly Linked List Implementation""" ## Constructor ## def __init__(self): super(SinglyLinkedList, self).__init__() self._first = None self._last = None self._count = 0 def prepend(self, item): if self._count == 0: # If list is empty, create a new node a point _first and _last to it self._first = Node(item) self._last = self._first else: # Add new node to front of the chain self._first = Node(item, self._first) # Increment node count self._count += 1 def append(self, item): if self._count == 0: # If list is empty, create a new node a point _first and _last to it self._first = Node(item) self._last = self._first else: # Make current last node's 'other' point to a new node and point _last to it self._last.setOther(Node(item)) self._last = self._last.getOther() # Increment node count self._count += 1 def insert(self, item, index): if index > self._count: # Raise expection if trying to insert past contiguous block of nodes raise IndexError("Discontiguous Index!") elif index < 0: # Raise expection if index is negative raise IndexError("List Index Out of Bounds!") elif index == 0: # Use prepend function for inserting at index 0 self.prepend(item) return elif index == self._count: # Use append fucntion for inserting at the end self.append(item) return # Loop through all nodes up to two nodes before the index, grabbing the next node selected_node = self._first for i in range(index - 1): selected_node = selected_node.getOther() # Alias node before the index as previous_node previous_node = selected_node # Grab node/other currently at index and store as next_node next_node = previous_node.getOther() # Have previous node point to a new node which in turn points to the next_node previous_node.setOther(Node(item, next_node)) # Increment node count self._count += 1 def remove(self, index): if index > self._count - 1 or index < 0: # Raise expection if index is beyond bounds raise IndexError("List Index Out of Bounds!") elif index == 0: # Make _first point to its 'other' self._first == self._first.getOther() # Decrement node count self._count -= 1 return # Loop through all nodes up to two nodes before the index, grabbing the next node selected_node = self._first for i in range(index - 1): selected_node = selected_node.getOther() # Alias node before the index as previous_node previous_node = selected_node # Grab node/other beyond the index and store as next_node next_node = previous_node.getOther().getOther() # Have previous node point to next_node previous_node.setOther(next_node) # Decrement node count self._count -= 1 def _get_node(self, index): if index > self._count - 1 or index < 0: # Raise expection if index is beyond bounds raise IndexError("List Index Out of Bounds!") selected_node = self._first for i in range(index): selected_node = selected_node.getOther() # Return currently selected node return selected_node def replace(self, index, item): self._get_node(index).setItem(item) def get(self, index): return self._get_node(index).getItem() def count(self): return self._count def __repr__(self): # Initialize repr_string with the list's opening bracket repr_string = "[" # Loop through all nodes selected_node = self._first while selected_node: # Tack on each item's representation to the repr_string repr_string += selected_node.getItem().__repr__() # Select next node selected_node = selected_node.getOther() # If there's still another node to print, add coma to repr_string if selected_node: repr_string += ", " # Punctuate repr_string with the list's closing bracket repr_string += "]" # Return the built repr_string return repr_string def __str__(self): return self.__repr__()