Thread: how to delete nodes

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    how to delete nodes

    hi guys i need some help can you guys help me at deleting nodes I'm not yet that good in linked list and I can only delete the first node but I can't delete nodes in the middle and at the last help please I'm making a student database where we can create courses and student accounts thanks

    Code:
    void deleteclass(ptrClass *pFirst,int arr[])
    {
         int a;
         int i=0;
         ptrClass current,trail;
         
         current = *pFirst;
         
         //dview();
        
        
        
         if(arr[0] != 0)
         {
         do
         {
         //dview();          
         printf("\t\t\t[1] for next\n\t\t[2] for Previous \n\t\t[3] for delete\n\t\t[4] Exit\n\n");          
         printf("\t\t[course code] : %s \n\n",current->coursecode);
            printf("\t\t[section] : %d \n\n",current->section);
                printf("\t\t[number of the units] : %d \n\n",current->units);
                   printf("\t\t[The schedule] : %s \n\n",current->schedule);
                   printf("\t\t[Starting time %d:%d End Time %d:%d \n\n",current->pTime.shours,current->pTime.smin,current->pTime.ehours,current->pTime.emin);
                      printf("\t\t[Name of the lecturer] : %s \n\n",current->lecturer);
                         printf("\t\t[total slots] : %d\n\n",current->slots);
         
         scanf("%d",&a);  
         //for menu functions
         if(a==1)
         {
         system("cls");    
         if(current->pNext != NULL)   
         current= current->pNext;
         
         if(current->pNext == NULL)
         {
           system("cls");
    }
        
         i++;
         if(i > arr[0])
         current = current->pPrev;
         }
         else if(a==2)
         {
         system("cls");   
         current = current->pPrev; 
         i--;          
           if( i < 0)
           i = arr[0] - 1;  
         }
         
         if(a == 3)
         {
            
          
          if(*pFirst == current)
          *pFirst = (*pFirst)->pNext;  
          
        //  else if(current->pNext != NULL)
          
          else if(current->pNext == NULL)
        {
          current->pPrev = current->pNext;
          
          }
        
          free(current);
            
          }
         
         else if(a==4)
         {
         system("cls");    
         i=7; 
         }               
         }while(i<6);
         system("cls");
    }
          else
         printf("\n\n\n\n\n\n\t\t\t\t[There's no class created] \n");
         
         sleep(1000);
         system("cls");
         }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First of all your indentation is crap, which makes this very very difficult to read. Moreover, you are using obscure variable names like a, and arr which could pretty much be anything.

    Also, if i were to take a guess your problems stem from the fact that you are typedefining pointers to other type names which makes the program extremely unreadable. Don't use typedefs unless you really have to, especially if you are a beginner. Just write the whole struct whatever* instead of typedefing it to ptrWhatever.

    What is the effect compilation wise or run-time wise of this code?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete by copy only for non-children nodes?
    By patricio2626 in forum C++ Programming
    Replies: 17
    Last Post: 11-18-2006, 08:37 AM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM