circular queue linked list c++ main()
时间: 2024-05-12 07:20:09 浏览: 110
As an AI language model, I am not capable of providing you with a complete implementation of circular queue using linked list in C language. However, I can provide you with the main() function of this program as an example:
```
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Queue {
struct Node *front, *rear;
};
void enqueue(struct Queue *q, int data) {
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
if (q->front == NULL) {
q->front = temp;
} else {
q->rear->next = temp;
}
q->rear = temp;
q->rear->next = q->front;
}
int dequeue(struct Queue *q) {
if (q->front == NULL) {
printf("Queue is empty.\n");
return -1;
} else if (q->front == q->rear) {
int data = q->front->data;
free(q->front);
q->front = NULL;
q->rear = NULL;
return data;
} else {
int data = q->front->data;
struct Node *temp = q->front;
q->front = q->front->next;
q->rear->next = q->front;
free(temp);
return data;
}
}
void display(struct Queue *q) {
struct Node *temp = q->front;
printf("Queue: ");
if (q->front == NULL) {
printf("empty\n");
} else {
while (temp->next != q->front) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("%d ", temp->data);
printf("\n");
}
}
int main() {
struct Queue q;
q.front = NULL;
q.rear = NULL;
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50);
display(&q);
int data = dequeue(&q);
printf("Dequeued element: %d\n", data);
display(&q);
enqueue(&q, 60);
enqueue(&q, 70);
display(&q);
return 0;
}
```
阅读全文