在bool end()函数中,程序将知道标记是开始还是结束?有没有我可以检查的结果,以确保它正在读取哨兵作为结束?循环双链表结束函数
#include "ring.h"
#include <stdlib.h>
#include <stdio.h>
struct node {
int item;
struct node *prev;
struct node *next;
};
typedef struct node node;
struct ring {
node *sentinel;
node *current;
};
ring *new_ring() {
ring *p;
node *n;
p = (ring *) malloc (sizeof(ring));
n = malloc(sizeof(node));
p->sentinel = n;
n->next = n;
n->prev = n;
return p;
}
void start(ring *r) {
r->current = r->sentinel;
}
bool end(ring *r) {
return r->current == r->sentinel;
}
void forward(ring *r) {
while (r->current != r->sentinel) {
r->current = r->current->next;
}
}
请详细描述圆形列表应该如何工作。哨兵的角色究竟是什么? – Codor
其目的是为了让第一个和最后一个节点指向它,以便在任何地方都没有NULL指针,因此不需要对NULL进行特殊测试。当列表为空时,它只包含前哨节点,前哨节点向前和向后指向自身。 – user5647965
由于您使用* bool *,因此您需要包含* stdbool.h *。 –