예제 #1
0
    def test_length(self):
        self.linkedlist = SinglyLinkedList()
        self.linkedlist.add(20)
        self.linkedlist.add(120)
        self.linkedlist.add(7102)
        self.linkedlist.add(773)

        self.assertEqual(len(self.linkedlist), 4)
        self.assertEqual(self.linkedlist.count(), 4)
예제 #2
0
 def test_str(self):
     self.linkedlist = SinglyLinkedList()
     self.linkedlist.add(20)
     self.linkedlist.add(120)
     self.linkedlist.add(7102)
     self.linkedlist.add(773)
     self.linkedlist.add('Hello')
     self.assertEqual(str(self.linkedlist),
                      "20 --> 120 --> 7102 --> 773 --> Hello --> None")
예제 #3
0
    def test_getitem(self):
        self.linkedlist = SinglyLinkedList()
        self.linkedlist.add(20)
        self.linkedlist.add(120)
        self.linkedlist.add(7102)
        self.linkedlist.add(773)

        self.assertEqual(self.linkedlist[0], 20)
        self.assertEqual(self.linkedlist[1], 120)
        self.assertEqual(self.linkedlist[2], 7102)
        self.assertEqual(self.linkedlist[3], 773)
예제 #4
0
    def test_remove(self):
        self.linkedlist = SinglyLinkedList()
        self.linkedlist.add(20)
        self.linkedlist.add(120)
        self.linkedlist.add(7102)
        self.linkedlist.add(773)

        self.assertEqual(str(self.linkedlist),
                         '20 --> 120 --> 7102 --> 773 --> None')
        self.linkedlist.removeAt(0)
        self.assertEqual(str(self.linkedlist), '120 --> 7102 --> 773 --> None')

        self.linkedlist.removeAt(self.linkedlist.count() - 1)
        self.assertEqual(str(self.linkedlist), "120 --> 7102 --> None")
예제 #5
0
class Stack:
    def __init__(self):
        """
        Stack Implementation with Linked List
        """
        self.stack = SinglyLinkedList()

    def push(self, value):
        """
        Push some value on to the stack
        Args:
            value (Any): Any value you want to push to the stack.
        """
        self.stack.add(value)

    def pop(self):
        """
        pops out the last value from the stack.
        Returns:
            - Element that just popped off the stack
        """
        if self.stack.count() != 0:
            return self.stack.removeAt(self.stack.count() - 1)
        else:
            raise LookupError('Not enough element to pop')

    def __reversed__(self):
        return reversed(self.stack)

    def __iter__(self):
        raise NotImplementedError(
            'Iteration over the stack is not implemented yet')

    def peak_top(self):
        """
        Quickly peak at the very top of the stack
        Returns:
            - The top most element of the stack
        """
        return self.stack.get_head()

    def __len__(self):
        return len(self.stack)

    def isEmpty(self):
        """
        Says whether the stack is empty or not
        Returns:
            - Bool: True if no elements in the stack else False.
        """
        return True if self.stack.count() == 0 else False

    def __str__(self):
        """
        This is the string representation of the stack element's values
        Returns:
            - String representation of the stack element's values
        """
        return str(self.stack)
예제 #6
0
 def __init__(self):
     """
     Stack Implementation with Linked List
     """
     self.stack = SinglyLinkedList()
예제 #7
0
 def setUp(self) -> None:
     self.linkedlist = SinglyLinkedList()
예제 #8
0
class TestCases(unittest.TestCase):
    def setUp(self) -> None:
        self.linkedlist = SinglyLinkedList()

    def test_add(self):
        self.linkedlist.add(2)
        self.assertEqual(len(self.linkedlist), 1)
        self.assertEqual(self.linkedlist.get_head().value, 2)

    def test_isEmpty(self):
        self.assertEqual(self.linkedlist.isEmpty(), True)
        self.linkedlist.add(123)
        self.assertEqual(self.linkedlist.isEmpty(), False)

    def test_get_size(self):
        self.assertEqual(len(self.linkedlist), 0)
        self.linkedlist.add(3)
        self.assertEqual(len(self.linkedlist), 1)

    def test_get_head(self):
        self.assertEqual(self.linkedlist.get_head().value, None)
        self.linkedlist.add(4)
        self.assertEqual(self.linkedlist.get_head().value, 4)

    def test_str(self):
        self.linkedlist = SinglyLinkedList()
        self.linkedlist.add(20)
        self.linkedlist.add(120)
        self.linkedlist.add(7102)
        self.linkedlist.add(773)
        self.linkedlist.add('Hello')
        self.assertEqual(str(self.linkedlist),
                         "20 --> 120 --> 7102 --> 773 --> Hello --> None")

    def test_remove(self):
        self.linkedlist = SinglyLinkedList()
        self.linkedlist.add(20)
        self.linkedlist.add(120)
        self.linkedlist.add(7102)
        self.linkedlist.add(773)

        self.assertEqual(str(self.linkedlist),
                         '20 --> 120 --> 7102 --> 773 --> None')
        self.linkedlist.removeAt(0)
        self.assertEqual(str(self.linkedlist), '120 --> 7102 --> 773 --> None')

        self.linkedlist.removeAt(self.linkedlist.count() - 1)
        self.assertEqual(str(self.linkedlist), "120 --> 7102 --> None")

    def test_length(self):
        self.linkedlist = SinglyLinkedList()
        self.linkedlist.add(20)
        self.linkedlist.add(120)
        self.linkedlist.add(7102)
        self.linkedlist.add(773)

        self.assertEqual(len(self.linkedlist), 4)
        self.assertEqual(self.linkedlist.count(), 4)

    def test_getitem(self):
        self.linkedlist = SinglyLinkedList()
        self.linkedlist.add(20)
        self.linkedlist.add(120)
        self.linkedlist.add(7102)
        self.linkedlist.add(773)

        self.assertEqual(self.linkedlist[0], 20)
        self.assertEqual(self.linkedlist[1], 120)
        self.assertEqual(self.linkedlist[2], 7102)
        self.assertEqual(self.linkedlist[3], 773)