Exemplo n.º 1
0
 def sort(self):
     slash_counts = self.get_slash_counts()
     sorted_items = sorted(slash_counts.items(), key=lambda x: x[1])
     new_ll = LinkedList()
     for (k, v) in sorted_items:
         new_ll.append(k)
     self.values = new_ll
Exemplo n.º 2
0
 def test_findlast(self):
     l = LinkedList(1)
     l.append(2)
     l.append(3)
     l.append(4)
     l.append(5)
     n = l.find(5)
     self.assertEqual(n.value, 5)
     self.assertEqual(n.next, None)
Exemplo n.º 3
0
 def test_remove(self):
     l = LinkedList(1)
     l.append(2)
     l.append(3)
     l.append(4)
     l.append(5)
     l.remove(3)
     self.assertEqual(l.value, 1)
     self.assertEqual(l.next.value, 2)
     self.assertEqual(l.next.next.value, 4)
     self.assertEqual(l.length(), 4)
Exemplo n.º 4
0
 def test_append(self):
     l = LinkedList(1)
     l.append(2)
     l.append(3)
     l.append(4)
     l.append(5)
     n = l.find(4)
     self.assertEqual(n.value, 4)
     self.assertEqual(l.length(), 5)
     self.assertEqual(n.next.value, 5)
     self.assertEqual(n.next.next, None)
Exemplo n.º 5
0
class Queue():
    def __init__(self):
        self.values = LinkedList()

    def add(self, newVal):
        self.values.append(newVal)

    def remove(self):
        self.values.remove_from_front()

    def itemAt(self, index):
        return self.values.itemAt(index)

    def indexOf(self, value):
        return self.values.indexOf(value)

    def __len__(self):
        return self.values.length

    def __str__(self):
        return str(self.values)
Exemplo n.º 6
0
import sys
sys.path.insert(0, '../')

from ll import LinkedList

ll = LinkedList()
ll.append(20)
ll.prepend(19)
ll.append(21)
print("Is 21 present?", ll.find(21))
print("Is 22 present?", ll.find(22))
print(ll)
print("Deleting", ll.delete(20))
print(ll)
print("Deleting 23", ll.delete(23))
ll.insertAtPos(20, 1)
print("After insertion of 20 at 1", ll)
print("Cycle length", ll.detectCycleLen())
print("Creating a 3-cycle", ll.createcycle(19))
print("Next of 21:", ll.last.next)
print("Cycle length", ll.detectCycleLen())
print("Chain length", ll.findlen())
ll.createcycle(20)
print("Changing to 2-cycle", ll.last, "<->", ll.last.next)
print("Cycle length", ll.detectCycleLen())
print("Chain length", ll.findlen())
print("LL now:", ll)
ll.delete(19)
print("Deleting head", ll)
ll.delete(21)
print("Deleting last", ll)