Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

queue python

"""Linear Queue
Queue is a First In - First Out (FIFO) data structure much like a real queue
	- The below is a simple implementation of a Linear Queue

Check out the source for more info!
"""

class LQ:
    def __init__(self):
        self.q = []
    
    def enq(self, data): # enqueue
        self.q.append(data)
        
    def deq(self): #dequeue
        if self.q:
            return self.q.pop(0)
        return "Empty!"
    
    def peek(self):
        if self.q:
            return self.q[0]
        return "Empty!"
    
    def size(self):
        return len(self.q)
    
    def display(self):
        return self.q

# Test
from random import randrange
def LQ_Test(LQ):
    # Create random list
    lst = [randrange(10, 100) for _ in range(10)]
    print(f"List: {lst}")
    
    # Create Empty LQ
    LQ = LQ()
    print(f"LQ: {LQ.display()}",
          f"Size: {LQ.size()}",
          f"Front: {LQ.peek()}",
          sep = " | ", end = "

")
    
    # Put items into LQ
    for item in lst: 
        LQ.enq(item)
    print(f"Front: {LQ.peek()}",
          f"LQ: {LQ.display()}",
          sep = " | ", end = "

")
    
    # Remove items from LQ
    for _ in range(len(lst)): 
        print(f"Deq: {LQ.deq()}", 
              f"Front: {LQ.peek()}",
              f"Size: {LQ.size()}", 
              f"LQ: {LQ.display()}",
              sep = " | ")
LQ_tester(LQ)
Source by www.programiz.com #
 
PREVIOUS NEXT
Tagged: #queue #python
ADD COMMENT
Topic
Name
3+9 =