Home > Back-end >  Is someone who can data structure? This how to write?
Is someone who can data structure? This how to write?

Time:03-09

Written in c a delete value is the first key nodes in the singly linked list: find the value of the key nodes exists, if any, are removed; Otherwise it returns NULL,

CodePudding user response:

 
/*
==========================
Function: to delete a specified node
(in this case is to remove node) of the specified student id
Returns: a pointer to the first chain table
==========================
*/
Struct student * Del (struct student * head, int num)
{
Struct student * p1;//p1 to save the current need to check the node address
Struct student * (p2);//p2 to save the current checked the address of the node
If (head==NULL)//is empty list (a combination of FIG. 3)
{
Printf (" \ nList is null! \n");
return head;
}

//positioning to delete node
p1=head;
While (p1 - & gt; Num! Num=& amp; & P1 - & gt; Next!=NULL)//p1, pointing to the node is not to find, and it is not the last node, will continue to find
{
p2=p1;//save the address of the current node
P1=p1 - & gt; next;//ward a node
}

If (p1 - & gt; Num==num)//found, (a combination of figure 4, 5)
{
If (p1==head)//if you want to delete the node is the first node
{
The head=p1 - & gt; next;//head pointer to a node after the first node, which is the second node, thus the first node is not in the list, or delete
}
Else//if it is other nodes, let original pointer to the current node, point to the next node, remove
{
The p2 - & gt; Next=p1 - & gt; next;
}

Free (p1);//release the current node
P1=NULL;
Printf (" \ ndelete % ld success! \ n ", num);
N -=1;//the total number of nodes minus 1
}
The else//not found
{
Printf (" \ n % ld not had been found! \ n ", num);
}

return head;
}


Search the yao in CSDN, a lot of, key word: C language list,
  • Related