概念
队列的基本操作
- 入队EnterQueue:在队列的队尾插入一个元素
- 出队DeleteQueue:将队列的队头元素出队
- 判空IsEmpty:判断队列是否有元素存在
- 判满IsFull:判断队列是否已满
- front:返回队头元素
数组实现队列
顺序队列
- 一开始令front和rear等于-1,表示队空。入队第一个元素时front和rear都赋值为0,然后每入队一个元素rear+1,每出队一个元素front+1,当front等于rear时队空两个整数重新赋值为-1。
#include <stdio.h>
#define MAX_SIZE 8
struct queue
{
int front;
int rear;
int data[MAX_SIZE];
};
int IsEmpty(struct queue *p)
{
if (p->front == -1 && p->rear == -1)
{
return 0;
}
else
{
return 1;
}
}
int IsFull(struct queue *p)
{
if (p->rear == MAX_SIZE - 1)
{
return 0;
}
else
{
return 1;
}
}
int EnterQueue(struct queue *p, int num)
{
if (!IsFull(p))
{
printf("队列已满\n");
return 0;
}
else if (!IsEmpty(p))
{
p->front = 0;
p->rear = 0;
}
else
{
p->rear++;
}
p->data[p->rear] = num;
}
int DeleteQueue(struct queue *p)
{
if (!IsEmpty(p))
{
printf("队列已空\n");
return 0;
}
else if (p->front == p->rear)
{
p->rear = -1;
p->front = -1;
return 0;
}
else
{
p->front++;
}
}
void front(struct queue *p)
{
printf("队首元素:%d\n", p->data[p->front]);
}
void printQ(struct queue *p)
{
printf("\n^front|\n");
int i = p->front;
while (i <= p->rear)
{
printf("%d\n", p->data[i++]);
}
printf("^rear|\n");
}
int main()
{
int i = 0;
struct queue q = {-1, -1, 0};
for (i = 0; i < 10; i++)
{
EnterQueue(&q, 2 * i + 1);
}
printQ(&q);
DeleteQueue(&q);
DeleteQueue(&q);
printQ(&q);
return 0;
}
队列已满
队列已满
^front|
1
3
5
7
9
11
13
15
^rear|
^front|
5
7
9
11
13
15
^rear|
循环队列
#include <stdio.h>
#define MAX_SIZE 8
struct queue
{
int front;
int rear;
int n;
int data[MAX_SIZE];
};
int IsEmpty(struct queue *p)
{
if (p->front == -1 && p->rear == -1)
{
return 0;
}
else
{
return 1;
}
}
int IsFull(struct queue *p)
{
int i = p->rear;
if ((i + 1) % MAX_SIZE == p->front)
{
return 0;
}
else
{
return 1;
}
}
int EnterQueue(struct queue *p, int num)
{
if (!IsFull(p))
{
printf("队列已满\n");
return 0;
}
else if (!IsEmpty(p))
{
p->front = 0;
p->rear = 0;
}
else
{
p->rear = (p->rear + 1) % MAX_SIZE;
}
p->data[p->rear] = num;
p->n++;
}
int DeleteQueue(struct queue *p)
{
if (!IsEmpty(p))
{
printf("队列已空\n");
return 0;
}
else if (p->front == p->rear)
{
p->rear = -1;
p->front = -1;
p->n = 0;
return 0;
}
else
{
p->front = (p->front + 1) % MAX_SIZE;
p->n--;
}
}
void front(struct queue *p)
{
printf("队首元素:%d\n", p->data[p->front]);
}
int printQ(struct queue *p)
{
printf("\n^front|\n");
int i = p->front;
if (p->n == 0)
{
printf("队列已空\n");
return 0;
}
else if (i <= p->rear)
{
while (i < p->rear)
{
printf("%d\n", p->data[i]);
i++;
}
}
else
{
for (int j = 0; j < p->n; j++)
{
printf("%d\n", p->data[i]);
i = (i + 1) % MAX_SIZE;
}
}
printf("^rear|\n");
}
int main()
{
int i = 0;
struct queue q = {-1, -1, 0, 0};
for (i = 0; i < MAX_SIZE+2; i++)
{
EnterQueue(&q, i + 1);
}
printQ(&q);
DeleteQueue(&q);
DeleteQueue(&q);
DeleteQueue(&q);
printQ(&q);
EnterQueue(&q, 666);
EnterQueue(&q, 233);
printQ(&q);
return 0;
}
队列已满
队列已满
^front|
1
2
3
4
5
6
7
8
^rear|
^front|
4
5
6
7
8
^rear|
^front|
4
5
6
7
8
666
233
^rear|
链表实现队列
- 定义两个指针分别指向队头和队尾,队头即链表头。入队时使用队尾指针把新节点给尾节点的next,指针后移;出队时队头指针后移的同时释放头节点。
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node *front;
struct node *rear;
int num;
int size;
};
int IsEmpty(struct queue *p)
{
if (p->front == NULL && p->rear == NULL)
{
return 0;
}
else
{
return 1;
}
}
int IsFull(struct queue *p)
{
if (p->num == p->size)
{
return 0;
}
else
{
return 1;
}
}
int EnterQueue(struct queue *p)
{
if (!IsFull(p))
{
printf("队列已满\n");
return 0;
}
else if (!IsEmpty(p))
{
p->rear = (struct node *)malloc(sizeof(struct node));
p->front = p->rear;
p->rear->next = NULL;
p->num++;
printf("请输入第1个节点数据:\n");
scanf("%d", &(p->rear->data));
return 0;
}
else
{
p->rear->next = (struct node *)malloc(sizeof(struct node));
p->rear->next->next = NULL;
p->rear = p->rear->next;
p->num++;
printf("请输入第%d个节点数据:\n", p->num);
scanf("%d", &(p->rear->data));
return 0;
}
}
int DeleteQueue(struct queue *p)
{
if (!IsEmpty(p))
{
printf("队列已空\n");
return 0;
}
else if (p->front == p->rear)
{
free(p->rear);
p->rear = NULL;
p->front = NULL;
p->num = 0;
return 0;
}
else
{
struct node *temp = p->front;
p->front = p->front->next;
free(temp);
p->num--;
return 0;
}
}
int front(struct node *front)
{
if (!IsEmpty)
{
printf("队列已空\n");
return 0;
}
printf("队首元素:%d\n", front->data);
}
int printQ(struct node *front)
{
printf("\nfront:< ");
if (!IsEmpty)
{
printf("队列已空\n");
return 0;
}
while (front)
{
printf("%d ", front->data);
front = front->next;
}
printf("<rear\n");
}
int main()
{
int i = 0;
struct queue q = {NULL, NULL, 0, 8};
printf("请输入要入队的元素个数,最多入队%d个\n", q.size);
scanf("%d", &i);
while (i--)
{
EnterQueue(&q);
}
printQ(q.front);
front(q.front);
DeleteQueue(&q);
DeleteQueue(&q);
printQ(q.front);
return 0;
}
请输入要入队的元素个数,最多入队8个
9
请输入第1个节点数据:
111
请输入第2个节点数据:
222
请输入第3个节点数据:
333
请输入第4个节点数据:
444
请输入第5个节点数据:
555
请输入第6个节点数据:
666666
请输入第7个节点数据:
777
请输入第8个节点数据:
888
队列已满
front:< 111 222 333 444 555 666666 777 888 <rear
队首元素:111
front:< 333 444 555 666666 777 888 <rear
再见! ??
|