在C语言中,可以通过以下步骤来查找双向链表中的节点名称并删除节点:
typedef struct Node {
char name[50];
struct Node* prev;
struct Node* next;
} Node;
Node* head = NULL;
Node* tail = NULL;
void insertNode(char name[]) {
Node* newNode = (Node*)malloc(sizeof(Node));
strcpy(newNode->name, name);
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
Node* findNode(char name[]) {
Node* current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
return current;
}
current = current->next;
}
return NULL;
}
void deleteNode(char name[]) {
Node* nodeToDelete = findNode(name);
if (nodeToDelete == NULL) {
printf("Node not found.\n");
return;
}
if (nodeToDelete == head) {
head = nodeToDelete->next;
} else {
nodeToDelete->prev->next = nodeToDelete->next;
}
if (nodeToDelete == tail) {
tail = nodeToDelete->prev;
} else {
nodeToDelete->next->prev = nodeToDelete->prev;
}
free(nodeToDelete);
}
通过调用上述函数,可以实现在C语言中查找双向链表中的节点名称并删除节点的功能。请注意,这只是一个简单的示例,实际应用中可能需要考虑更多的边界情况和错误处理。
云+社区技术沙龙[第6期]
GAME-TECH
Techo Day
DB TALK 技术分享会
Elastic 中国开发者大会
serverless days
腾讯技术开放日
DBTalk技术分享会
高校开发者
腾讯云GAME-TECH沙龙
领取专属 10元无门槛券
手把手带您无忧上云