Beispiel #1
0
class Queue:
    """
    Implementation of Queue data structure using
    singly-linked list.
    """
    def __init__(self):
        self.queue = LinkedList()

    def __len__(self):
        return len(self.queue)

    def __str__(self):
        return str(self.queue)

    def enqueue(self, x):
        """
        Add element x to the queue.
        """
        self.queue.push(x)

    def dequeue(self):
        """
        Remove first element of the queue.
        """
        self.queue.delete_index(0)
def main():
    llist = LinkedList()
    llist.push(7)
    llist.push(1)
    llist.push(3)
    llist.push(2)
    llist.push(8)
    llist.append(10)
    llist.insert(llist.get_head().get_next(), 5)

    llist.print_list()
    print("Reversing a list")
    reverse(llist)
    llist.print_list()

    print("Reversing a list again using recursion")
    reverse_rec(llist)
    llist.print_list()
def main():
    llist = LinkedList()
    llist.push(7)
    llist.push(1)
    llist.push(3)
    llist.push(2)
    llist.push(8)
    llist.append(10)
    llist.insert(llist.get_head().get_next(), 5)

    llist.print_list()

    print(find_middle(llist))

    llist.delete_node(2)
    llist.print_list()

    print(find_middle(llist))
Beispiel #4
0
def main():
    llist = LinkedList()
    llist.push(20)
    llist.push(4)
    llist.push(15)
    llist.push(10)

    # Create a loop for testing
    llist.head.next.next.next.next = llist.head

    if (detect_loop_hash(llist)):
        print("Loop found")
    else:
        print("No Loop ")

    if (detect_loop_floyds(llist)):
        print("Loop found")
    else:
        print("No Loop ")

    print(count_nodes_in_loop(llist))
Beispiel #5
0
    else:
        llist.set_head(currY)


    if prevY is not None:
        prevY.set_next(currX)
    else:
        llist.set_head(currX)

    # Second connect the next of current nodes
    temp = currX.get_next()
    currX.set_next(currY.get_next())
    currY.set_next(temp)

llist = LinkedList()
llist.push(7)
llist.push(1)
llist.push(3)
llist.push(2)
llist.push(8)

print("Created Linked List:")
llist.print_list()

swap_nodes(llist, 2, 1)
llist.print_list()

swap_nodes(llist,8,1)
llist.print_list()

swap_nodes(llist,2,7)