Example #1
0
 def test_one_node(self):
     ll = linked_list()
     self.assertFalse(ll.oneNode())
     ll.push_back(1)
     self.assertTrue(ll.oneNode())
     ll.pop_back()
     self.assertFalse(ll.oneNode())
Example #2
0
 def test_make_empty(self):
     ll = linked_list()
     self.assertTrue(ll.empty())
     ll.makeEmpty()
     self.assertTrue(ll.empty())
     ll.push_back(1)
     self.assertFalse(ll.empty())
     ll.makeEmpty()
     self.assertTrue(ll.empty())
Example #3
0
 def test_push_front_pop_front(self):
     ll = linked_list()
     ll.push_front(1)
     ll.push_front(2)
     ll.push_front(3)
     self.assertEquals(ll.pop_front(), 3)
     self.assertEquals(ll.pop_front(), 2)
     self.assertEquals(ll.pop_front(), 1)
     self.assertTrue(ll.empty())
Example #4
0
 def test_push_back_pop_back(self):
     ll = linked_list()
     ll.push_back(1)
     ll.push_back("foo")
     ll.push_back([3,2,1])
     self.assertFalse(ll.empty())
     self.assertEquals(ll.pop_back(),[3,2,1])
     self.assertEquals(ll.pop_back(), "foo")
     self.assertEquals(ll.pop_back(), 1)
     self.assertTrue(ll.empty())
Example #5
0
 def test_str(self):
     ll = linked_list()
     self.assertEquals(str(ll),"[]")
     ll.push_back(1)
     self.assertEquals(str(ll),"[1]")
     ll.push_back(2)
     self.assertEquals(str(ll),"[1,2]")
     ll.pop_back()
     self.assertEquals(str(ll),"[1]")
     ll.pop_back()
     self.assertEquals(str(ll),"[]")
Example #6
0
 def test_len(self):
     ll = linked_list()
     self.assertEquals(len(ll),0)
     ll.push_back(1)
     self.assertEquals(len(ll),1)
     ll.push_back(2)
     self.assertEquals(len(ll),2)
     ll.pop_back()
     self.assertEquals(len(ll),1)
     ll.pop_back()
     self.assertEquals(len(ll),0)
Example #7
0
 def test_reverse(self):
     ll = linked_list()
     ll.reverse()
     self.assertTrue(ll.empty())
     ll.push_back(1)
     ll.push_back(2)
     ll.push_back(3)
     ll.reverse()
     self.assertEquals(ll.pop_back(),1)
     self.assertEquals(ll.pop_back(),2)
     self.assertEquals(ll.pop_back(),3)
Example #8
0
    def fact(self, a):
        if a < 0:
            raise ValueError("Less than zero")
        if a == 0 or a == 1:
            return 1

        stack = linked_list()
        while a > 1:
            stack.push_front(a)
            a -= 1

        result = 1
        while not stack.empty():
            result *= stack.pop_front()

        return result
###########################################################################
#   Neha Gupta
#   Implementation of Linked List in Python
#   Google Jump
#   Supervised by: Laura Beth Lincoln
###########################################################################

from linkedList import node
from linkedList import linked_list

x = linked_list()
x.insert(1)
x.insert("2")
print("Our initial list, after inserting 2 things")
print(x)

x.insert(5.0, 1)
print("Now the list after inserting 5.0 at the first spot")
print(x)

x.insert(3, 2)
print("Now the list after inserting 3 at the 2nd spot")
print(x)

y = node(3)
x.insert(y)
print("Now the list after inserting a node, {}, at the end".format(y))
print(x)

x.delete("2")
print("The list after deleting 2:")
Example #10
0
 def test_pop_back_empty(self):
     self.assertRaises(RuntimeError, lambda: linked_list().pop_back())
Example #11
0
 def test_none(self):
     self.assertTrue(linked_list().empty())