def merge(list1, list2): p1 = list1.head p2 = list2.head result = LinkedList(None) while p1 and p2: if p1.data > p2.data: result.insert_at_end(p2.data) p2 = p2.next else: result.insert_at_end(p1.data) p1 = p1.next if p1 is None: while p2: result.insert_at_end(p2.data) p2 = p2.next if p2 is None: while p1: result.insert_at_end(p1.data) p1 = p1.next return result
class Stack: def __init__(self): self.storage = LinkedList(None) self.last_item = None self.item_counter = 0 self.min = None def __len__(self): return self.item_counter def push(self, item): head = self.storage.head if head is None: self.storage.head = Node(item) else: new_node = Node(item) new_node.next = head self.storage.head = new_node self.last_item = item self.item_counter += 1 if self.min is None: self.min = item elif self.min > item: self.min = item #self.storage.walk() #print(self.item_counter) def pop(self): if self.item_counter > 0: head = self.storage.head if head is not None: next = head.next self.storage.head = next return head.data else: print("Stack is empty") return None def top(self): self.storage.walk() return self.last_item def reverse(self): print(self.storage.head.data) self.storage.reverse() print(self.storage.head.data) self.last_item = self.storage.head.data def minimum(self): return self.min
def add_number(list1, number): list1.rev(None, list1.head) result = LinkedList(None) current = list1.head s = int(current.data) + int(number) result.insert_at_end(s % 10) carry = int(s / 10) current = current.next while current: s = carry + int(current.data) result.insert_at_end(s % 10) carry = int(s / 10) current = current.next result.rev(None, result.head) return result
def run(): items = [[7, 8, 9], [3, 5, 7, 12]] lists = [LinkedList(None) for i in range(len(items))] for i in range(len(items)): for j in range(len(items[i])): lists[i].insert_at_end(items[i][j]) # inputs for i in range(len(lists)): lists[i].walk() merged = LinkedLists.fnc.merge(lists[0], lists[1]) merged.walk() add_number = LinkedLists.fnc.add_number(lists[0], 1) add_number.walk() print("Test run complete")
from LinkedLists.LinkedList import LinkedList ll = LinkedList() # ll.insert_at_head(0) # ll.insert_at_head(2) # ll.insert_at_head(4) # ll.insert_at_head(6) ll.append_from_list([0, 2, 4, 6]) ll.append_from_list([8, 10, 12, 14]) ll.append(16) # print(ll) # ll.insert_at_index(0, -2) # print(ll) # ll.insert_at_index(1, -1) print(ll.length) ll.insert_at_index(9, 18) print(ll)
from LinkedLists.LinkedList import LinkedList l = LinkedList() l.insertatbegining(44) l.insertatend(323) l.insertatbegining(123) l.insertatend(54) print("The Data in First Element :", l.headelement()) print("The Data in Last Element :", l.tailelement()) print("The Number of Elements of LinkedList :", l.count) print("Before Delete") LinkedList.display(l.head) l.delete(54) print("After Delete") l.display(l.head) print("The Data in First Element :", l.headelement()) print("The Data in Last Element :", l.tailelement())
def __init__(self): self.storage = LinkedList(None) self.last_item = None self.item_counter = 0 self.min = None