Thread: delete an element from a linked list

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    16

    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

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I am too sleepy to understand your code now.Specially a long linkedlist one.But I can write:

    Code:
    typedef struct linkedlist {
    char word[100];
    struct linkedlist *next; } newnode;
    
    int main() {
    newnode *startptr=NULL;
    fgets(s,50,stdin);
    delete(&startptr,s);
    return 0;
    }
    
    void delete(newnode **sptr,char *d) {
    newnode *previous,*current,*temp;
    
    
    previous=NULL;
    current=*sptr;
    while((strcmp(current->word,d))!=0 && current!=NULL) {
    previous=current;
    current=current->next; }
    
    if(previous==NULL) {
    printf("There is not d here or linkedlist is empty"); }
    else {
    if(current==NULL) {
    printf("there is no d definitely"); }
    else {
    temp=current;
    previous->next=current->next;
    free(temp); }
    }
    My friends. It is probably full of bugs. Maybe I used freakishly long ways. But It would do it. At least it may give you a different perspective.I hope it helps. And If there are faults please mention them.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    I understand your logic (somehow), but tell me what these 2 lines of codes are supposed to do ?

    Code:
      fgets(s,50,stdin);
      delete(&startptr,s);
    Errors:

    Code:
    stru.c: In function `main':
    stru.c:12: error: `s' undeclared (first use in this function)
    stru.c:12: error: (Each undeclared identifier is reported only once
    stru.c:12: error: for each function it appears in.)
    stru.c: At top level:
    stru.c:16: error: conflicting types for 'delete'
    stru.c:12: error: previous implicit declaration of 'delete' was here
    stru.c: In function `delete':
    stru.c:40: error: syntax error at end of input

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well deleting from any list is an excercise in special cases, really, like this function demonstrates. Once you find the node to delete, and its links, it's easy to reset them and destroy the old node.

    The example assumes that you've passed a pointer to the node to delete, and head is really a pointer to the first node (not a dummy node or somewhere else). Returning NULL must be okay, too.
    Code:
    node * delete_node( node *head, node *that )
    {
        /** make sure you don't try to delete something from an empty list. **/
        assert( head != NULL );
        if( that != NULL ) {
            if( that == head )
                /** deleting the head node. **/
                /** relink the new head **/
                head = head->next;
            } else {
                /** deleting something else; find that. **/
                node *previous = head;
                while( previous->next != that ) {
                    previous = previous->next;
                }
                /** now unlink that using the previous node **/
                previous->next = that->next;
            }
             /** destroy that **/
            free( that );
            that = NULL;
            return head; /** in case it changed. **/
        }
        return NULL;
    }
    Last edited by whiteflags; 10-16-2007 at 08:12 PM.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Firstly , I have written those to give you my logic. It is not compilable . That is normal that gives you some errors if you put my codes directly.

    fgets takes a word from the user by keybord.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    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("&#37;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.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    You're adding all your words to a elem struct list but you are trying to delete the word from a list struct list. liste is empty. So, your search of liste for the word to delete fails.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM