Here's my new code
Code:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
struct elem {
char word [ 50 ];
char words [50];
struct elem * next;
};
struct list {
char words[50];
struct elem *first;
struct elem *last;
};
void init(struct list *liste)
{
liste->first = NULL;
liste->last = NULL;
}
void Printlist (struct elem * );
void delete(struct list *liste, struct elem *elem)
{
struct elem *cur;
struct elem *prev;
if (liste->first == NULL) return;
for (cur=liste->first, prev=NULL; cur != elem; prev=cur, cur=cur->next);
if (prev)
prev->next=cur->next;
else
liste->first=cur->next;
if (cur->next == NULL)
liste->last = cur;
free(elem);
}
void mdelete(char *word, struct list *liste){
struct elem *cur;
for (cur=liste->first; cur != NULL; cur=cur->next)
{
if (strcmp(cur->word, word) == 0)
break;
}
if (cur) delete(liste, cur);
}
int main ( void )
{ char words[50];
struct list liste, *del;
int choice;
init(&liste);
struct elem *start, *p;
start = malloc ( sizeof (struct elem));
start->next = NULL;
del = malloc( sizeof (struct list));
del->first = NULL;
del->last = 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){}
p->next = malloc (sizeof(struct elem));
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", del->words);
mdelete(del->words, &liste);
}
if ( choice == 4 ) {
Printlist ( start );
}
} while ( choice != 5 );
return 0;
}
void Printlist (struct elem * x)
{
int v = 1;
while ( x->next != NULL ) {
printf ("\n\tElement %i is : %s \n", v++, x->word);
x = x->next;
}
}
Could anyone help me with this code. It works and compiles but the when I enter a word to delete it doesn't work.