def main(): l = SingleLinkedList() print l print l.get_list_length() l.add(1) l.add(2) l.add(3) l.print_list() l.insert_at_beginning(500) l.insert_at_beginning(600) l.insert_at_beginning(700) l.print_list() l.insert_at_position(3, 99999) l.print_list() l.delete_at_position(3) l.print_list() l.delete_value(500) l.print_list() nd = SingleLinkedListNode() nd.set_data(2) l.delete_node(nd) l.print_list() # l.clear() # l.print_list() l.find_nth_node_from_end(4)
def insert_at_beginning(self, data): new_node = SingleLinkedListNode() new_node.set_data(data) if self.length == 0: self.head = new_node else: new_node.set_next(self.head) self.head = new_node self.length += 1
def insert_at_end(self, data): new_node = SingleLinkedListNode() new_node.set_data(data) if self.length == 0: self.head = new_node else: current = self.head while current.has_next(): current = current.get_next() current.set_next(new_node) self.length += 1
def insert_at_end(self, data): new_node = SingleLinkedListNode() new_node.set_data(data) if self.length == 0: self.head = new_node else: current = self.head while current.has_next(): current = current.get_next() current.set_next(new_node) self.length += 1
def insert_at_end(self, data): if self.length == 0: self.insert_at_beginning(data) else: new_node = SingleLinkedListNode() new_node.set_data(data) current = self.head while current.get_next() is not self.head: current = current.get_next() new_node.set_next(self.head) current.set_next(new_node) self.length += 1
def __insert_at_beginning(self, data): new_node = SingleLinkedListNode() new_node.set_data(data) if self.length > self.max_size - 1: print "Stack is full" return else: if self.length == 0: self.head = new_node else: new_node.set_next(self.head) self.head = new_node self.length += 1
def insert_at_position(self, position, data): if position > self.length or position < 0: return None else: if position == 0: self.insert_at_beginning(data) else: if position == self.length: self.insert_at_end(data) else: new_node = SingleLinkedListNode() new_node.set_data(data) current = self.head count = 0 while count < (position - 1): current = current.get_next() count += 1 new_node.set_next(current.get_next()) current.set_next(new_node) self.length += 1
def insert_at_beginning(self, data): new_node = SingleLinkedListNode() new_node.set_data(data) if self.length == 0: self.head = new_node new_node.set_next(self.head) else: current = self.head while current.get_next() is not self.head: current = current.get_next() new_node.set_next(self.head) current.set_next(new_node) self.head = new_node self.length += 1
def insert_at_beginning(self, data): new_node = SingleLinkedListNode() new_node.set_data(data) if self.length == 0: self.head = new_node else: new_node.set_next(self.head) self.head = new_node self.length += 1
def __insert_at_beginning(self, data): new_node = SingleLinkedListNode() new_node.set_data(data) if self.length > self.max_size - 1: print "Stack is full" return else: if self.length == 0: self.head = new_node else: new_node.set_next(self.head) self.head = new_node self.length += 1
def insert_node_in_sorted_list(self, data): new_node = SingleLinkedListNode() new_node.set_data(data) if self.length == 0: self.head = new_node else: current = self.head previous = None stop = False while current is not None and not stop: if current.get_data() > data: stop = True else: previous = current current = current.get_next() if previous is None: new_node.set_next(current) self.head = new_node else: new_node.set_next(current) previous.set_next(new_node) self.length += 1
def insert_at_position(self, position, data): if position > self.length or position < 0: return None else: if position == 0: self.insert_at_beginning(data) else: if position == self.length: self.insert_at_end(data) else: new_node = SingleLinkedListNode() new_node.set_data(data) current = self.head count = 0 while count < (position - 1): current = current.get_next() count += 1 new_node.set_next(current.get_next()) current.set_next(new_node) self.length += 1
def insert_node_in_sorted_list(self, data): new_node = SingleLinkedListNode() new_node.set_data(data) if self.length == 0: self.head = new_node else: current = self.head previous = None stop = False while current is not None and not stop: if current.get_data() > data: stop = True else: previous = current current = current.get_next() if previous is None: new_node.set_next(current) self.head = new_node else: new_node.set_next(current) previous.set_next(new_node) self.length += 1