コード例 #1
0
class Stack:
    """Stack data structure implementation using linked list"""
    def __init__(self, max_size):
        if not isinstance(max_size, int):
            raise Exception('size must be int greater than 0')

        if (max_size < 1):
            raise Exception('size must be int greater than 0')

        self.top = 0
        self.max_size = max_size
        self.llist = SinglyLinkedList()

    def push(self, val):
        """Push item onto stack"""

        if self.top >= self.max_size:
            raise Exception('Stack is full')

        self.llist.add(val)
        self.top += 1

    def pop(self):
        """Pop item from stack"""

        if self.top == 0:
            raise Exception('Stack is empty')

        self.top -= 1
        return self.llist.remove()
コード例 #2
0
class Queue:
    """Queue data structure implementation using linked list"""
    
    def __init__(self, max_size):
        if not isinstance(max_size, int):
            raise Exception('size must be int greater than 0')
        
        if (max_size < 1):
            raise Exception('size must be int greater than 0')

        self.top = 0
        self.max_size = max_size
        self.llist = SinglyLinkedList()

    def enqueue(self, val):
        """add item into queue"""
        
        if self.top >= self.max_size:
            raise Exception('Queue is full')

        self.llist.add(val)
        self.top += 1

    def dequeue(self):
        """remove item from queue"""
        
        if self.top == 0:
            raise Exception('Queue is empty')
        
        self.top -= 1
        return self.llist.remove_tail()
コード例 #3
0
def merge(left, right):
    """
    Merges two linked lists, sorting by data in nodes
    Returns a new, merged list
    Runs in O(n) time
    """

    # Create a new linked list that contains nodes from
    # merging left and right
    merged = SinglyLinkedList()

    # Add a fake head that is discarded later
    merged.add(0)

    # Set current to the head of the linked list
    current = merged.head

    # Obtain head nodes for left and right linked lists
    left_head = left.head
    right_head = right.head

    # Iterate over left and right until we reach the tail node
    # of either
    while left_head or right_head:
        # If the head node of left is None, we're past the tail
        # Add the node from right to merged linked list
        if left_head is None:
            current.next_node = right_head
            # Call next on right to set loop condition to False
            right_head = right_head.next_node
        # If the head node of right is None, we're past the tail
        # Add the tail node from left to merged linked list
        elif right_head is None:
            current.next_node = left_head
            # Call next on left to set loop condition to False
            left_head = left_head.next_node
        else:
            # Not at either tail node
            # Obtain node data to perform comparison operations
            left_data = left_head.data
            right_data = right_head.data
            # If data on left is less than right, set current to left node
            if left_data < right_data:
                current.next_node = left_head
                # Move left head to next node
                left_head = left_head.next_node
            # If data on left is greater than right, set current to right node
            else:
                current.next_node = right_head
                # Move right head to next node
                right_head = right_head.next_node
        # Move current to next node
        current = current.next_node

    # Discard fake head and set first merged node as head
    head = merged.head.next_node
    merged.head = head

    return merged
コード例 #4
0
ファイル: zadanie_2.py プロジェクト: ReTTy98/Algorytmy
    office_1.add_worker(Worker('A'))
    office_1.add_worker(Worker('B'))
    office_1.add_worker(Worker('C'))

for i in range(2):
    office_2.add_worker(Worker('A'))
    office_2.add_worker(Worker('B'))
    office_2.add_worker(Worker('C'))
    office_2.add_worker(Worker('E'))


queue_1 = SinglyLinkedList()
queue_2 = SinglyLinkedList()

for i in range(30):
    queue_1.add(Client())
    queue_2.add(Client())


simulation.run_simulation(office_1, queue_1)
simulation.run_simulation(office_2, queue_2)


office_1.print_summary()
office_2.print_summary()

del office_1
del office_2
del queue_1
del queue_2
コード例 #5
0
office = Office()
queue = SinglyLinkedList()

# ma być: 3xA, 3xB, 3xC, 1xE

for i in range(3):
    office.add_worker(Worker('A'))
    office.add_worker(Worker('B'))
    office.add_worker(Worker('C'))

office.add_worker(Worker('E'))

for i in range(30):
    c = Client()
    print(c, 'lines up')
    queue.add(c)

simulation.run_simulation(office, queue)

office.print_summary()
'''
# Propozycja usprawnienia:

Wykorzystanie stringów określających typ pracownika
i porównywanie tych stringów z typem zadania nie jest
wydajne, porównania łańcuchów znaków są z natury powolne,
nawet jesli mówimy o łańcuchach jednoznakowych

Szybszą metodą byłoby wykorzystanie zestawu flag bitowych
określających typ zadania: na przykład
コード例 #6
0
            # If data on left is less than right, set current to left node
            if left_data < right_data:
                current.next_node = left_head
                # Move left head to next node
                left_head = left_head.next_node
            # If data on left is greater than right, set current to right node
            else:
                current.next_node = right_head
                # Move right head to next node
                right_head = right_head.next_node
        # Move current to next node
        current = current.next_node

    # Discard fake head and set first merged node as head
    head = merged.head.next_node
    merged.head = head

    return merged


l = SinglyLinkedList()
l.add(10)
l.add(2)
l.add(44)
l.add(15)
l.add(200)

print(l)
sorted_linked_list = merge_sort(l)
print(sorted_linked_list)
コード例 #7
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)