从队列中删除特定值 [英] Delete specific values from the queue
问题描述
您好,基本上我在这里写小东西,我正在使用链接列表实现队列,一切都处于高峰期但有一点我无法理解哪个是我出列了并且表现不错但是如果我希望我也会提供要删除的值!这就是我所做的!!
我尝试过:
Hello, basically i am writing small stuff here and i am implementing queue using linked list and everything is at its peak but there is little thing that i cant understand which is that i am dequeueing and it is performing good but what if i want like i will also provide number of values to be deleted !! here is what i have done !!
What I have tried:
#include<iostream>
#include<cstdlib>
using namespace std;
struct node{
int info;
struct node *next;
};
class Queue{
private:
node *rear;
node *front;
public:
Queue();
void enqueue();
void dequeue();
void display();
};
Queue::Queue(){
rear = NULL;
front = NULL;
}
void Queue::dequeue(){
node *temp = new node;
if(front == NULL){
cout<<"\nQueue is Emtpty\n";
}else{
temp = front;
front = front->next;
cout<<"The data Dequeued is "<<temp->info;
delete temp;
}
}
int main(){
Queue queue;
queue.dequeue();
return 0;
}
推荐答案
如果要将特定数量的值出列,可以使用循环:
If you want to dequeue a specific number of values, you can use a loop:
int count_to_dequeue = 3; // choose a number
while (count_to_dequeue > 0) {
queue.dequeue();
count_to_dequeue--;
}
如何工作:首先,初始化变量 count_to_dequeue
。在示例中,它是 3
,以使三个值出列。然后有一个while循环。这意味着当count_to_dequeue大于零时,执行块中的代码。在块中,一个值出列并且count_to_dequeue减少一个。
运行时发生的所有步骤的完整列表:
How this works: first, you initialize a variable count_to_dequeue
. In the example, it's 3
, to dequeue three values. Then there is a while loop. It means "while count_to_dequeue is greater than zero, execute the code in the block". In the block, a value is dequeued and count_to_dequeue is decreased by one.
Full list of all steps that happen on runtime:
- count_to_dequeue设置为3.
- (进入循环时)
- 值已出列。
- count_to_dequeue减少 - >它的值为2.
- 一个值出列。
- count_to_dequeue减少 - >它的值为1.
- 一个值出列。
- count_to_dequeue减少 - >它的值为0.
- (while循环存在:count_to_dequeue不再大于零)
这篇关于从队列中删除特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!