Ejemplo n.º 1
0
	def test_SLL(self, mock_stdout):
		sll = LinkedList(5)

		# check default instantiation values
		self.assertEqual(sll.head.value, 5)
		self.assertEqual(sll.tail.value, 5)
		self.assertEqual(sll.tail.next, None)
		self.assertEqual(sll.length, 1)
		self.assertEqual(sll.count(), 1)

		# check if append() is working properly
		sll.append(6)
		self.assertEqual(sll.head.next.value, 6)
		self.assertEqual(sll.count(), 2)

		# check if prepend is working properly
		sll.prepend(4)
		self.assertEqual(sll.head.value, 4)
		self.assertEqual(sll.head.next.value, 5)
		self.assertEqual(sll.count(), 3)

		# check if traverse to index is working properly
		self.assertEqual(sll.traverseToIndex(1).value, 5)

		# check if insert() working properly
		sll.insert(1, 10)
		self.assertEqual(sll.head.next.value, 10)

		# Check if display() is working properly
		sll.display()
		self.assertEqual(mock_stdout.getvalue(), '4-->10-->5-->6-->\n')

		# reset stdout
		mock_stdout.seek(0)
		mock_stdout.truncate(0)

		# Check if remove() is working properly
		sll.remove(5)
		self.assertEqual(sll.head.next.value, 10)

		# Test reverse()
		sll.reverse()
		sll.display()
		self.assertEqual(mock_stdout.getvalue(), '6-->10-->4-->\n')

		# Test count() again
		self.assertEqual(sll.count(), 3)
Ejemplo n.º 2
0
    def DS_linkedlist(self):
        try:
            head = int(input(" Enter the Head of your Linked List: "))
        except:
            return self.DS_queue()

        # Linked List instantiation with the given length
        linkedlist = LinkedList(head)

        while True:
            print(
                '\n [LINKED LIST OPERATIONS] \n\n 1. Append\n 2. Prepend\n 3. Display\n 4. Count\n 5. Insert\n 6. Remove\n 7. Reverse\n 8. Back\n'
            )

            try:
                selectedNumber = int(input("Select a number: "))
            except:
                print("\nPlease enter a valid input\n")
                input("Press [Enter] to continue...")
                self.DS_linkedlist()

            if selectedNumber == 1:
                value = int(input("Enter the value that you want to append: "))
                linkedlist.append(value)
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 2:
                value = int(
                    input("Enter the value that you want to prepend: "))
                linkedlist.prepend(value)
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 3:
                linkedlist.display()
                input("Press [Enter] to continue...")

            elif selectedNumber == 4:
                linkedlist.count()
                input("Press [Enter] to continue...")

            elif selectedNumber == 5:
                linkedlist.insert()
                print('Insert Complete')
                input("Press [Enter] to continue...")

            elif selectedNumber == 6:
                value = int(input("Enter the value that you want to remove: "))
                linkedlist.remove(value)
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 7:
                linkedlist.reverse()
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 8:
                self.DS_main()

            else:
                print(
                    "The number you entered is not in the choices... Going back to the main screen instead..."
                )
                input("Press [Enter] to continue...")
                self.DS_main()