C中链表删除后显示 [英] Display after deletion in linked list in C

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

问题描述

实际上这是另一个问题,但它发生了变化,所以我决定提出一个新问题.

Actually this was another problem but it changed so I decided to open a new question.

我的代码是

typedef struct inner_list 
{
 int count;
 char word[100];
 inner_list*next;
} inner_list;
typedef struct outer_list
{
 char word [100];
 inner_list * head;
 int count;
 outer_list * next; 
} outer_list;
void delnode(outer_list **head,char num[100])//thanks to both Nir Levy and Jeremy P.
{
    outer_list *temp, *m;
    m=temp=*head; /*FIX #1*/
    while(temp!=NULL) {
        if(strcmp(temp->word,num)==0) {
            if(temp==*head) {
                delinner(temp->head); /* FIX#2 */
    *head=temp->next;

                free(temp);
                return;
            } else {
                delinner(temp->head); /* FIX#2 */ 
    m->next=temp->next;

                free(temp);
                return;
            }
        } else {
            m=temp;
            temp= temp->next;
        }
    }
    printf(" ELEMENT %s NOT FOUND ", num);
}
void delinner(inner_list *head) { /* FIX#2 */
    inner_list *temp;
    temp=head;
    while(temp!=NULL) {
        head=temp->next;
        free(temp);
        temp=head;
    }
}
void delnode2(outer_list *up,inner_list **head,char num[100])
{
    inner_list *temp2,*temp, *m;
 outer_list *p;
 p = up;

 while(p!=NULL){m=temp=temp2=p->head; 
    while(temp!=NULL) {
        if(strcmp(temp->word,num)==0) {
            if(temp==(*head)) {
                *head=temp->next;

                free(temp);
                return;
            } else {
                m->next=temp->next;
                free(temp);
                return;
            }
        } else {
            m=temp;
            temp= temp->next;
        }
    }
 p=p->next;
 }
    printf(" ELEMENT %s NOT FOUND ", num);
}
void print_node(outer_list *parent_node) 
{ 


 while(parent_node!=NULL){
 printf("%s	%d	", parent_node->word, parent_node->count);

    inner_list *child_node = parent_node->head;
 printf("list: ");
 if(child_node ==NULL){printf("BUARADA");}

 while (child_node != NULL) {
  printf("%s-%d", child_node->word,child_node->count);

        child_node = child_node->next;

        if (child_node != NULL) {
            printf("->");
        }
 }
    printf("
");
 parent_node = parent_node->next;
 }
}

在从外部列表中删除一个元素时,我也尝试从inner_list 中删除相同的元素.

While deleting an element from outer list I am also trying the delete the same element from inner_list too.

例如: - 假设aaa 是outer_list 链表的一个元素,让我们用outer_list *p 指向它 - 这个aaa 也可以在inner_list 链表中.(它可以在 p->head 或另一个内部列表中.)现在,又是棘手的部分.我尝试对outer_list 删除应用相同的规则,但是每当我删除inner_list 的head 元素时,它都会出现错误.print_node或者delnode2哪里出错了?

For example: - Let's say aaa is an element of outer_list linked list and let's point it with outer_list *p - This aaa can also be in an inner_list linked list too. (it can be in p->head or another innerlist.) Now, the tricky part again. I tried to apply the same rules with outer_list deletion but whenever i delete the head element of inner_list it gives an error. Where is the wrong thing in print_node or delnode2?

实际上,如果一个outer_list节点删除了它内部的inner_list的链表,也应该被删除.这就是使用delinner方法的原因.

actually if it a outer_list node deleted the linked list of inner_list inside of it should be removed too. This is why delinner method used.

例如:

outer     inner
aaa       bb->cc
bb        aaa->cc

when i wanted to delete "aaa" The result should be:
outer     inner
bb         cc 

推荐答案

delnode()inner_list **head 参数是什么?我假设 up 是外部列表节点,您要从其内部列表中删除包含 num 中给出的字符串的节点.head 不适合这张图片.而且你似乎没有正确使用它.我稍微重写了函数,省略了参数,更改了注释行,并给出了更有意义的名称:

What is the inner_list **head parameter of delnode() ? I assume that up is the outer list node, from whose inner list you want to remove the node containing the string given in num. head just doesn't fit into this picture. And you don't seem to be using it properly anyway. I rewrote the function a bit, omitting the parameter, changing the commented lines, and giving more meaningful names:

void del_inner_node(outer_list *up, char num[100])
{
  inner_list *temp, *m;
  outer_list *p;
  p = up;

  while (p != NULL) {
    m = temp = p->head;
    while(temp!=NULL) {
      if(strcmp(temp->word,num)==0) {
        if(temp==p->head) {   // refer to p->head
          p->head=temp->next; // refer to p->head
          free(temp);
          return;
        } else {
          m->next=temp->next;
          free(temp);
          return;
        }
      } else {
        m=temp;
        temp= temp->next;
      }
    }
    p=p->next;
  }
  printf(" ELEMENT %s NOT FOUND ", num);
}

注意 temp2 没有使用,所以我删除了它.

Note that temp2 is not used, so I removed it.

现在,在您的代码中没有对 delnode2 (del_inner_node) 的调用.您实际上可以在 delnode 中调用它,以防在当前外部节点中找不到搜索的字符串:

Now, in your code there is no call to delnode2 (del_inner_node). You could actually call it within delnode, in case the searched string was not found in the current outer node:

void del_all_nodes(outer_list **head,char num[100])//thanks to both Nir Levy and Jeremy P.
{
    ...
    while(temp!=NULL) {
        if(strcmp(temp->word,num)==0) {
            ...
        } else {
            del_inner_node(temp,num);
            m=temp;
            temp= temp->next;
        }
    }
    ...
}

这样你就可以通过一次调用删除所有包含aaa"的节点:

This way you can remove all nodes containing "aaa" with a single call:

outer_list *head;
// set up the lists
del_all_nodes(&head, "aaa");

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

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