创建队列 c语言

A queue in C is basically a linear data structure to store and manipulate the data elements. It follows the order of First In First Out (FIFO).

C语言中的队列基本上是用于存储和操作数据元素的linear data structure 。 它遵循先进先出(FIFO)的顺序。

In queues, the first element entered into the array is the first element to be removed from the array.

在队列中,输入到数组中的第一个元素是要从数组中删除的第一个元素。

For example, let’s consider the scenario of a bus-ticket booking stall. Here, the fashion of a C programming queue is followed. The tickets are distributed on the first-come-first-serve basis i.e. the first one to enter is the first one to be served with the tickets.

例如,让我们考虑公交车票预订摊位的情况。 在此,遵循C编程队列的方式。 门票将按照先到先得的原则分配,即,第一个进入的门票将是第一个与门票一起出售的门票。

A queue is open at both ends. One end is provided for the insertion of data and the other end for the deletion of data.

两端都有一个队列 。 提供一端用于插入数据,另一端用于删除数据。

A queue can be implemented with any programming language such as C, Java, Python, etc.

可以使用任何编程语言(例如C,Java,Python等)来实现队列。



与C中的队列相关联的操作 (Operations Associated with a Queue in C )

A queue being an Abstract Data Structure provides the following operations for manipulation on the data elements:

作为抽象数据结构的队列为数据元素提供了以下操作:

  • isEmpty(): To check if the queue is emptyisEmpty() :检查队列是否为空
  • isFull(): To check whether the queue is full or notisFull() :检查队列是否已满
  • dequeue(): Removes the element from the frontal side of the queuedequeue() :从队列的正面移除元素
  • enqueue(): It inserts elements to the end of the queueenqueue() :将元素插入队列的末尾
  • Front: Pointer element responsible for fetching the first element from the queueFront :负责从队列中获取第一个元素的指针元素
  • Rear: Pointer element responsible for fetching the last element from the queueRear :负责从队列中获取最后一个元素的指针元素


队列数据结构的工作 (Working of Queue Data Structure )

Queue follows the First-In-First-Out pattern. The first element is the first to be pulled out from the list of elements.

队列遵循先进先出模式。 第一个元素是第一个从元素列表中拉出的元素。

  • Front and Rear pointers keep the record of the first and last element in the queue.FrontRear指针保持在队列中的第一个和最后一个元素的记录。
  • At first, we need to initialize the queue by setting Front = -1 and Rear = -1首先,我们需要通过设置Front = -1Rear = -1来初始化队列。
  • In order to insert the element (enqueue), we need to check whether the queue is already full i.e. check the condition for Overflow. If the queue is not full, we’ll have to increment the value of the Rear index by 1 and place the element at the position of the Rear pointer variable. When we get to insert the first element in the queue, we need to set the value of Front to 0.为了插入元素( enqueue ),我们需要检查队列是否已满,即检查Overflow的条件 。 如果队列未满,则必须将Rear索引的值增加1并将元素放置在Rear指针变量的位置。 当我们将第一个元素插入队列时,我们需要将Front的值设置为0。
  • In order to remove the element (dequeue) from the queue, we need to check whether the queue is already empty i.e. check the condition for Underflow. If the queue is not empty, we’ll have to remove and return the element at the position of the Front pointer, and then increment the Front index value by 1. When we get to remove the last element from the queue, we will have to set the values of the Front and Rear index to -1. 为了从队列中删除元素( 出队 ),我们需要检查队列是否已经为空,即检查Underflow的条件 。 如果队列不为空,则必须删除并返回Front指针位置的元素,然后将Front索引值增加1。当我们从队列中删除最后一个元素时 ,我们将将前和后索引的值设置为-1。


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

Queues in C can be implemented using Arrays, Lists, Structures, etc. Below here we have implemented queues using Arrays in C.

C中的队列可以使用数组,列表,结构等实现。下面,我们在C中使用数组实现队列。

Example:

例:


#include <stdio.h>
# define SIZE 100
void enqueue();
void dequeue();
void show();
int inp_arr[SIZE];
int Rear = - 1;
int Front = - 1;
main()
{int ch;while (1){printf("1.Enqueue Operation\n");printf("2.Dequeue Operation\n");printf("3.Display the Queue\n");printf("4.Exit\n");printf("Enter your choice of operations : ");scanf("%d", &ch);switch (ch){case 1:enqueue();break;case 2:dequeue();break;case 3:show();break;case 4:exit(0);default:printf("Incorrect choice \n");} }
} void enqueue()
{int insert_item;if (Rear == SIZE - 1)printf("Overflow \n");else{if (Front == - 1)Front = 0;printf("Element to be inserted in the Queue\n : ");scanf("%d", &insert_item);Rear = Rear + 1;inp_arr[Rear] = insert_item;}
} void dequeue()
{if (Front == - 1 || Front > Rear){printf("Underflow \n");return ;}else{printf("Element deleted from the Queue: %d\n", inp_arr[Front]);Front = Front + 1;}
} void show()
{if (Front == - 1)printf("Empty Queue \n");else{printf("Queue: \n");for (int i = Front; i <= Rear; i++)printf("%d ", inp_arr[i]);printf("\n");}
}

Output:

输出:


1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 1
Element to be inserted in the Queue: 101.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 1
Element to be inserted in the Queue: 201.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 3
Queue:
10 20 1.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations : 2
Element deleted from the Queue: 101.Enqueue Operation
2.Dequeue Operation
3.Display the Queue
4.Exit
Enter your choice of operations: 3
Queue:
20


使用堆栈实现队列 (Implementation of Queue using Stacks)

Stack Data Structure can be used to implement the operations of the queue. We’ll need two stacks to implement a queue using them. Before you work through the examples below, make sure you understand the functioning of stacks very well.

堆栈数据结构可用于实现队列的操作。 我们将需要两个堆栈以使用它们来实现队列 。 在完成下面的示例之前,请确保您非常了解堆栈的功能。

A queue can be implemented using Stacks by either of the following ways:

可以通过以下两种方式之一使用Stacks实现队列:

  • Making the enqueue operation costly使入队操作成本高昂
  • Making the dequeue operation costly使出队操作昂贵

方法1:使入队操作变得昂贵 (Method 1: Making the enqueue Operation Costly)

Let us assume two stacks: S1 and S2 to implement queue operations using the same.

让我们假设两个堆栈:S1和S2,以使用它们来实现队列操作。

  • If S1 is not empty, push all elements to stack 2 (S2)如果S1不为空,则将所有元素推入堆栈2(S2)
  • In order to perform the enqueue operation, we will assume ‘x’ to be the element to be entered into the queue. We will push ‘x’ back to Stack S1 so it comes up on the top.为了执行入队操作 ,我们将假定“ x”为要输入队列的元素。 我们将'x'推回堆栈S1,使其出现在顶部。
  • Further, push all the elements of stack S2 back to Stack S1此外,将堆栈S2的所有元素推回堆栈S1

Note: The time complexity of the enqueue operation would be O(n).

注意:入队操作的时间复杂度为O(n)

  • In order to perform dequeue operation, we’ll need to pop an item from the Stack S1 since now the first inserted element is on the top in S1 instead of being at the bottom.为了执行出队操作 ,我们需要从堆栈S1中弹出一个项目,因为现在第一个插入的元素在S1中位于顶部,而不是位于底部。

Note: The time complexity of the dequeue operation would be O(1).

注意:出队操作的时间复杂度为O(1)

If you analyze the time complexities of the Enqueue and Dequeue operations using Stack, you’ll find out that the Enqueue operation is much costlier than the Dequeue operation.

如果使用堆栈分析Enqueue和Dequeue操作的时间复杂度,您会发现Enqueue操作比Dequeue操作要昂贵得多。

Thus, as mentioned above, we make the first entered or the oldest entered element to remain at the top of Stack S1 so that it gets removed when the Dequeue operation is invoked.

因此,如上所述,我们使第一个输入或最早输入的元素保留在堆栈S1的顶部,以便在调用出队操作时将其删除。



方法2:使出队操作变得昂贵 (Method 2: Making the Dequeue operation costly)

Let us again assume two Stacks: S1 and S2 to implement queue operations using the same.

让我们再次假设两个堆栈:S1和S2,使用它们来实现队列操作。

  • In order to implement enqueue operation, we insert the newly entered element at the top of Stack S1. Thus, the time complexity of the Enqueue operation using Stacks becomes O(1).为了实现入队操作 ,我们将新输入的元素插入堆栈S1的顶部。 因此,使用堆栈的入队操作的时间复杂度变为O(1)。
  • For the implementation of dequeue operation, it checks for the Underflow condition of Stack S1 and S2. If both the Stacks stands out to be empty, an error would be raised.为了实现出队操作 ,它检查堆栈S1和S2的下溢条件。 如果两个堆栈都突出显示为空,则会引发错误。
  • If the Stack S2 is empty and S1 is not empty, then all the elements from Stack S1 are moved to Stack S2 and the top element of Stack S2 is popped out and returned out of the Dequeue operation.如果堆栈S2为空且S1不为空,则将堆栈S1中的所有元素移至堆栈S2,并弹出堆栈S2的顶部元素,并将其从出队操作中移出。
  • Thus, the time complexity of the dequeue operation using Stacks becomes O(n).因此, 使用堆栈的出队操作的时间复杂度变为O(n)


队列数据结构的应用 (Applications of Queue Data Structure)

  • CPU SchedulingCPU调度
  • Disk Scheduling磁盘调度
  • Asynchronous data transfer between processors such as File IO, etc.处理器之间的异步数据传输,例如文件IO等。
  • Breadth-First Search Algorithm (BFS)广度优先搜索算法(BFS)


结论 (Conclusion)

In this article, we have understood the working of queue data structure and have also shadowed over the implementation of queues using stack data structure.

在本文中,我们了解了队列数据结构的工作原理,并且对使用堆栈数据结构的队列实现也有所了解。



参考资料 (References)

  • Queue in C在C中排队
  • Queue using Stacks使用堆栈排队

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

创建队列 c语言

创建队列 c语言_在C中创建队列相关推荐

  1. java 创建动态int数组_在Scala中创建动态增长数组的最佳方法是什么?

    如果要使用不可变结构,可以使用以下方法: scala> val orgList = List(1,2,3) orgList: List[Int] = List(1, 2, 3) scala> ...

  2. 如何创建多个条形图_在R中创建条形图

    如何创建多个条形图 Bar plots in R are the most frequently used plots in elementary statistics. These consist ...

  3. 创建视图SQL:在SQL Server中创建视图

    介绍 (Introduction) In this article, we are going to see how to use the CREATE VIEW SQL statement to c ...

  4. 在idea中创建mybatis-config.xml模板(在idea中创建mybatis核心配置文件模板)

    在idea中创建mybatis-config.xml模板(在idea中创建mybatis核心配置文件模板) 1.写配置文件 2.设置 3.查看 1.写配置文件 先创建一个mybatis-config. ...

  5. a.创建动物类Animal,在该类中创建一个成员方法cry(), 输出“动物会发出叫声”,以及一个eat()方法,输出“动物需要食物”; b.创建一个Animal子类Dog类,在该类中重写父类的成员

    创建Zoo类作为主类,在main方法中分别创建各个类对象 ,并调用各自类的cry()方法, 创建Dog类的对象赋值给Animal类的对象,然后调用cry()和eat()方法. ** a.创建动物类An ...

  6. 如何建立队列c语言_什么是优先队列

    前言 我们之前已经介绍过队列-C语言实现,它们是先入先出的,这很容易用平常的排队来理解.但是如果这个队列要支持有紧急情况的人先出队呢?原先那种队列就不再适用了,我们需要使用本文所提到的特殊队列--优先 ...

  7. excel箱形图中位数_在Excel中创建简单的箱形图

    excel箱形图中位数 A box plot (box and whisker chart) lets you show how numbers are distributed in a set of ...

  8. swift 自定义滑动视图_在Swift中创建一个向上滑动菜单视图(以编程方式)

    swift 自定义滑动视图 This is a quick tutorial on how to create a slide-up menu view in iOS 这是有关如何在iOS中创建向上滑 ...

  9. c++ enum 给定类型_在 Rust 中创建 C/C++ API

    Rust 是一种神奇的语言,有着更好的生态系统.许多 Rust 的设计决策都非常适合向现有的C/C++系统添加新功能,或者逐步替换这些系统的部分! 当我尝试为 Rust 创建 C++ API 时,我发 ...

最新文章

  1. 全球及中国皮肤晒黑喷雾行业销售模式及动态盈利分析报告2021年版
  2. python3.6入门到高阶(全栈) day02 while循环 运算符 格式化输出 编码
  3. jqueryppt_jquery简单实现幻灯片的方法
  4. 点击链接,执行.py脚本,cgi脚本,浏览器中没有显示解析后的web页面,而是.py文件本身的代码内容...
  5. 织梦在线报名平台php,DedeCMSv5
  6. html代码style图片width,HTML Style columnWidth用法及代码示例
  7. 大数据基础系列 1:Windows 安装 VMware Workstation 虚拟机完整步骤及需要注意的问题
  8. Android 系统(229)---OTA
  9. 乐视网:公司董事、总经理、财务总监张巍因个人原因辞职
  10. github 快速配置简易教程笔记
  11. 解除用户锁定、修改用户密码
  12. 不混淆 android jni,JNI 防混淆 Android proguard
  13. mysql with roll up_GROUP BY...WITH ROLL UP 分组统计后的再合计
  14. 产品能力提升|《金字塔原理》
  15. Xcode打包IPA包
  16. 添加网络位置(共享目录)
  17. 解读Tilera怪兽级64核处理器(转)
  18. FT232RL FTDIUSB转串口芯片SSOP28 国产替代
  19. 非常实用的织梦dede所有标签调用方法大全
  20. 中瀛手机销售软件 v5.0 官方

热门文章

  1. Tasker to stop Poweramp control for the headset while there is an incoming SMS - frozen
  2. VC++网络安全编程范例(2)-创建自签名证书
  3. 背景图片,颜色变化脚本
  4. 对DataGridView中的DataGridViewComboBoxColumn有了一点点体会
  5. [转载] C++子字符串查找及提取
  6. [转载] python笔记
  7. Hibernate中的3中状态
  8. 实训|第七天横扫Linux磁盘分区、软件安装障碍附制作软件仓库
  9. 文件的I/O c++
  10. C/C++基础一:stack heap