删除链接列表中的节点 [英] Deleting Nodes in a Linked List

查看:69
本文介绍了删除链接列表中的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量的努力,我设法拼凑了一个函数,该函数从我的链表中删除了一些节点.但是,出于纯粹的兴趣,我想了解如何从列表中删除第一个节点,即头部.

After a lot of effort, I've managed to piece together a function that deletes some node from my linked list. But, out of sheer interest, I would like to find out how you can go about deleting the first node from the list, i.e. the head.

例如,我的程序要求删除一个字母. Hello存储在列表中,用户输入H进行删除,因此列表现在是ello 在使用我的代码的那一刻,该程序显然崩溃,就好像删除了H,没有头,并且该程序也不知道在哪里查找列表.

My program asks for a letter to delete, so for example. Hello is stored in the list, the user inputs H for deletion, so that now the list is ello At the moment with my code, the program crashes obviously as if H is deleted, there is no head, and the program doesn't know where to go to look for the list.

下面是我当前的实现,非常感谢您了解如何修改此代码(我希望使其与现在保持相似)以允许头节点删除的任何线索或提示!.

Below is my current implementation, any clues or hints on how to modify this code( I would like to keep it similar to how I have) to allow Head Node Deletion would be much appreciated!.

针对以下内容

FullList DeleteNode(FullList temp, char c) {
FullList remember;
FullList ptr;
while (temp.head->c != c) {
    remember.head = temp.head;
    temp.head = temp.head->next;
}
ptr.head = temp.head->next;
free(temp.head);
remember.head->next = ptr.head;
return temp;
}

int main(void) {
FullList List;
char c, s;
List.head = NULL;

while ((c=getchar()) != '.') {
    List = addToEnd(List, c);
}
scanf(" %c", &s);
List = DeleteNode(List, s);
while (List.head != NULL) {
    printf("%c", List.head->c);
    List.head = List.head->next;
}
return 0;
}


typedef struct List {
 char c;
 struct List *next;
}List;

typedef struct {
 List *head;
 List *tail;
}FullList;

 List *insertList(char c, List *t1) {
  List *t = (List*)calloc(1, sizeof(List));
  t->c = c ;
  t->next = t1;
 return t;
 }

FullList addToEnd(FullList c, char element) {
 if (c.head == NULL) { 
    c.head = c.tail = insertList(element, NULL);
 }else {
    c.tail->next = insertList(element, NULL);
    c.tail = c.tail->next;
 }
 return c;
} 

void DeleteNode(FullList temp, char c) {
 FullList remember;
 FullList ptr;
 while (temp.head->c != c) {
    remember.head = temp.head;
    temp.head = temp.head->next;
 } 
 ptr.head = temp.head->next;
 free(temp.head);
 remember.head->next = ptr.head;
} 


int main(void) {
 FullList List;
 char c, s;
 List.head = NULL;

 while ((c=getchar()) != '.') {
    List = addToEnd(List, c);
 }
 scanf(" %c", &s);
 DeleteNode(List, s);
 while (List.head != NULL) {
    printf("%c", List.head->c);
    List.head = List.head->next;
 }
 return 0;
}

推荐答案

如果不更改现有代码,您将无法做到.

You can't do it without changing your existing code.

您要将您的FullList结构传递给您的DeleteNode()函数.这意味着对该结构的任何更改在main中都不可见-该函数正在获取该结构的副本.

You're passing your FullList struct to your DeleteNode() function. This means that any changes to that struct are not visible back in main - the function is getting a copy of it.

您需要将DeleteNode()更改为接受指针:

You would need to change DeleteNode() to accept a pointer:

void DeleteNode(FullList *temp, char c)

然后将其命名为main()时,您会这样做:

Then when calling it main() you would do:

DeleteNode(&List, s);

这样做,您可以在函数中更改temp->head的值,并且在main()

By doing this, you can change the value of temp->head in your function and it will be visible back in main()

temp->head = temp->head->next;

您需要的逻辑是:

  • 检查是否为temp->head->c == c
  • 如果是,请将temp->head替换为temp->head->next
  • 否则将temp->head分配给临时指针*previous.将temp->head->next分配给指针*current.遍历列表,移动两个指针.在current->c中找到匹配项时,将current->next分配给previous->next并将free()分配给current节点.
  • Check to see if temp->head->c == c
  • If yes, replace temp->head with temp->head->next
  • else assign temp->head to a temp pointer *previous. Assign temp->head->next to a pointer *current. Loop through the list, moving both pointers. When you find a match in current->c, assign current->next to previous->next and free() the current node.

这篇关于删除链接列表中的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆