Beispiel #1
0
    def test_node_can_have_previous_element(self):
        n1 = Node(1)
        n2 = Node(2)

        n2.prev = n1

        self.assertIs(n2.prev, n1)
class Test(unittest.TestCase):
    '''Test Cases'''
    def setUp(self):
        self.node1 = Node(7)
        self.node1.connect(1).connect(6)

        self.node2 = Node(5)
        self.node2.connect(9).connect(2)

    def testGetNumberFromList(self):
        self.assertEqual(getReverseNumberFromLinkedList(self.node1), 617)
        self.assertEqual(getReverseNumberFromLinkedList(self.node2), 295)

    def testGetListFromNumber(self):
        node = getReverseLinkedListFromNumber(123)
        self.assertEqual(node.getPrev(), None)
        self.assertEqual(node.getValue(), 3)
        self.assertEqual(node.getNext().getValue(), 2)
        self.assertEqual(node.getNext().getNext().getValue(), 1)
        self.assertEqual(node.getNext().getNext().getNext(), None)

    def testSumLists(self):
        sum_node = sumLists(self.node1, self.node2)
        self.assertEqual(sum_node.getPrev(), None)
        self.assertEqual(sum_node.getValue(), 2)
        self.assertEqual(sum_node.getNext().getValue(), 1)
        self.assertEqual(sum_node.getNext().getNext().getValue(), 9)
        self.assertEqual(sum_node.getNext().getNext().getNext(), None)
Beispiel #3
0
 def insertFirst(self, data):
     newNode = Node(data)
     if not self.head:
         self.head = newNode
     else:
         newNode.nextNode = self.head
         self.head = newNode
Beispiel #4
0
    def test_node_can_have_next_element(self):
        n1 = Node(1)
        n2 = Node(2)

        n1.next = n2

        self.assertIs(n1.next, n2)
Beispiel #5
0
	def test_list_have_tail_node(self):
		n1 = Node(12)
		n2 = Node(55)
		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		self.assertIs(linked_list.tail, n2)
		self.assertIsNone(n2.next)
    def setUp(self):
        self.node = Node(1)
        self.node.connect(2).connect(3).connect(4).connect(5)
        deleteMiddleNode(self.node)

        self.node2 = Node(1)
        self.node2.connect(2).connect(3).connect(4)
        deleteMiddleNode(self.node2)
Beispiel #7
0
 def insert_at_begining(self, data):
     new_node = Node(data)
     if self.get_head() == None and self.get_tail() == None:
         self.set_head(new_node)
         self.set_tail(new_node)
     else:
         new_node.set_next(self.get_head())
         self.set_head(new_node)
Beispiel #8
0
 def insert_start(self, data):
   self.counter += 1
   new_node = Node(data)
   if self.head is None: 
     self.head = new_node
   else:
     new_node.next_node = self.head
     self.head = new_node
class Test(unittest.TestCase):
    '''Test Cases'''
    def setUp(self):
        self.node = Node(1)
        self.node.connect(2).connect(3).connect(2).connect(4)

    def test_node_values(self):
        self.assertEqual(returnKthToLast(2, self.node), [2, 3, 2, 4])
Beispiel #10
0
    def insertStart(self, data):
        self.counter += 1
        newNode = Node(data)

        if not self.head:
            self.head = newNode
        else:
            newNode.nextNode = self.head
            self.head = newNode
Beispiel #11
0
	def setUp(self):
		n1, n2, n3 = Node(1), Node(2), Node(3)	

		linked_list = List();
		linked_list.add_in_tail(n1)	
		linked_list.add_in_tail(n2)	
		linked_list.add_in_tail(n3)

		self.n1, self.n2, self.n3, self.linked_list = n1, n2, n3, linked_list
Beispiel #12
0
    def insertStart(self, data):

        self.counter += 1;

        newNode = Node(data);

        if not self.head:
            self.head = newNode;
        else:
            newNode.nextNode = self.head;
            self.head = newNode;
Beispiel #13
0
	def test_list_can_find_node_by_value(self):
		n1 = Node(12)
		n2 = Node(55)
		n3 = Node(128)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)

		result = linked_list.find(55)
		self.assertIs(result, n2)	
Beispiel #14
0
	def test_list_can_delete_all_nodes_by_value(self): # 1.2
		n1 = Node(55)
		n2 = Node(12)
		n3 = Node(55)
		n4 = Node(128)
		n5 = Node(55)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)
		linked_list.add_in_tail(n4)
		linked_list.add_in_tail(n5)

		linked_list.delete_by_value(55)
		self.assertIsNone(linked_list.find(55))
    def insertEnd(self, data):
        newNode = Node(data);
        actualNode = self.head;

        while actualNode.nextNode is not None:
            actualNode = actualNode.newNode;
        
        actualNode.nextNode = newNode;
Beispiel #16
0
	def test_list_can_count_itself_length(self): # 1.5
		n1 = Node(55)
		n2 = Node(12)
		n3 = Node(55)
		n4 = Node(128)	
		n5 = Node(55)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)
		linked_list.add_in_tail(n4)
		linked_list.add_in_tail(n5)

		list_length = linked_list.length()

		self.assertEqual(list_length, 5)
Beispiel #17
0
 def insert_at_end(self, data):
     new_node = Node(data)
     if self.get_head() == None and self.get_tail() == None:
         self.set_head(new_node)
         self.set_tail(new_node)
     else:
         self.get_tail().set_next(new_node)
         self.set_tail(new_node)
Beispiel #18
0
    def addToTail(self, value):
        toAppend = Node(value)
        curr = self.head
        while (curr.next != None):
            curr = curr.next

        # once we have found the end, append.
        curr.next = toAppend
Beispiel #19
0
    def test_list_can_append_node_in_head(self):
        n1 = Node(1)
        n2 = Node(2)
        n3 = Node(3)
        n4 = Node(4)

        linked_list = List()
        linked_list.add_in_tail(n2)
        linked_list.add_in_tail(n3)
        linked_list.add_in_tail(n4)

        linked_list.append_in_head(n1)

        self.assertIs(linked_list.head, n1)
        self.assertIsNone(n1.prev)
        self.assertIs(n1.next, n2)
        self.assertIs(n2.prev, n1)
Beispiel #20
0
 def append(self, data):
     new_node = Node(data)
     if not self.head:
         self.head = new_node
     else:
         last_node = self.head
         while last_node.next:
             last_node = last_node.next
         last_node.next = new_node
Beispiel #21
0
	def test_list_can_delete_all_nodes(self): # 1.3
		n1 = Node(12)
		n2 = Node(55)
		n3 = Node(128)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)

		linked_list.flush_all()

		self.assertIsNone(linked_list.head)
		self.assertIsNone(linked_list.tail)

		self.assertIsNone(linked_list.find(12))
		self.assertIsNone(linked_list.find(55))
		self.assertIsNone(linked_list.find(128))
Beispiel #22
0
 def insertLast(self, data):
     newNode = Node(data)
     if not self.head:
         self.insertFirst(data)
     else:
         actualNode = self.head.nextNode
         while actualNode is not None:
             if actualNode.nextNode == None:
                 actualNode.nextNode = newNode
                 actualNode.nextNode.nextNode = None
             actualNode = actualNode.nextNode
Beispiel #23
0
 def insert_at_middle(self, pos, data):
     if pos < 1:
         print("Position does not exist")
         return None
     else:
         if pos == 1:
             self.insert_at_begining(data)
         elif pos == self.length_linkedlist() + 1:
             self.insert_at_end(data)
         elif pos > self.length_linkedlist() + 1:
             print("Position does not exist")
         else:
             new_node = Node(data)
             temp = self.get_head()
             i = 1
             while i < pos - 1:
                 temp = temp.get_next()
                 i += 1
             new_node.set_next(temp.get_next())
             temp.set_next(new_node)
Beispiel #24
0
    def test_list_can_append_node_after_item(self):
        n1 = Node(1)
        n2 = Node(2)
        n3 = Node(3)
        n4 = Node(4)

        linked_list = List()
        linked_list.add_in_tail(n1)
        linked_list.add_in_tail(n2)
        linked_list.add_in_tail(n4)

        linked_list.append_after(node=n3, after=n2)

        self.assertIsNone(n1.prev)
        self.assertIs(n1.next, n2)
        self.assertIs(n2.prev, n1)
        self.assertIs(n2.next, n3)
        self.assertIs(n3.prev, n2)
        self.assertIs(n3.next, n4)
        self.assertIs(n4.prev, n3)
        self.assertIsNone(n4.next)
Beispiel #25
0
    def insertEnd(self, data):
        if self.head is None:
            self.insertStart(data)
            return
        self.counter += 1
        newNode = Node(data)
        actualNode = self.head

        while actualNode.nextNode is not None:
            actualNode = actualNode.nextNode

        actualNode.nextNode = newNode
Beispiel #26
0
	def test_list_can_delete_one_node_by_value(self): # 1.1
		n1 = Node(12)
		n2 = Node(55)
		n3 = Node(128)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)

		linked_list.delete_first_by_value(55)

		self.assertIsNone(linked_list.find(55))
		self.assertTrue(linked_list.find(12))
		self.assertTrue(linked_list.find(128))

		self.assertIs(linked_list.head, n1)
		self.assertIs(linked_list.tail, n3)

		self.assertIsNone(n2.next) 
		self.assertIs(n1.next, n3)
		self.assertIsNone(n3.next)
Beispiel #27
0
    def test_list_can_delete_one_node_by_value(self):
        n1 = Node(1)
        n2 = Node(2)
        n3 = Node(3)
        n4 = Node(4)

        linked_list = List()
        linked_list.add_in_tail(n1)
        linked_list.add_in_tail(n2)
        linked_list.add_in_tail(n3)
        linked_list.add_in_tail(n4)

        linked_list.delete_first_by_value(3)

        self.assertIsNone(n3.prev)
        self.assertIsNone(n3.next)

        self.assertIsNone(n1.prev)
        self.assertIs(n1.next, n2)
        self.assertIs(n2.prev, n1)
        self.assertIs(n2.next, n4)
        self.assertIs(n4.prev, n2)
        self.assertIsNone(n4.next)
Beispiel #28
0
	def test_list_can_find_all_nodes_by_value(self): # 1.4
		n1 = Node(55)
		n2 = Node(12)
		n3 = Node(55)
		n4 = Node(128)	
		n5 = Node(55)

		linked_list = List()
		linked_list.add_in_tail(n1)
		linked_list.add_in_tail(n2)
		linked_list.add_in_tail(n3)
		linked_list.add_in_tail(n4)
		linked_list.add_in_tail(n5)

		result = linked_list.find_all(55)		

		self.assertEqual(len(result), 3)

		self.assertIn(n1, result)
		self.assertIn(n3, result)
		self.assertIn(n5, result)

		self.assertNotIn(n2, result)
		self.assertNotIn(n4, result)
class Test(unittest.TestCase):
    '''Test Cases'''
    def setUp(self):
        self.node = Node(1)
        self.node.connect(2).connect(3).connect(2).connect(4)
        removeDups(self.node)

    def test_node_values(self):
        self.assertEqual(self.node.getValue(), 1)
        self.assertEqual(self.node.getNext().getValue(), 2)
        self.assertEqual(self.node.getNext().getNext().getValue(), 3)
        self.assertEqual(self.node.getNext().getNext().getNext().getValue(), 4)
        self.assertEqual(self.node.getNext().getNext().getNext().getNext(),
                         None)
Beispiel #30
0
  def insert_end(self, data):
    self.counter += 1

    if self.head is None:
      self.insert_start(data)
      return
    
    new_node = Node(data)
    actual_node = self.head

    if actual_node.next_node is None: 
      actual_node.next_node = new_node

    else: 
      while actual_node.next_node is not None:
        actual_node = actual_node.next_node

      actual_node.next_node = new_node