def sum_lists(n1, n2): c = 0 n3 = LinkedList() while n1 is not None: n3.insert(n1.get_data() + n2.get_data() - 10 + c if n1.get_data() + n2.get_data() + c > 9 else n1.get_data() + n2.get_data() + c) c = 1 if n1.get_data() + n2.get_data() + c > 9 else 0 n1 = n1.get_next() n2 = n2.get_next() return n3
def partition(linked_list, pivot): current = linked_list.head left_to_pivot = LinkedList() right_to_pivot = LinkedList() while current is not None: if current.data <= pivot: if left_to_pivot.head is None: left_to_pivot.head = left_to_pivot.tail = current else: left_to_pivot.tail.next = current left_to_pivot.tail = current else: if right_to_pivot.head is None: right_to_pivot.head = right_to_pivot.tail = current else: right_to_pivot.tail.next = current right_to_pivot.tail = current # left_to_pivot.print_list() right_to_pivot.print_list() current = current.next return left_to_pivot
class Queue: def __init__(self): self.list = LinkedList() def push(self, val): self.list.pushBack(val) def get(self): val = self.list.popFront() return val def __repr__(self): return self.list.__repr__() def empty(self): return self.list.size == 0
def test_index_of(self): self.myList = LinkedList() node1 = Node(1) node2 = Node(2) node3 = Node(3) self.myList.head = node1 node1.next = node2 node2.next = node3 self.assertEqual(self.myList.index_of(1), 0) self.assertEqual(self.myList.index_of(2), 1) self.assertEqual(self.myList.index_of(3), 2) self.assertFalse(self.l1.index_of(1)) self.assertFalse(self.l2.index_of(1)) self.assertEqual(self.l3.index_of(1), 0)
from SinglyLinkedList import LinkedList # create Linked List LL = LinkedList() choice = None while(choice!=5): print('###### Singly Linked List operations ######') print('1. Display the integers') print('2. insert integer at end of Linked List') print('3. insert integer at begining of Linked List') print('4. remove integer from Linked List') print('5. Exit') choice = input("Enter your choice: ") choice = int(choice) if choice == 1: LL.printLL() elif choice == 2: itemToAdd = input("Enter integer to push: ") LL.insertAtEnd(itemToAdd) elif choice == 3: itemToAdd = input("Enter integer to push: ") LL.insertAtBegining(itemToAdd) elif choice == 4:
else: if right_to_pivot.head is None: right_to_pivot.head = right_to_pivot.tail = current else: right_to_pivot.tail.next = current right_to_pivot.tail = current # left_to_pivot.print_list() right_to_pivot.print_list() current = current.next return left_to_pivot if __name__ == "__main__": pivot = int(input().strip()) # CREATING INPUT FOR THE PROBLEM linked_list = LinkedList() elements = [1,4,3,2,5,2,3] # 1. Creating linked list with some nodes for n in elements: linked_list.append(n) left_to_pivot = partition(linked_list, pivot) # left_to_pivot.print_list() # print(left_to_pivot)
def main(): ll = LinkedList() # Insertion methods ll.insert(Node(1)) ll.insert(Node(2)) ll.insert(Node(3)) ll.insert_after(Node(4), Node(3)) # Print current llst ll.print_list() print("Inserting element at beggining of llst") ll.insert_beginning(Node(5)) # Print current llst ll.print_list() print("Count: ", ll.size, "\n") # Removal methods print("Removing node with value 2") ll.remove(Node(2)) ll.print_list() print("Count: ", ll.size, "\n") print("Removing last element of llst") ll.pop() ll.print_list() print("Count: ", ll.size, "\n") # trying to remove non existing node print("Removing not existent element Node(2)") ll.remove(Node(2)) ll.print_list() print("Count: ", ll.size, "\n") # empty whole llst with pop calls print("About to empty list with `LinkedList.pop` calls") for _ in range(ll.size + 10): ll.pop() ll.print_list() #ll.empty() print("Count: ", ll.size, "\n")
def __init__(self): self.list = LinkedList()
def setUp(self): self.l1 = LinkedList() self.l2 = LinkedList(2) self.l3 = LinkedList(3)
class ListFunctionsTest(unittest.TestCase): def setUp(self): self.l1 = LinkedList() self.l2 = LinkedList(2) self.l3 = LinkedList(3) def test_is_empty(self): self.assertEqual(self.l1.head, None) self.assertFalse(self.l2.head == None) def test_insert_beginning(self): self.l1.insert_beginning(1) self.assertEqual(self.l1.head.data, 1) self.assertEqual(self.l1.head.next, None) self.l2.insert_beginning(1) self.assertEqual(self.l2.head.data, 1) self.assertEqual(self.l2.head.next.data, 2) def test_size(self): self.assertEqual(self.l1.size(), 0) self.assertEqual(self.l2.size(), 1) def test_search(self): self.assertFalse(self.l1.search(1)) self.assertTrue(self.l2.search(2)) def test_remove(self): self.l2.remove(2) self.assertEqual(self.l2.head, None) self.assertFalse(self.l1.remove(1)) self.l3.insert_beginning(2) self.l3.insert_beginning(1) self.l3.remove(2) def test_index_of(self): self.myList = LinkedList() node1 = Node(1) node2 = Node(2) node3 = Node(3) self.myList.head = node1 node1.next = node2 node2.next = node3 self.assertEqual(self.myList.index_of(1), 0) self.assertEqual(self.myList.index_of(2), 1) self.assertEqual(self.myList.index_of(3), 2) self.assertFalse(self.l1.index_of(1)) self.assertFalse(self.l2.index_of(1)) self.assertEqual(self.l3.index_of(1), 0)
from SinglyLinkedList import LinkedList if __name__ == '__main__': new_linked_list = LinkedList() new_linked_list.insert_at_end(4) new_linked_list.insert_at_end(5) new_linked_list.insert_at_end(1) new_linked_list.insert_at_start(451) new_linked_list.insert_at_start(154) new_linked_list.insert_at_start(155) new_linked_list.insert_after_item(5, 51) new_linked_list.insert_after_item(154, 11) new_linked_list.insert_after_item(1, 111) new_linked_list.insert_before_item(1, 451) new_linked_list.insert_before_item(4, 4511) new_linked_list.insert_before_item(2, 45) #new_linked_list.insert_after_item(4511, 11) new_linked_list.insert_at_index(1, 41) new_linked_list.insert_at_index(2, 42) new_linked_list.insert_at_index(7, 43) new_linked_list.insert_at_index(12, 45) new_linked_list.insert_at_index(17, 3313) new_linked_list.traverse_list() print("\nThe total elements in the linkedlist are:" +