delete an element from a linked list
Hi,
I would like to delete an element from a singled linked list, I have the written the function, but I have some troubles calling it.
The program accepts any words and stores them in the linked list. But How can I delete one of the word entered by the user ?
here's my code:
Code:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
struct list {
char word [ 50 ]; // words entered by the user
struct list * next; //our structure
};
void Printlist (struct list * ); //prototype to display the linked list
/* Deleting an element */
void mdelete( struct list *p,
struct list *i,
struct list **start,
struct list **last)
{
if(p) p->next = i->next;
else *start = i->next;
if(i==*last && p) *last = p;
}
int main ( void )
{ int choice;
struct list *start, *p;
start = malloc ( sizeof (struct list));
start->next = NULL;
do {
printf("\n\n\t 1. Add a word");
printf("\n\t 2. Delete a word");
printf("\n\t 3. Search for and display");
printf("\n\t 4. Display data in queue order");
printf("\n\t 5. Quit");
printf("\n\n\t Enter your choice : ");
scanf("%i", & choice);
if ( choice == 1 ) {
for(p = start; p->next != NULL; p = p->next){} // puts the word after the last one
p->next = malloc (sizeof(struct list));
p->next->next = NULL;
printf("\n\tEnter the word to add : ");
scanf("%s", p->word);
}
if ( choice == 2 ) {
printf("\n\tEnter the word to delete : ");
scanf("%s", ???); // here's my problem, I need to know what should be inside scanf
mdelete( ??? ); // What the parameters should be here?
Printlist ( start );
}
if ( choice == 4 ) {
Printlist ( start );
}
} while ( choice != 5 );
return 0;
Thanks for your help