def addToTail(self, value): newNode = Node(value) if not self.tail: self.head = newNode else: self.tail.next = newNode newNode.prev = self.tail self.items.append(newNode) self.tail = newNode
def addToHead(self, value): newNode = Node(value) if not self.head: self.tail = newNode else: self.head.prev = newNode newNode.next = self.head self.items.insert(0, newNode) self.head = newNode
def insertAtIndex(self, index, value): if index > len(self.items) - 1: return False if index == 0: return self.addToHead(value) if index == len(self.items) - 1: return self.addToTail(value) new_node = Node(value) before_node = self.items[index - 1] after_node = self.items[index] before_node.next = new_node new_node.prev = before_node after_node.prev = new_node new_node.next = after_node self.items.insert(index, new_node)
def add(self, item): if self.head == None: self.head = Node(item) else: temp = Node(item) temp.set_next(self.head) self.head.set_previous(temp) self.head = temp
def append(self, item): if self.head == None: self.head = Node(item) else: current = self.head while current.get_next() != None: current = current.get_next() temp = Node(item) current.set_next(temp) temp.set_previous(current)
def insert_after(self, item, x): if self.head == None: print("List is empty") else: current = self.head found = False while current != Nonde and not found: if current.get_item() == x: found = True else: current = current.get_next() if found == False: print("item is not in the list") else: temp = Node(item) temp.set_next(current.get_next()) temp.set_previous(current) if current.get_next() != None: current.get_next().set_previous(temp) current.set_next(temp)
class DList: def __init__(self): self.head = None def is_empty(self): return self.head == None def add(self, item): if self.head == None: self.head = Node(item) else: temp = Node(item) temp.set_next(self.head) self.head.set_previous(temp) self.head = temp def append(self, item): if self.head == None: self.head = Node(item) else: current = self.head while current.get_next() != None: current = current.get_next() temp = Node(item) current.set_next(temp) temp.set_previous(current) def insert_after(self, item, x): if self.head == None: print("List is empty") else: current = self.head found = False while current != Nonde and not found: if current.get_item() == x: found = True else: current = current.get_next() if found == False: print("item is not in the list") else: temp = Node(item) temp.set_next(current.get_next()) temp.set_previous(current) if current.get_next() != None: current.get_next().set_previous(temp) current.set_next(temp) def insert_before(self, item, x): if self.head == None: print("List is empty") else: current = self.head found = False while current != Nonde and not found: if current.get_item() == x: found = True else: current = current.get_next() if found == False: print("item is not in the list") else: temp = Node(item) temp.set_next(current) temp.set_previous(current.get_previous()) if current.get_previous() != None: current.get_previous().set_next(temp) else: self.head = temp current.set_previous(temp) def pop_first(self): if self.head == None: print("List is empty") else: if self.head.get_next() == None: self.head = None else: self.head = self.head.get_next() self.head.set_previous(None) def pop_last(self): if self.head == None: print("List is empty") else: if self.head.get_next() == None: self.head = None else: current = self.head while current.get_next() != None: current = current.get_next() current.get_previous().set_next(None) def delete(self, item): if self.head.get_item() == item: self.head = self.head.get_next() self.head.set_previous(None) else: current = self.head found = False while not found: if current.get_item() == item: found = True else: current = current.get_next() if current.get_next() != None: current.get_previous().set_next(current.get_next()) current.get_next().set_previous(current.get_previous()) else: current.get_previous().set_next(None) def search(self, item): current = self.head found = False while current != None and not found: if current.get_item() == item: found = True else: current = current.get_next() return found def size(self): current = self.head count = 0 while current != None: count = count + 1 current = current.get_next() return count