Esempio n. 1
0
    def test_2_2(self):
        mylist = UnorderedList()
        numbers = [31, 77, 17, 93, 26, 54]
        for number in numbers:
            mylist.add(number)

        # The linked list should contain 6 items overall
        self.assertEqual(mylist.size(), 6)

        # Let n equal 3 as we want the node at indice 3 which
        # will enable us to reach through to the last node in the list.
        newNode = mylist.findFromNth(3)
        self.assertEqual(newNode.getData(), 93)

        # Test that we can request the first item in the list
        newNode = mylist.findFromNth(0)
        self.assertEqual(newNode.getData(), 31)

        # Test that we can request the last item in the list
        newNode = mylist.findFromNth(mylist.size() - 1)
        self.assertEqual(newNode.getData(), 54)

        # Test that we can't request something by passing in a non-existent indice
        newNode = mylist.findFromNth(7)
        self.assertEqual(newNode.getData(), None)
class StackLinkedList:
    def __init__(self):
        '''Stack()创建一个新的空栈。不需要参数,并返回一个空栈'''
        self.items = UnorderedList()

    def push(self, item):
        '''Push(item)将新项添加到堆栈的顶部。它需要参数 item 并且没有返回值'''
        self.items.add(item)

    def pop(self):
        '''pop()从栈顶删除项目。它不需要参数,返回 item。栈被修改'''
        return self.items.pop()

    def peek(self):
        """返回栈顶的项,不删除它。它不需要参数。堆栈不被修改。"""
        return self.items.index(0)

    def isEmpty(self):
        """测试看栈是否为空。它不需要参数,返回一个布尔值。"""
        return self.items.isEmpty()

    def size(self):
        """返回栈的项目数。它不需要参数,返回一个整数。"""
        return self.items.size()

    def __str__(self):
        return str(self.items)
Esempio n. 3
0
    def test_2_1(self):
        mylist = UnorderedList()
        numbers = [31, 77, 17, 93, 93, 26, 26, 26, 54, 54, 54]
        for number in numbers:
            mylist.add(number)

        # The linked list should contain 11 items overall
        self.assertEqual(mylist.size(), 11)

        duplicate_results = mylist.find_duplicates()

        # The duplicates should be:
        # 93 occurs a second time
        # 26 occurs a third time
        # 54 occurs a third time
        self.assertEqual(duplicate_results[93], 2)
        self.assertEqual(duplicate_results[26], 3)
        self.assertEqual(duplicate_results[54], 3)

        mylist.remove_duplicates()

        # The size of the Unordered Linked list should be 6
        self.assertEqual(mylist.size(), 6)
        # There should still be an occurrence of every digit:
        # 31, 77, 17, 93, 26, 54
        self.assertTrue(mylist.search(31))
        self.assertTrue(mylist.search(77))
        self.assertTrue(mylist.search(17))
        self.assertTrue(mylist.search(93))
        self.assertTrue(mylist.search(26))
        self.assertTrue(mylist.search(54))

        # There should be no duplicates remaining, running the find_duplicates function
        # should still leave the linkedlist untouched
        mylist.remove_duplicates()
        self.assertEqual(mylist.size(), 6)
Esempio n. 4
0
NUM = 20

myList = UnorderedList()

for i in xrange(NUM):

    value = random.randint(1, 100)
    #temp = Node(value)

    #print temp, temp.getData()

    myList.add(value)

#exit()

print myList.size()
myList.traversal()
myList.add(99)
myList.add(75)
myList.add(92)

print

myList.search(99)
myList.search(62)

myList.remove(70)
myList.traversal()

myList.remove(99)
myList.traversal()
Esempio n. 5
0
 def size(self):
     return UnorderedList.size(self)
Esempio n. 6
0
except IOError:
    sys.exit("\n\n\nUnable to Open File!!!\n\n\n")

while True:
    line = fp.readline()
    if line == "":
        break

    line = line.strip()
    field = line.split()
    data = float(field[0])

    if data <= SHORT:
        jobListShort.add(data)

    elif data <= MEDIUM:
        jobListMedium.add(data)

    else:
        jobListLong.add(data)

#jobListShort.traversal()

print "Number of Process in Short Linked List  :", jobListShort.size()

print "Number of Process in Medium Linked List :", jobListMedium.size()

print "Number of Process in Long Linked List   :", jobListLong.size()

#print "Total execution Time is: ", jobListShort.Sum()
Esempio n. 7
0
from unorderedList import UnorderedList

if __name__ == "__main__":
    ml = UnorderedList()
    ml.add(1)
    ml.add("haha")
    ml.add("wut")
    print(ml)
    print(ml.size())
    #print(ml.pop(3))
    print(ml.end_item())
    print(ml.append("this is the end"))
    print(ml)
    print(ml.append("this is the final end"))
    print(ml)
    ml.insert(12,0)
    print(ml)
    print(ml.insert(12,6))
    print(ml)