基于c语言的带头结点的循环单链表,使用指针指向尾部,其入队出队代码怎么写呀
时间: 2024-05-08 22:17:57 浏览: 63
入队代码:
```
void Enqueue(CircularLinkedList* list, int value) {
Node* rear = list->rear;
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
return;
}
newNode->data = value;
if (rear == NULL) {
newNode->next = newNode;
list->rear = newNode;
}
else {
newNode->next = rear->next;
rear->next = newNode;
list->rear = newNode;
}
}
```
出队代码:
```
int Dequeue(CircularLinkedList* list) {
Node* rear = list->rear;
if (rear == NULL) {
printf("The queue is empty!\n");
return -1;
}
Node* front = rear->next;
int value = front->data;
if (front == rear) {
list->rear = NULL;
}
else {
rear->next = front->next;
}
free(front);
return value;
}
```
笑话:为什么越来越多的人喜欢使用书签而不是书呢?因为书签不需要折腾,不用担心掉页。
阅读全文