English 中文(简体)
这里的错误是什么? 删除一个名字、联系名单
原标题:Whats the mistake here? deleting a node, linked list,
  • 时间:2012-05-06 15:07:39
  •  标签:
  • c

I wrote this code,and I d like to know whats my problem, if u can help me fix my code insted of writting one of ur own, it d help me a lot... Edit: I ve changed it to this, and now it doesnt give me a run time error, but when I print the name, or check if the node exists, it says it is...

void node_delete(friend *amit) // Deleting a node
{
 friend* temp;
 int flag = 0;
 while (head != NULL) // As long the node isnt the last one
 {
       if (0 == (strcmp(head -> next -> name, amit -> name))) // If the name that the user entered matchs a name in the linked list,
       { // It ll skip it
            temp = head -> next;
             head -> next = head -> next -> next; // Deletes a node from the linked list
             flag = 1;
       }
       if (flag == 1) break;
       head = head -> next; // Going to the next node
}
 free(temp); // Freeing the deleted node
 printf ("%s
", amit -> name);
}

:

amit.name = "amit"
amit.age = 16
amit.gender =  m 
node_delete(&amit);

定义:

typedef struct friend // The struct
 {
   char *name; 
   int age;
   char gender; 
   struct friend* next; // A pointer that points to the next node in the linked list
}friend;

感谢:

最佳回答

以下是对前几行的一些评论:

 head = amit;
 head -> next = amit;  // same as amit->next = amit;
 amit -> next = NULL;  // same as head->next = null;
 while (head -> next != NULL) // will never do anything since head->next is null
问题回答
void node_delete(char *name)
{
 friend *tmp, **p;

 for (pp = &root; (tmp = *pp) ; pp = &tmp->next) 
 {
     if (strcmp(tmp->name, name)) continue; // No match: walk on
     printf("Deleted %s
", tmp->name);
     *pp = temp->next;
     free(tmp);
     break;
 }    
}

(assuming a global root pointer to the head of the linked list) To be called like:

node_delete("Amit");
friend *head = NULL;

...

add(data) {
  friend *next = malloc(sizeof(bla));
  fill(next, data);

  if (head == NULL) 
    head = next; 
  else {
    next->next = head;
    head = next;
  }
}

del(data) {
  if (head) {
    if (your_check(head, data)) {
       to_free = head;
       head = head->next;
       free(to_free);
    } else {
      friend *list = head;
      while (list->next) {
        if (your_check(list->next, data)) {
          to_free = list->next;
          list->next = list->next->next;
          free(to_free);
        }
        list = list->next;
      }
    }
  }
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签