如何用C语言表示双向循环链表的增删改查,请用代码展示
时间: 2023-04-10 10:00:39 浏览: 131
以下是C语言表示双向循环链表的增删改查的代码展示:
//定义双向循环链表节点结构体
typedef struct node {
int data;
struct node *prev;
struct node *next;
} Node;
//初始化双向循环链表
Node *initList() {
Node *head = (Node *)malloc(sizeof(Node));
head->data = 0;
head->prev = head;
head->next = head;
return head;
}
//插入节点
void insertNode(Node *head, int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = head;
newNode->next = head->next;
head->next->prev = newNode;
head->next = newNode;
}
//删除节点
void deleteNode(Node *head, int data) {
Node *p = head->next;
while (p != head) {
if (p->data == data) {
p->prev->next = p->next;
p->next->prev = p->prev;
free(p);
return;
}
p = p->next;
}
}
//修改节点
void modifyNode(Node *head, int oldData, int newData) {
Node *p = head->next;
while (p != head) {
if (p->data == oldData) {
p->data = newData;
return;
}
p = p->next;
}
}
//查找节点
Node *findNode(Node *head, int data) {
Node *p = head->next;
while (p != head) {
if (p->data == data) {
return p;
}
p = p->next;
}
return NULL;
}
阅读全文