Thread: I need help, deleting nodes!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    I need help, deleting nodes!

    I am trying to delete a node in a contact manager based on the users criteria.I can add contacts just fin and print them out but i cant delete them.Right now im working on deleting a contact based on its SSN but my program crashes when i try to do it.My deletion function is in bold.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    enum SSN_ID_type
    {
        SSN, ID
    };
    typedef union SSN_ID_u
    {
        int SSN;
        int ID;
    } SSN_or_ID;
    typedef struct pnumber
    {
        int area;
        int prefix;
        int suffix;
    } phone_number;
    typedef struct homeAddress
    {
        
        char* street;
        char* zip_code;
        char* city;
        char* state;
    }home_address;
    typedef struct cs_node
    {
        //int number_of_nodes;
        home_address address;
        int id;
        char* first;/* declaration of firstname string*/
        char* last;/* declaration of lastname string*/
        phone_number phone;
        enum SSN_ID_type type;
        SSN_or_ID SSN_ID;
        struct cs_node* next;
    } contact;
    
    contact *start_ptr=NULL;
    
    char *get_string()/* the call string function for first and last name*/
    {
        char a;
        char* string1;
        int counter = 1;
        string1 = (char*)malloc(sizeof(char));
     
        while(((a = getchar())!='\n') && counter != -1)
        {
            string1 =(char*) realloc(string1, sizeof(char)*counter);
            if(string1 == NULL)
            {
                counter =-1;
            }
            else
            {
                string1[counter -1]=a;
                counter++;
            }
        }
        if(counter != -1)
        {
            string1= (char*) realloc(string1, sizeof(char)*counter);
            string1[counter-1] = '\0';
        }
        return(string1);
     
    }
    void add_contact ()
      { contact *temp;   // Temporary pointers
         char input[100];
      
        temp = (contact*)malloc(sizeof(contact));
    
        char id_choice = 'A';
         // Reserve space for new node and fill it with data
        //temp = new contact;
         getchar();
         printf("Enter contacts first name: ");
         temp->first=get_string();
         printf("Enter contacts last name : ");
         temp->last=get_string();
      
         do
         {
      
        printf("S) Enter the social security number for this contact\n");
        printf("I) Enter the student ID for this contact\n");
    
        printf(">> ");
    	fgets(input,sizeof(input), stdin);
    	sscanf(input,"&#37;c",&id_choice);
        //scanf("%c", &id_choice);
        if(id_choice == 'S')
        {
        
         printf(">> social security number: ");
    	fgets(input,sizeof(input), stdin);
    	sscanf(input,"%d",&temp->SSN_ID.SSN);
         //scanf("%d",&temp->SSN_ID.SSN);
         temp->type=SSN;
        
        }
        else if(id_choice == 'I')
        {
      
        printf(">> ID: ");
        fgets(input,sizeof(input), stdin);
        sscanf(input,"%d",&temp->SSN_ID.ID);
        temp->type=ID;
            }
        else
        {
        printf("Let's try that again.\n");
        }
        } while(id_choice != 'S' && id_choice != 'I');
    
         // Set up link to this node
        printf("Enter the phone number of this person\n>> ");
    	fgets(input,sizeof(input), stdin);
            sscanf(input,"%i-%i-%i",&temp->phone.area,
                    &temp->phone.prefix,
                    &temp->phone.suffix);
                    
        temp->next=start_ptr;
        start_ptr=temp;
         //count->number_of_nodes++;
         return ;
      }
    
    
    void display_contact(contact*temp)
    {
        printf("Name: %s %s\n",temp->first,temp->last);
        if(temp->type==SSN){
          printf("SSN:%d\n",temp->SSN_ID.SSN);
        }
        else{
          printf("ID:%d\n",temp->SSN_ID.ID);
          }
        printf("Phone Number:%d-%d-%d\n",temp->phone.area,temp->phone.prefix,temp->phone.suffix);
      //  printf("Street:%s\nCity:%s\nState:%s\nZip Code:%s\n",temp->address.street,temp->address.city,temp->address.state,temp->address.zip_code);
    }
    //////****Delete function*******////////////////////////
    void delete_contact()
    {
            
        int sel,input;
        int input_a,input_p,input_r;
        
        contact *curr_ptr,*prev_ptr;// declares current pointer and previous pointer.
        prev_ptr = NULL; //sets previous pointer to null
    
    
        printf("\nDelete Options:\n 1)Social Security #\n 2)ID number\n 3)phone number\n");
        scanf("%d",&sel);
        for (curr_ptr = start_ptr;curr_ptr != NULL;prev_ptr = curr_ptr, curr_ptr = curr_ptr->next)
        {
        if(sel==1){
            printf("Enter contatcs SSN:\n");
            scanf("%d",&input);
            if (curr_ptr->SSN_ID.SSN == input){ //checks if input criteria matches a contacts
            if(start_ptr==curr_ptr && prev_ptr==NULL){//deletes contacts if its the head/start node
              start_ptr= curr_ptr->next;
              free(curr_ptr);
            }if(curr_ptr==prev_ptr->next && curr_ptr->next!=NULL){//deletes contact if its a middle node
                   
                    
                   prev_ptr->next = curr_ptr->next; //previous nodes pointer skips the removed node
                   free(curr_ptr); 
            }
            if(curr_ptr->next==NULL && prev_ptr->next==curr_ptr){//deletes contact if contats is the last node
                 prev_ptr->next==NULL;
                 }
                 if(start_ptr=NULL){// deletes contact if its the only node
                                 free(curr_ptr);   
                                    }
                 
                 
    }
     }
      }
       }
    
    void display_contacts()
    {
        contact* tmp_node=start_ptr;
        int i = 0;
    
        while(tmp_node != NULL){
            printf("%d) ", i);
        display_contact(tmp_node);
            tmp_node = tmp_node->next;
            i++;
        }
    }
    
    int main()
    {
        int selection = 0;
        
        do {
            printf("\n1) Enter a new contact\n");/* Main menu- gives users selections*/
            printf("2) Display the contacts\n");
            printf("3) Delete a contact\n");
            printf("4) Quit\n");
            printf(">> ");
    
            scanf("%d", &selection);
    
            if(selection == 1)
            {
                add_contact();
            }
            else if(selection == 2)
            {
                display_contacts();
            }
            
        else if(selection==3)
            {
            delete_contact();
        } 
            else if(selection != 4)
            {
        printf("Quit.\n\n");
            }
    
        } while(selection != 5);
        return 0;
    }
    Last edited by Felon; 11-08-2008 at 01:08 AM.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Location
    Finland
    Posts
    14
    You should _not_ post code that is this long in forums, use http://pastebin.com etc.

    I didnt read the whole code just looked there and here. This caught in my eye:
    Code:
    if(start_ptr==NULL){// deletes contact if its the only node
       free(curr_ptr);   
    }

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Finland
    Posts
    14
    Few more things.

    Choosing 4 dont quit because logic error.
    Include string.h if not done already.
    You dont need type cast malloc and realloc in C: (1)
    Are you sure about this: (2) ?
    Code:
    (1)
    string1 = (char*)malloc(sizeof(char));
    etc.
    
    (2)
    for (curr_ptr = start_ptr;curr_ptr != NULL;prev_ptr = curr_ptr, curr_ptr = curr_ptr->next)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting nodes in a linear list
    By sudhanshu_nsit in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2006, 02:00 AM
  2. 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
  3. deleting all nodes of a binary search tree...
    By sachitha in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2004, 06:19 AM
  4. Deleting nodes
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2003, 07:50 PM
  5. Creating three nodes, deleting head
    By Nakeerb in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 09:00 PM