Thread: delete a node from linked list problem

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    delete a node from linked list problem

    Code:
    arxeio=first;         s=0;
             while(arxeio!=NULL)
             {
                if (arxeio->aStudent->idNumber==iddel)
                {
                printf("erasing id %d",arxeio->aStudent->idNumber);
        if(first->aStudent->idNumber==iddel)     /* first node to be deleted) */
        {
            temp1 = first->next;  
            free(first);        
            first= temp1; 
            s=1;    
        }
    
    
                  }
                  
                       
               if (arxeio->next->aStudent->idNumber==iddel)
                {
                if (arxeio->next!=NULL)   
                {
                  temp1=arxeio->next->next;
                  free(arxeio->next);
                  arxeio->next=temp1;
                  }                                              
                s=1;
                }
    
    
                arxeio=arxeio->next;
                } 
                if (s!=1)
                { 
                         printf("error! wrong id");
                }
    the list contains the following nodes
    first:
    alex
    4598
    ------
    john
    2245
    ------
    tom
    4445

    if i ask to delete the id 4445 my program works just fine,if i ask to delete any other node the program stops.any idea why is this happening?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    4
    break was the issue after all,i wrote one break in every if loop an the problem is solved.So now the question is why "break"?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Fix your indentation, your code looks horrible. Bad formatting/indentation means it's easy to make mistakes and hard to find or fix them. And it means nobody here will want to help you.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, and there is no such thing as an "if loop", so I have no idea where you put the break statements. Please post your new code, properly indented, and with the break statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete node in linked list..
    By aren34 in forum C Programming
    Replies: 3
    Last Post: 03-19-2011, 11:54 PM
  2. Linked list - Delete first node
    By budala in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 01:32 PM
  3. Linked List Node Problem
    By cpazurek in forum C Programming
    Replies: 5
    Last Post: 07-22-2009, 01:37 PM
  4. Replies: 5
    Last Post: 04-07-2009, 05:51 PM
  5. delete node in Linked List
    By ronenk in forum C Programming
    Replies: 8
    Last Post: 01-05-2005, 01:28 AM