队列

1 简介

概念

  • 队列可以定义为有序列表。在一端执行插入操作rear,删除操作在另一端执行,称为front。
  • 队列被称为先进先出列表。

应用

  • 单个共享资源(如打印机,磁盘,CPU)的等待列表。
  • 异步数据传输。管道,文件IO,套接字。
  • 缓冲区.
  • 操作系统中处理中断。

时间复杂度

时间复杂性 访问 搜索 插入 删除
平均情况 θ(n) θ(n) θ(1) θ(1)
最坏情况 θ(n) θ(n) θ(1) θ(1)

2 队列的操作

基本操作

  • 创建
  • 遍历(显示第一个元素)
  • 插入
  • 删除

3 队列的实现

队列的数组实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>   
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main()
{
int choice;
while (choice != 4)
{
printf("*************************Main Menu*****************************\n");
printf("=================================================================\n");
printf("1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("Enter your choice ?");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Enter valid choice??\n");
}
}
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d", &item);
if (rear == maxsize - 1)
{
printf("OVERFLOW\n");
return;
}
if (front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear + 1;
}
queue[rear] = item;
printf("Value inserted ");

}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("UNDERFLOW\n");
return;

}
else
{
item = queue[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
front = front + 1;
}
printf("value deleted ");
}


}

void display()
{
int i;
if (rear == -1)
{
printf("Empty queue\n");
}
else
{
printf("printing values .....\n");
for (i = front;i <= rear;i++)
{
printf("\n%d\n", queue[i]);
}
}
}

队列的链表实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<stdio.h>   
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main()
{
int choice;
while (choice != 4)
{
printf("*************************Main Menu*****************************\n");
printf("=================================================================\n");
printf("1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("Enter your choice ?");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Enter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("OVERFLOW\n");
return;
}
else
{
printf("Enter value?\n");
scanf("%d", &item);
ptr->data = item;
if (front == NULL)
{
front = ptr;
rear = ptr;
front->next = NULL;
rear->next = NULL;
}
else
{
rear->next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if (front == NULL)
{
printf("UNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front->next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if (front == NULL)
{
printf("Empty queue\n");
}
else
{
printf("printing values .....\n");
while (ptr != NULL)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
}

C++模板

1
2
3
#include<queue>

queue<int> que;