c++中的队列_C ++中的队列

c++中的队列

介绍 (Introduction)

We worked on the working as well as the implementation of a Stack in C++ in our previous tutorial. Today in this tutorial, we are going to discuss another data structure, Queue in C++.

在上一教程中 ,我们研究了C ++中Stack的工作和实现。 今天,在本教程中,我们将讨论另一种数据结构, C ++中的Queue

什么是队列? (What is a Queue?)

Basically, a queue is also a linear data structure that follows the First In First Out (FIFO) pattern in the insertion or deletion of data. As far as the pattern is concerned, the first element inserted is deleted first, and the last one to enter the queue is deleted at the last.

基本上, 队列也是线性数据结构,在插入或删除数据时遵循先进先出( FIFO )模式。 就模式而言,首先删除插入的第一个元素,最后删除进入队列的最后一个元素。

Unlike stack, Queue operations take place on both sides. But, note that one operation should be performed on one end, and the other on the other end. Hence, both insertion and deletion operations do not take place on the same side.

与堆栈不同, 队列操作在两侧进行。 但是,请注意,一个操作应在一端执行,另一端应在另一端执行。 因此,插入和删除操作不会在同一侧进行。

C ++中的队列工作 (Working of Queue in C++)

A Queue is analogous to a real-world queue. It works on a first come first serve basis. Hence as mentioned earlier, the first element to enter the queue is deleted first, and the last one to enter is deleted after all the previous members are already deleted. Now let us take a look at the basic operations that can be performed on a queue,

队列类似于现实世界中的队列。 它以先到先得的原则工作。 因此,如前所述,首先删除进入队列的第一个元素,并在所有先前成员都已被删除之后,最后一个进入队列的元素被删除。 现在让我们看一下可以在队列上执行的基本操作,

  • enqueue

    入队
  • dequeue

    出队
  • Show

    表演

In the next section, we are going to elaborate on the above-mentioned techniques.

在下一节中,我们将详细介绍上述技术。

1.队列中的enqueue() (1. enqueue() in Queue)

enqueue() does the insertion of an element into the queue. It is simply done by adding the element at the end of the queue. So, as we can infer, this operation is performed at the end.

enqueue()做的元素插入到队列中。 只需在队列末尾添加元素即可完成。 因此,我们可以推断,此操作将在最后执行

The Algorithm for enqueue is given below,

排队算法如下:


Procedure ENQUEUE(VALUE)
    If REAR==Size_of_Stack
        Write OVERFLOW
    else
        REAR=REAR+1
        QUEUE[REAR]=VALUE
    END IF
END of Procedure

2.队列中的dequeue() (2. dequeue() in Queue)

On the other hand dequeue() removes or deletes and accesses the first element present in the queue. Note, this element is the one which was inserted before all the other ones hence is the first one to get dequeued. This dequeue operation occurs at the front of the present queue. Hence, the opposite side to the one where enqueue was performed.

另一方面, dequeue()删除或删除并访问队列中存在的第一个元素。 请注意,此元素是在所有其他元素之前插入的元素,因此是第一个要出队的元素。 此出队操作发生在当前队列的前面 。 因此,与执行入队的一侧相反。

The Algorithm for dequeue is given below,

出队算法如下:


Procedure DEQUEUE()
    If FRONT==-1 OR FRONT>REAR  //If the queue is empty
        Write UNDERFLOW
    else
        FRONT=FRONT+1
        RETURN QUEUE[FRONT-1]
    END IF
END of Procedure

3.显示 (3. Show)

The show is basically the operation in which the corresponding elements of the queue is shown to the user or in other words, is printed. This allows the user to check the current status of the queue at any point in time.

表演基本上是一种操作,其中将队列的相应元素显示给用户或换句话说,进行打印 。 这使用户可以在任何时间点检查队列的当前状态

The Algorithm for show is given below,

展示算法如下:


Procedure DEQUEUE()
    If FRONT==-1 OR FRONT>REAR  //If the queue is empty
        Write UNDERFLOW
    else
        Repeat While FRONT<=REAR
            PRINT QUEUE[FRONT]
            FRONT=FRONT+1
    END IF
END of Procedure

在C ++中实现队列 (Implementation of a Queue in C++)

Now let us implement the whole concept of a Queue in C++, this will give us a clear understanding of its working.

现在让我们在C ++中实现Queue的整个概念,这将使我们对它的工作有一个清晰的了解。


#include<iostream>
using namespace std;
#define max 10                

int queue[max],front=-1,rear=-1;           //queue declaration

void enqueue(int num) //enqueue() inserts an element into the Queue
{
	if(rear==max)  //check if Queue is full
	{
		cout<<"OVERFLOW!";
	}
	else if(front==-1 && rear==-1) //For 1st insertion in Queue
	{
		front++;
		rear++;
		queue[rear]=num;
	}
	else 
	{
		rear++;
		queue[rear]=num;
	}
} 
int dequeue() //dequeue() deletes out the 1st element from the Queue
{
	if(front==-1 || front>rear)  //check if Queue is empty
	{
		cout<<"UNDERFLOW!";
		return -1;
	}
	else
	{
		cout<<"The deleted data is : "<<queue[front++];   //printing the deleted element
		return queue[front-1];
	}
}

void show()
{
	int i=front;
	if(front==-1 || front>rear)    //if Queue is empty
	{
		cout<<"UNDERFLOW!";
	}
	else
	{
		while(i<=rear) //printing the current Queue elements
		{
			cout<<"\t"<<queue[i];
			i++;
		}
		cout<<endl;
	}
}
int main() 
{ 
    int ch,val;
    cout<<"   :::MENU:::";   //Menu for Queue operations
    cout<<"\n1.enqueue\n2.dequeue\n3.Show\n4.Exit";
    while(1)    
    {
        printf("\nEnter the choice:");
        scanf("%d",&ch);
         
        switch(ch)
        {
            case 1: cout<<"Enter the value to be pushed: ";
            		cin>>val;
            		enqueue(val);
                    break;
            case 2: dequeue();
                    break;
            case 3: cout<<"Stack : ";
					show();
                    break;
            case 4: exit(0);
             
            default: printf("\nError! Invalid choice!...");
        }
    }
    return 0;
}

Output:

输出

Queue Output
Queue in C++ Output
在C ++输出中排队

Understanding the code,

了解代码,

  • Firstly have initially declared two variables, front=-1 and rear=-1, to signify the queue is empty. The size of the queue is stored inside the constant max

    首先,首先声明了两个变量front = -1Rear = -1 ,以表明队列为 。 队列的大小存储在常量max
  • enqueue() – as the name suggests, we use this function to perform the enqueue operation. It adds the passed value to the queue at the rear position

    enqueue() –顾名思义,我们使用此函数执行入队操作。 它将传递的值添加到后面位置的队列中
  • dequeue() – similarly is used to remove or access the front element from the queue. This function returns the first value removed from the queue

    dequeue() –类似地用于从队列中删除或访问前面的元素。 此函数返回从队列中删除的第一个值
  • show() – This function prints the whole queue, all of its elements. Starting from the front up to the rear.

    show() –此函数打印整个队列及其所有元素。 从前到后。

As we saw in our stack tutorial, the repetitive checking the top was important to avoid errors. Similarly in case of a queue too, we need to check whether the given queue is full or empty and print OVERFLOW and UNDERFLOW errors respectively.

正如我们在堆栈教程中所看到的,重复检查顶部对于避免错误很重要。 同样在队列的情况下,我们需要检查给定队列是否已满或为空,并分别打印OVERFLOWUNDERFLOW错误。

应用领域 (Applications)

Queues find application in various programming methods and also are used to solve many real-time problems. Some of them are,

队列可以以各种编程方法找到应用,也可以用来解决许多实时问题。 他们之中有一些是,

  1. CPU sharing

    CPU共享
  2. Breadth-First Search algorithm

    广度优先搜索算法
  3. Waiting lists

    等待名单
  4. IO buffers,

    IO缓冲区
  5. File IO

    文件IO
  6. Keyboard Buffer

    键盘缓冲
  7. And many more…

    还有很多…

结论 (Conclusion)

In this tutorial, we have discussed the concept of a queue in C++ as well as the different operations that we perform on it. We have also taken the various applications of a Queue into consideration.

在本教程中,我们讨论了C ++中队列的概念以及我们对其执行的不同操作。 我们还考虑了队列的各种应用。

参考资料 (References)

翻译自: https://www.journaldev.com/35394/queue-in-c-plus-plus

c++中的队列

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值