【发布时间】:2013-05-02 06:05:33
【问题描述】:
有人能解释一下主要区别吗?我对任何语言的编程中的这些函数都没有清楚的了解。
【问题讨论】:
标签: queue programming-languages
有人能解释一下主要区别吗?我对任何语言的编程中的这些函数都没有清楚的了解。
【问题讨论】:
标签: queue programming-languages
C 和 C++ 等编程语言中的一些基本数据结构是堆栈和队列。
堆栈数据结构遵循“先进后出”策略 (FILO),其中插入或“推入”堆栈的第一个元素是从堆栈中移除或“弹出”的最后一个元素。
同样,队列数据结构遵循“先进先出”策略(就像我们在柜台排队时的普通队列一样),其中第一个元素被推入队列或“入队”当必须从队列中删除时,相同的元素是“Dequeued”。
这与堆栈中的 push 和 pop 非常相似,但术语 enqueue 和 dequeue 避免混淆使用的数据结构是堆栈还是队列。
Class coders 有一个简单的程序来演示入队和出队过程。您可以查看它以供参考。
http://classcoders.blogspot.in/2012/01/enque-and-deque-in-c.html
【讨论】:
FILO 的首字母缩写词(某些糕点产品除外)。一直是LIFO,虽然意思相同,但可能更常见:-)
Enqueue 和 Dequeue 往往是对队列的操作,这是一种数据结构,其功能与听起来完全一样。
您将物品排在一边,另一边排在队列中,就像一队人在排队买最新的泰勒·斯威夫特演唱会的门票(我原本打算说比利·乔尔,但那会让我严重约会)。
队列有多种变体,例如双端队列,您可以在任一端入队和出队,但绝大多数是更简单的形式:
+---+---+---+
enqueue -> | 3 | 2 | 1 | -> dequeue
+---+---+---+
该图显示了一个队列,您已按顺序将数字 1、2 和 3 排入队列,但尚未将任何数字排入队列。
作为示例,这里有一些 Python 代码,其中显示了一个简单的队列,其中包含 enqueue 和 dequeue 函数。如果是更严肃的代码,它会被实现为一个类,但它应该足以说明工作原理:
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 []
【讨论】:
这些是描述“FIFO”队列时通常使用的术语,即“先进先出”。这就像一条线。你决定去看电影。买票排了很长的队,你决定排队买票,那就是“入队”。在某个时刻你排在队伍的最前面,你可以买一张票,然后你离开队伍,这就是“出队”。
【讨论】:
队列是某种双面数据结构。您可以在一侧添加新元素,并从另一侧删除元素(与只有一侧的堆栈相反)。入队意味着添加一个元素,出队意味着删除一个元素。请看here。
【讨论】:
Enqueue表示添加一个元素,dequeue表示删除一个元素。
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();
}
【讨论】:
在我看来,这是描述该过程的最糟糕的选择词之一,因为它与现实生活中的任何事物或类似事物无关。一般来说,“queue”这个词很糟糕,好像发音一样,听起来像英文字符“q”。看到这里的低效率了吗?
enqueue: 将某物放入队列;将元素添加到队列的尾部;
dequeue 将某物从队列中取出;从队列头部移除第一个可用元素
【讨论】: