
Originally Posted by
Salem
Your first task is to make delete just find the element to delete.
You'll need
- a while loop
- an if statement
- and printf("Found node containing %d\n", value);
Don't try to delete the node yet, just make sure your code is right at just being able to find the node to delete.
Baby steps.
Done it
Code:
#include<stdio.h>#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void delete( struct node **current, int value)
{
struct node *temp = *current;
while (temp != NULL)
{
if ( temp -> data == value)
{
printf("Found node containing %d\n", value);
}
temp = temp->next;
}
}
void instert( struct node **current, int value)
{
struct node *new = malloc(sizeof(struct node));
if ( new != NULL)
{
new-> data = value;
new -> next = *current;
*current = new;
}
}
void List(struct node *current)
{
while (current !=NULL)
{
printf("%d -> ", current -> data);
current = current -> next;
}
printf("NULL\n");
}
int main ()
{
struct node *head = NULL;
instert(&head, 1);
instert(&head, 2);
instert(&head, 3);
instert(&head, 4);
instert(&head, 5);
List(head);
delete (&head,4);
List(head);
delete (&head,3);
List(head);
delete (&head,5);
// List(head);
printf("\n");
return 0;
}
Code:
5 -> 4 -> 3 -> 2 -> 1 -> NULLFound node containing 4
5 -> 4 -> 3 -> 2 -> 1 -> NULL
Found node containing 3
5 -> 4 -> 3 -> 2 -> 1 -> NULL
Found node containing 5