数据结构与算法中使用C语言过程描述:22,10,3,100先后进入队列,然后22出队。 一、使用顺序队列实现上述数据进队和出队的操作,每次进队和出队操作后需要遍历队列。 二、使用链式队列实现上述数据进队和出队的操作,每次进队和出队操作后需要遍历队列。 三、使用循环队列实现上述数据进队和出队的操作,每次进队和出队操作后需要遍历队列。
时间: 2023-11-28 13:48:25 浏览: 109
一、使用顺序队列实现上述数据进队和出队的操作,每次进队和出队操作后需要遍历队列。
```c
#include <stdio.h>
#define MAX_SIZE 5
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
} SeqQueue;
void initQueue(SeqQueue *q) {
q->front = q->rear = 0;
}
int isQueueEmpty(SeqQueue q) {
return q.front == q.rear;
}
int isQueueFull(SeqQueue q) {
return (q.rear + 1) % MAX_SIZE == q.front;
}
void enQueue(SeqQueue *q, int x) {
if (isQueueFull(*q)) {
printf("Queue is full.\n");
return;
}
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAX_SIZE;
}
int deQueue(SeqQueue *q) {
if (isQueueEmpty(*q)) {
printf("Queue is empty.\n");
return -1;
}
int x = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
return x;
}
void traverseQueue(SeqQueue q) {
if (isQueueEmpty(q)) {
printf("Queue is empty.\n");
return;
}
printf("Queue: ");
int i = q.front;
while (i != q.rear) {
printf("%d ", q.data[i]);
i = (i + 1) % MAX_SIZE;
}
printf("\n");
}
int main() {
SeqQueue q;
initQueue(&q);
enQueue(&q, 22);
enQueue(&q, 10);
enQueue(&q, 3);
enQueue(&q, 100);
traverseQueue(q);
int x = deQueue(&q);
printf("Dequeued element: %d\n", x);
traverseQueue(q);
return 0;
}
```
输出结果为:
```
Queue: 22 10 3 100
Dequeued element: 22
Queue: 10 3 100
```
二、使用链式队列实现上述数据进队和出队的操作,每次进队和出队操作后需要遍历队列。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} LinkNode;
typedef struct {
LinkNode *front;
LinkNode *rear;
} LinkQueue;
void initQueue(LinkQueue *q) {
q->front = q->rear = (LinkNode *)malloc(sizeof(LinkNode));
if (!q->front) {
printf("Memory allocation failed.\n");
exit(1);
}
q->front->next = NULL;
}
int isQueueEmpty(LinkQueue q) {
return q.front == q.rear;
}
void enQueue(LinkQueue *q, int x) {
LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
if (!p) {
printf("Memory allocation failed.\n");
exit(1);
}
p->data = x;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}
int deQueue(LinkQueue *q) {
if (isQueueEmpty(*q)) {
printf("Queue is empty.\n");
return -1;
}
LinkNode *p = q->front->next;
int x = p->data;
q->front->next = p->next;
if (q->rear == p) {
q->rear = q->front;
}
free(p);
return x;
}
void traverseQueue(LinkQueue q) {
if (isQueueEmpty(q)) {
printf("Queue is empty.\n");
return;
}
printf("Queue: ");
LinkNode *p = q.front->next;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
LinkQueue q;
initQueue(&q);
enQueue(&q, 22);
enQueue(&q, 10);
enQueue(&q, 3);
enQueue(&q, 100);
traverseQueue(q);
int x = deQueue(&q);
printf("Dequeued element: %d\n", x);
traverseQueue(q);
return 0;
}
```
输出结果为:
```
Queue: 22 10 3 100
Dequeued element: 22
Queue: 10 3 100
```
三、使用循环队列实现上述数据进队和出队的操作,每次进队和出队操作后需要遍历队列。
```c
#include <stdio.h>
#define MAX_SIZE 5
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
} CircleQueue;
void initQueue(CircleQueue *q) {
q->front = q->rear = 0;
}
int isQueueEmpty(CircleQueue q) {
return q.front == q.rear;
}
int isQueueFull(CircleQueue q) {
return (q.rear + 1) % MAX_SIZE == q.front;
}
void enQueue(CircleQueue *q, int x) {
if (isQueueFull(*q)) {
printf("Queue is full.\n");
return;
}
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAX_SIZE;
}
int deQueue(CircleQueue *q) {
if (isQueueEmpty(*q)) {
printf("Queue is empty.\n");
return -1;
}
int x = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
return x;
}
void traverseQueue(CircleQueue q) {
if (isQueueEmpty(q)) {
printf("Queue is empty.\n");
return;
}
printf("Queue: ");
int i = q.front;
while (i != q.rear) {
printf("%d ", q.data[i]);
i = (i + 1) % MAX_SIZE;
}
printf("\n");
}
int main() {
CircleQueue q;
initQueue(&q);
enQueue(&q, 22);
enQueue(&q, 10);
enQueue(&q, 3);
enQueue(&q, 100);
traverseQueue(q);
int x = deQueue(&q);
printf("Dequeued element: %d\n", x);
traverseQueue(q);
return 0;
}
```
输出结果为:
```
Queue: 22 10 3 100
Dequeued element: 22
Queue: 10 3 100
```
阅读全文