Replace a list with a new entry?

This is a discussion on Replace a list with a new entry? within the C Programming forums, part of the General Programming Boards category; Hey guys, back again with yet another "intelligent" question So, I finished my linked list assignment to build a telephone ...

  1. #1
    Hopeless
    Join Date
    Jul 2005
    Location
    Bay Area, CA
    Posts
    25

    Replace a list with a new entry?

    Hey guys, back again with yet another "intelligent" question

    So, I finished my linked list assignment to build a telephone directory, or phone book.

    The teacher then gave us the second part, which is to provide the user with an option to modify the pre-existing phone numbers. Basically, you enter in a phone number, you print the list and realize you entered the phone number wrong. Instead of just deleting it and entering the all the information again, just modify the phone number alone.

    This is what I know:

    I know I have to ask the user to enter the old number and compare that number to the numbers in the list (I am using a strcmp function). If the numbers match, ask the user for the new number and replace the old with the new.

    This is what I don't know:

    How do I "link" the new entry to the same name and replace the corresponding phone number? I know I have to set the old list equal to the new list in order to replace the old list (right ) but I am just not getting it. I have tried it over and over and it is just kicking my A$$!!!

    Here is my function:
    Code:
    void modify_list (void) // --> BEGIN modify_list FUNCTION
    {
    	//// LOCAL DEFINITIONS
     	char area[MAXLEN];  /* area code to look for */
        	char phone[MAXLEN];  /* phone number to look for */
    	char new_area[MAXLEN]; /* new area number */
    	char new_phone[MAXLEN]; /* new phone number */
        	NODE *search_ptr; /* Pointer to search list for area code */
    
    	//// STATEMENTS
    	while (pList==NULL){
    		printf("\aNO NUMBERS IN LIST\n");
    		return;
    		}
    
    	printf ("Enter the area code of the telephone number you wish to modify and key ENTER:  ");
        	fgets (area, MAXLEN, stdin);
    	area[strlen(area) -1] = '\0';
    
    	printf ("Enter the last 7 digits of the telephone number you wish to modify\n");
    	printf ("Enter in the format XXX-XXXX and key ENTER:  ");
    	fgets (phone, MAXLEN, stdin);
    	phone[strlen(phone) -1] = '\0';
    
        	search_ptr= pList;
    
        	while (search_ptr != NULL) {
            	if ((strcmp (search_ptr->area, area) == 0) &&
    		(strcmp (search_ptr->phone, phone) == 0)) {
               	printf ("\nEnter new area code and key ENTER: ");
    		fgets (new_area, MAXLEN, stdin);
    		new_area[strlen(new_area) -1] = '\0';
    
    		printf ("Enter new phone number and key ENTER: ");
    		fgets (new_phone, MAXLEN, stdin);
    		new_phone[strlen(new_phone) -1] = '\0';
    
    		
    		
              	return;
            	}
            
    	search_ptr= search_ptr->link; /* Move to next item */
    	}
        	printf ("\aNUMBERS NOT FOUND\n");
    	return;
    }
    The area coded in red is where I am comparing the old with the new. right now, it works, but it just does replace the old number with the new entry (obviously because I am not telling it to, but that is my question).

    Any thoughts?
    Last edited by niponki; 08-16-2005 at 10:34 PM.

  2. #2
    Hopeless
    Join Date
    Jul 2005
    Location
    Bay Area, CA
    Posts
    25
    Ok, another couple hours down the drain, and still not getting it!! arrrhhgg... anyone?

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Why are you even resting the pointer at all why not just change the value of the number of the specified list
    Woop?

  4. #4
    Hopeless
    Join Date
    Jul 2005
    Location
    Bay Area, CA
    Posts
    25
    hmm.. I don't really follow. I did try to take away the new arrays for the "new_area" and "new_phone" and just inputed them into the old ones to try and "cover-up" the old information, but it still did not work.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let's pretend we have the following node:
    Code:
    struct foo
    {
        struct foo *link;
        char data[BUFSIZ];
    };
    Now, all we want to do is given a node, find the entry that contains <something> and replace the contents of that same node with new data. We might do something like so:
    Code:
    int findandreplace( struct foo *thelist, char *tofind, char *newdata )
    {
        ...define some variables to help us along...
    
        ...do error checking to make sure all arguments aren't null...
    
        for( ptr = thelist; ptr != NULL ptr = ptr->next )
            if( strcmp( ptr->data, tofind ) == 0 )
                break;
    
        /* if you are here, you've either run out of list, or have a match */
        if( ptr != NULL )
        {
            strcpy( ptr->data, newdata );
            return 1; /* return successful replacement */
        }
        return 0; /* return no 'tofind' found in 'thelist' */
    }
    Now I didn't really do any error checking here, and with strcpy you can potentially overflow this buffer, but I'll leave all that up to you, as well as the actual conversion of this example into what you need.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21