开发者

How to remove a node any integer data

开发者 https://www.devze.com 2023-02-20 19:39 出处:网络
I am working on my homework but I really don\'t know how to remove a node with an integer data I already have a code for adding of nodes, I just need to remove nodes, or can you give me atleast an alg

I am working on my homework but I really don't know how to remove a node with an integer data I already have a code for adding of nodes, I just need to remove nodes, or can you give me atleast an algorithm of it goes like this

addnode(root,5);
addnode(root,2);
addnode(root,6);
display(root);
removenode(root,5);
display(root);
removenode(root,6);

do you guys need code for my addition code? but our proff already gave us the code for showing the display of nodes ;

void display(struct node *head)
{   
     struct node *traverser;
     traverser = head;

     while(traverser!=NULL)
     {
          printf("%d\n",traverser->x);
          traverser=traverser->next; 
     }

}

开发者_StackOverflow社区 struct node { int data; struct node *next };

a question though what does traverser=traverser->next;


I assume you are dealing with a linked list. These are usually built up of entries that contain data and a link to the following name (thus the name):

  struct node {
        int data;
        struct node *next;
  }

As you can see, the link is a pointer in C. Which should point to the next entry. To start traversing a list, you usually have got a head. If you want to remove an entry, you need to traverse the list and once you found the entry you want to remove, simply rearrange the pointers:

  void removeEntry(int data, struct node *head) {
        struct node *prev = NULL, *current = head;
        while(current->data != data) {
              prev = current; // current will always point to the entry in front of current
              current = current->next;
              if(current == NULL) // end of list and no match
                    return;
        }
        // now current is pointing to the entry you want to remove
        // remove it just by rearrangeing pointers
        prev->next = current->next;      
        free(current); // I assume you malloc'ed the memory
  }

Note: Please note that I omitted errorchecking here. Also, sometimes the head is a fixed item in which no data is stored (would work in my case), sometimes head can contain data itself (in this case you need to check if the element you want to remove is the first element and relink head accordingly)


1) you can delete the node from the linked list like(considering head is not dummy node i.e head also contains data)...

int flag=0;
 if(head->data==data_todel) head=head->next; //if head contains the data 
  ptr=head; 
   while(ptr->next!=NULL && flag==0)  
    {   
     if(ptr->next->data!=data_todel)  
      ptr=ptr->next;    
     else 
      {
       flag=1;
       break;
      }  
   } 
 if(flag) ptr->next=ptr->next->next;

you need to use two pointers to free the deleted node.

2)


+------+-------+        +------+-------+
| data1| next  |        | data2| next  |
+------+-------+        +------+-------+
    ^        |              ^
    |        |              |
    |        +--------------+
+---------+
|traverser|
+---------+

after traverser=traverser->next

+------+-------+        +------+-------+
| data1| next  |        | data2| next  |
+------+-------+        +------+-------+
           |              ^        ^
           |              |        |
           +--------------+        |
                                   |
                              +---------+
                              |traverser|
                              +---------+
that means it is assigning the address of of the next node currently pointed by traverser.


a question though what does traverser=traverser->next;

It sets the pointer to the next element in the queue. Iterating in your while cicle until it reaches a null pointer (end of the queue).

(You didn't post the node structure declaration, so i'm just guessing)


The next field is the key in the linked list. Each element has a successor, that way the list is linked. So, when you traverse the list, you start at the first element head and move from one element to the next until there is no next element.

To remove an entry, you need to cycle through the list until you find that entry. Then, set the next variable of the previous entry to the next entry. That way, the element is removed from the list.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号