Function for deleting an item from linked list.

node *delete(node *head)
{
node *find(node *p, int a);
int  key;        /* item to be deleted */
node *n1;        /* pointer to node preceding key node */
node *p;        /* temporary pointer */
printf("\n What is the item (number) to be deleted?");
scanf("%d", &key);
if(head->number == key)     /* first node to be deleted) */
{
p = head->next;        /* pointer to 2nd node  in list */
free(head);        /* release space of key node */
head = p;        /* make head to point to 1st node */
}
else
{
n1 = find(head, key);
if(n1 == NULL)
printf("\n key not found \n");
else                             /* delete key node */
{
p = n1->next->next;        /*  pointer to the node
following the keynode */

free(n1->next);            /* free key node */
n1->next = p;            /* establish link */
}
}
return(head);
}
                /* USE FUNCTION find() HERE */
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s