C语言中队列的插入元素是什么?
数据结构是按结构化方式组织的数据集合。它分为如下两种类型:
-
线性数据结构 − 数据以线性方式组织。例如,数组、结构体、栈、队列、链表。
-
非线性数据结构 − 数据以分层方式组织。例如,树、图、集合、表。
另请阅读:数据结构和类型
队列
队列是一种线性数据结构,其中插入在队尾进行,删除在队首进行。

队列的顺序是FIFO – 先进先出
操作
- 插入 – 将元素插入队列。
- 删除 – 从队列中删除元素。
条件
-
队列溢出 − 尝试将元素插入已满的队列。
-
队列下溢 − 尝试从空队列中删除元素。
算法
以下是插入()算法:
- 检查队列溢出。
if (r==n)
printf ("Queue overflow")
- 否则,将元素插入队列。
q[r] = item r++
C程序用于插入队列元素
以下是用于插入队列元素的C程序:
#include <stdio.h>
#define MAX 50
void insert();
int array[MAX];
int rear = -1;
int front = -1;
main() {
int add_item;
int choice;
while (1) {
printf("1.Insert element to queue
");
printf("2.Display elements of queue
");
printf("3.Quit
");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
display();
break;
case 3:
exit(1);
default:
printf("Wrong choice
");
}
}
}
void insert() {
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow
");
else {
if (front == -1) /*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
array[rear] = add_item;
}
}
void display() {
int i;
if (front == -1)
printf("Queue is empty
");
else {
printf("Queue is :
");
for (i = front; i <= rear; i++) printf("%d ", array[i]);
printf("
");
}
}
输出
执行上述程序后,将产生以下结果:
1.Insert element to queue 2.Display elements of queue 3.Quit Enter your choice: 1 Inset the element in queue: 34 1.Insert element to queue 2.Display elements of queue 3.Quit Enter your choice: 1 Inset the element in queue: 24 1.Insert element to queue 2.Display elements of queue 3.Quit Enter your choice: 2 Queue is: 34 24 1.Insert element to queue 2.Display elements of queue 3.Quit Enter your choice: 3
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP