コード例 #1
0
class Stack():
    def __init__(self, size):
        self.max_size = size
        self.size = 0
        self.storage = DoublyLinkedList()

    def push(self, val):
        if self.size >= self.max_size:
            raise MemoryError("Stack is full")

        self.storage.add(val)
        self.size += 1

    def pop(self):
        if self.size > 0:
            self.storage.remove_end()
            self.size -= 1
            return True
        else:
            return False

    def peek(self):
        if self.size > 0:
            return self.storage.get_at_index(self.size - 1).value

    def __str__(self):
        val = self.storage.print_list(True)
        var = val.split("\n")
        final_val = ""
        for x in range(len(var)):
            final_val += var[len(var) - x - 1]
            if x < len(var) - 1:
                final_val += "\n"

        return final_val
コード例 #2
0
class Stack():

    #Initialize the stack data using the doubly linked list we created before
    def __init__(self):
        self.data_list = DoublyLinkedList()

    #Reference the sizeOf function from doubly linked lists
    def sizeOf(self):
        return self.data_list.sizeOf()

    def isEmpty(self):
        return self.sizeOf() == 0

    #Add to the top of the stack
    def push(self, elem):
        self.data_list.add(elem)

    #Remove from the top of the stack
    def pop(self):
        if self.isEmpty():
            print('List is empty')
        else:
            self.data_list.removeLast()

    #Peak at what is on the top of the stack
    def stackPeak(self):
        return self.data_list.peakLast()
コード例 #3
0
class Queue:
    def __init__(self, size=sys.maxsize):
        self.max_size = size
        self.storage = DoublyLinkedList()
        self.size = 0

    def enqueue(self, val):
        if self.size >= self.max_size:
            raise MemoryError("Queue is full")

        self.storage.add(val)
        self.size += 1

    def dequeue(self):
        if self.size < 1:
            raise MemoryError("Queue is empty")

        self.storage.remove_at_index(0)
        self.size -= 1

    def peek(self):
        if self.size < 1:
            return "None"

        return self.storage.get_at_index(0).value

    def __str__(self):
        return self.storage.print_list(True)
コード例 #4
0
def test_dll_add():
    test_dll = DLL()
    test_dll.add(1)
    assert test_dll.head.data == 1
    assert test_dll.tail.data == 1
    test_dll.add(2)
    assert test_dll.head.data == 1
    assert test_dll.tail.data == 2
コード例 #5
0
 def test_filling(self):
     llist = DoublyLinkedList()
     s = 0
     for i in range(10):
         llist.add(i)
     for i in range(10):
         s += llist.remove_first()
     self.assertEqual(0, len(llist))
     self.assertEqual(45, s)
コード例 #6
0
class TestDoublyLinkedList(TestLinkedList):
	# Override
	def setUp(self):
		self.test_list = DoublyLinkedList()
	def test_delete_node(self):
		first = self.test_list.add(3)
		second = self.test_list.add(2)
		self.assertEqual(2, self.test_list.size)
		self.test_list.remove_node(first)
		self.assertEqual(1, self.test_list.size)
		self.test_list.remove_node(second)
		self.assertEqual(0, self.test_list.size)
コード例 #7
0
ファイル: queue.py プロジェクト: tibno99/learning-Python
class Queue():
    def __init__(self):
        self.data = DoublyLinkedList()

    def sizeOf(self):
        return self.data.sizeOf()

    def isEmpty(self):
        return self.data.sizeOf() == 0

    def peek(self):
        return self.data.peakFirst()

    def poll(self):
        self.data.removeFirst()

    def offer(self, val):
        self.data.add(val)
コード例 #8
0
def special_copy(DL1, DL2):
    """
    This function should return a new doubly linked list which contains the same element of DL1 concatenated with
    the elements of DL2 in reverse order.
     Ex. DL1 = 4 <-> 5 <-> 6, DL2 = 9 <-> 4 <-> 6 >>> returns DL3 = 4 <-> 5 <-> 6 <-> 6 <-> 4 <-> 9]
    Note: this function does not modify L1 or L2
    Note2: the method "add" of the DoublyLinkedList class adds element *at the tail* of the list
    :param DL1: a doubly linked list
    :param DL2: a doubly linked list
    """
    new = DoublyLinkedList()
    walk = DL1._head
    while walk is not None:
        new.add(walk._data)
        walk = walk._next
    walk = DL2._tail
    while walk is not None:
        new.add(walk._data)
        walk = walk._prev
    return new
コード例 #9
0
 def test_index_out_of_bounds4(self):
     ar = DoublyLinkedList()
     for _ in range(1000):
         ar.add("x")
     self.assertRaises(IndexError, ar.remove_at, -1)
コード例 #10
0
 def test_index_out_of_bounds(self):
     ar = DoublyLinkedList()
     ar.add(None)
     ar.add(1)
     ar.add("")
     self.assertRaises(IndexError, ar.remove_at, 3)
コード例 #11
0
    while walk is not None:
        new.add(walk._data)
        walk = walk._next
    walk = DL2._tail
    while walk is not None:
        new.add(walk._data)
        walk = walk._prev
    return new


""" main to do some testing"""
if __name__ == '__main__':
    """ create two doubly linked lists and add some datastore"""
    DL1 = DoublyLinkedList()
    DL2 = DoublyLinkedList()
    DL1.add("A")
    DL1.add("B")
    DL1.add("Y")
    DL1.add("Z")
    DL1.print()
    DL2.add(67)
    DL2.add(69)
    DL2.add(25)
    DL2.add(29)
    DL2.add(76)
    DL2.print()
    """ test swap """
    print("========== Test swap() ===================================")
    swap_first_with_last(DL2)
    DL2.print()
    print(str(DL2.__len__()))
コード例 #12
0
from doubly_linked_list import DoublyLinkedList
from singly_linked_list import SinglyLinkedList

if __name__ == '__main__':
    first = 0
    middle = 50000
    last = 100000

    print 'Running doubly linked list'
    doubly_linked_list = DoublyLinkedList()

    [doubly_linked_list.add(x) for x in range(last)]

    doubly_linked_list.remove(first)
    doubly_linked_list.remove(middle)
    doubly_linked_list.remove(last - 1)
    doubly_linked_list.remove(-1)

    print '\nRunning singly linked list'
    singly_linked_list = SinglyLinkedList()
    [singly_linked_list.add(x) for x in range(last)]

    singly_linked_list.remove(first)
    singly_linked_list.remove(middle)
    singly_linked_list.remove(last - 1)
    singly_linked_list.remove(-1)
コード例 #13
0
class DoublyLinkedListTest(unittest.TestCase):
    
    def setUp(self):
        self.list=DoublyLinkedList()

    def test_add(self):
        self.list.add(1)
        self.assertEqual(self.list.get_size(),1)
    
    def test_addMultiple(self):
        for i in range(4):
            self.list.add(i)
        self.assertEqual(self.list.get_size(),4)
    
    def test_add_to_index(self):
        self.list.add_to_index(5,0)
        self.assertEqual(self.list.get(0),5)
        self.list.add(1)
        self.list.add(3)
        self.list.add_to_index(2,1)
        self.assertEqual(self.list.get(1),2)
        self.list.add_to_index(0,0)
        self.assertEqual(self.list.get(0),0)
        self.list.add_to_index(4,4)
        self.assertEqual(self.list.get(4),4)
    
    def test_get(self):
        self.list.add_multiple(1,2,3)
        self.assertEqual(self.list.get(1),2)
    
    def test_get_last(self):
        self.list.add_multiple(1,2,3)
        self.assertEqual(self.list.get_last(),3)
    
    def test_get_first(self):
        self.list.add_multiple(1,2,3)
        self.assertEqual(self.list.get_first(),1)
    
    def test_remove(self):
        self.list.add_multiple(1,2,3,4,5)
        self.list.remove(1)
        self.assertEqual(self.list.get(1),3)
        self.list.remove(0)
        self.assertEqual(self.list.get(0),3)
    
    def test_remove_last(self):
        self.list.add_multiple(1,2,3,4,5)
        self.list.remove_last()
        self.assertEqual(self.list.get_last(),4)

    def test_pop(self):
        self.list.add_multiple(1,2,3)
        self.assertEqual(self.list.pop(),3)
        self.assertEqual(self.list.pop(),2)
    
    def test_get_size(self):
        self.assertEqual(self.list.get_size(),0)
        self.list.add_multiple(1,2,3)
        self.assertEqual(self.list.get_size(),3)
    
    def test_is_empty(self):
        self.assertTrue(self.list.is_empty())
        self.list.add_multiple(1,2,3)
        self.assertFalse(self.list.is_empty())

    def test_str(self):
        self.list.add(1)
        self.list.add(4)
        self.list.add(3)
        self.assertEqual(str(self.list),"[1,4,3]")
コード例 #14
0
    # var.add(2)
    # var.add(3)
    # var.add(2)
    # var.print_list()
    # print("-----")
    # var = dedup(var)
    # var.print_list()
    # print("-----")
    # print("-----")

    # var = DoublyLinkedList()
    # var.add(1)
    # var.add(2)
    # var.add(2)
    # var.add(2)
    # var.print_list()
    # print("-----")
    # var = dedup(var)
    # var.print_list()
    # print("-----")
    # print("-----")

    var = DoublyLinkedList()
    var.add(1)
    var.add(2)
    var.add(2)
    var.add(2)
    var.print_list()
    print("-----")
    var = dedup_smaller(var)
    var.print_list()