52

有人可以解释主要区别吗?我对任何语言的编程中的这些功能都没有清楚的了解。

4

6 回答 6

58

C 和 C++ 等编程语言中的一些基本数据结构是堆栈和队列。

堆栈数据结构遵循“先进后出”策略 (FILO),其中插入或“推入”堆栈的第一个元素是从堆栈中移除或“弹出”的最后一个元素。

类似地,队列数据结构遵循“先进先出”策略(就像我们站在柜台排队时的普通队列一样),其中第一个元素被推入队列或“入队”,同样必须从队列中删除的元素是“出队”。

这与堆栈中的 push 和 pop 非常相似,但术语 enqueue 和 dequeue 避免了关于使用的数据结构是堆栈还是队列的混淆。

Class coders 有一个简单的程序来演示入队和出队过程。您可以查看以供参考。

http://classcoders.blogspot.in/2012/01/enque-and-deque-in-c.html

于 2013-05-08T05:53:21.577 回答
44

Enqueue并且Dequeue往往是队列上的操作,队列是一种数据结构,听起来就像它所做的那样。

你在一端排队,另一端排队,就像一队人在排队买最新的泰勒斯威夫特演唱会的门票(我原本打算说比利乔尔,但那会让我严重约会)。

队列有多种变体,例如双端队列,您可以在任一端入队和出队,但绝大多数将是更简单的形式:

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

该图显示了一个队列,您已按该顺序将数字 1、2 和 3 加入队列,但尚未使任何队列出队。


举例来说,这里有一些 Python 代码,显示了一个简单的队列,其中包含enqueuedequeue函数。如果它是更严肃的代码,它将被实现为一个类,但它应该足以说明工作原理:

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}")

一个示例运行显示它正在运行:

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 []
于 2013-05-08T05:41:51.573 回答
13

这些是描述“FIFO”队列时通常使用的术语,即“先进先出”。这就像一条线。你决定去看电影。买票排了很长的队,你决定排队买票,那就是“入队”。在某个时刻,您排在队伍的最前面,您可以买一张票,然后您就离开了队伍,这就是“出队”。

于 2013-05-08T05:41:41.217 回答
4

队列是某种双面数据结构。您可以在一侧添加新元素,并从另一侧删除元素(与只有一侧的堆栈相反)。入队意味着添加一个元素,出队意味着删除一个元素。请看这里

于 2013-05-08T05:41:18.787 回答
2

队意味着添加一个元素,出队意味着删除一个元素。

var stackInput= []; // First stack
var stackOutput= []; // Second stack

// For enqueue, just push the item into the first stack
function enqueue(stackInput, item) {
  return stackInput.push(item);
}

function dequeue(stackInput, stackOutput) {
  // Reverse the stack such that the first element of the output stack is the
  // last element of the input stack. After that, pop the top of the output to
  // get the first element that was ever pushed into the input stack
  if (stackOutput.length <= 0) {
    while(stackInput.length > 0) {
      var elementToOutput = stackInput.pop();
      stackOutput.push(elementToOutput);
    }
  }

  return stackOutput.pop();
}
于 2020-02-18T22:32:12.677 回答
1

在我看来,这是描述该过程的最糟糕的选择词之一,因为它与现实生活中的任何事物或类似事物无关。一般来说,“队列”这个词很糟糕,好像发音一样,听起来像英文字符“q”。看到这里的低效率了吗?

enqueue:将某物放入队列中;将元素添加到队列的尾部;

出队从队列中取出一些东西;从队列头部移除第一个可用元素

来源:https ://www.thefreedictionary.com

于 2017-10-18T06:18:22.163 回答