Difference between “enqueue” and “dequeue”

前端 未结 6 1594
盖世英雄少女心
盖世英雄少女心 2021-01-30 13:26

Can somebody please explain the main differences? I don\'t have a clear knowledge about these functions in programming for any language.

6条回答
  •  粉色の甜心
    2021-01-30 13:43

    Enqueue and Dequeue tend to be operations on a queue, a data structure that does exactly what it sounds like it does.

    You enqueue items at one end and dequeue at the other, just like a line of people queuing up for tickets to the latest Taylor Swift concert (I was originally going to say Billy Joel but that would date me severely).

    There are variations of queues such as double-ended ones where you can enqueue and dequeue at either end but the vast majority would be the simpler form:

               +---+---+---+
    enqueue -> | 3 | 2 | 1 | -> dequeue
               +---+---+---+
    

    That diagram shows a queue where you've enqueued the numbers 1, 2 and 3 in that order, without yet dequeuing any.


    By way of example, here's some Python code that shows a simplistic queue in action, with enqueue and dequeue functions. Were it more serious code, it would be implemented as a class but it should be enough to illustrate the workings:

    import random
    
    def enqueue(lst, itm):
        lst.append(itm)        # Just add item to end of list.
        return lst             # And return list (for consistency with dequeue).
    
    def dequeue(lst):
        itm = lst[0]           # Grab the first item in list.
        lst = lst[1:]          # Change list to remove first item.
        return (itm, lst)      # Then return item and new list.
    
    # Test harness. Start with empty queue.
    
    myList = []
    
    # Enqueue or dequeue a bit, with latter having probability of 10%.
    
    for _ in range(15):
        if random.randint(0, 9) == 0 and len(myList) > 0:
            (itm, myList) = dequeue(myList)
            print(f"Dequeued {itm} to give {myList}")
        else:
            itm = 10 * random.randint(1, 9)
            myList = enqueue(myList, itm)
            print(f"Enqueued {itm} to give {myList}")
    
    # Now dequeue remainder of list.
    
    print("========")
    while len(myList) > 0:
        (itm, myList) = dequeue(myList)
        print(f"Dequeued {itm} to give {myList}")
    

    A sample run of that shows it in operation:

    Enqueued 70 to give [70]
    Enqueued 20 to give [70, 20]
    Enqueued 40 to give [70, 20, 40]
    Enqueued 50 to give [70, 20, 40, 50]
    Dequeued 70 to give [20, 40, 50]
    Enqueued 20 to give [20, 40, 50, 20]
    Enqueued 30 to give [20, 40, 50, 20, 30]
    Enqueued 20 to give [20, 40, 50, 20, 30, 20]
    Enqueued 70 to give [20, 40, 50, 20, 30, 20, 70]
    Enqueued 20 to give [20, 40, 50, 20, 30, 20, 70, 20]
    Enqueued 20 to give [20, 40, 50, 20, 30, 20, 70, 20, 20]
    Dequeued 20 to give [40, 50, 20, 30, 20, 70, 20, 20]
    Enqueued 80 to give [40, 50, 20, 30, 20, 70, 20, 20, 80]
    Dequeued 40 to give [50, 20, 30, 20, 70, 20, 20, 80]
    Enqueued 90 to give [50, 20, 30, 20, 70, 20, 20, 80, 90]
    ========
    Dequeued 50 to give [20, 30, 20, 70, 20, 20, 80, 90]
    Dequeued 20 to give [30, 20, 70, 20, 20, 80, 90]
    Dequeued 30 to give [20, 70, 20, 20, 80, 90]
    Dequeued 20 to give [70, 20, 20, 80, 90]
    Dequeued 70 to give [20, 20, 80, 90]
    Dequeued 20 to give [20, 80, 90]
    Dequeued 20 to give [80, 90]
    Dequeued 80 to give [90]
    Dequeued 90 to give []
    

提交回复
热议问题