Thread: 2 way linked list

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    2 way linked list

    This is a linked list. I am trying to input employee names and addresses. This program works so far but I am having two problems that I need help with

    1- How can I store the full street string ex: 410 North Ave.
    I have tried fgets,sscanf.....but nothing works it only likes non space strings.... Some suggested that I need to parse it or strcpy...but I think there must be another elegant way of doing this.

    2- if the user chooses option 3...then I want to erase the employee record that teh user specifies. ex. if the user types Smith for last name...the I want the program to scan the link list for that last name and erase the record...How can I do that??

    Thanks


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct employee_link
    {
    	char last[15];
    	char first[15];
    	char street[15];
    	char city[15];
    	char state[15];
    	struct employee_link *pnext;
    	struct employee_link *pprevious;
    
    }EMPLOYEE;
    
    int main(void)
    {
    	struct employee_link *pfirst =NULL;
    	struct employee_link *pcurrent =NULL;
    	struct employee_link *plast =NULL;
    
    	char temp[15];
    	int choice=0;                    
    	
    	for(;;)
    	{
    		printf("\n\n   Choose one:");
    		printf("\n   1. Add employee");
    		printf("\n   2. Delete employee");
    		printf("\n   3. Display employee data");
    		printf("\n   4. Quit\n\n");
    
    		printf("   your choice: ");
    		scanf("%d",&choice);
    
    		switch(choice)
    		{
    			case 1:
    				if((pcurrent=(EMPLOYEE *)malloc(sizeof(EMPLOYEE)) )==NULL)
    				{
    					fprintf(stderr,"link2way: no storage available\n");
    					exit(1);
    				}
    
    				if (pfirst==NULL)	
    				{
    					pfirst=pcurrent;
    					pcurrent->pprevious=NULL;
    				}		
    				else
    				{
    					plast->pnext=pcurrent;
    					pcurrent->pprevious=plast;			
    				}
    		
    				printf("\nenter last name: ");
    				scanf("%s", pcurrent->last);
    
    				printf("enter first name: ");
    				scanf("%s", pcurrent->first);
    
    				printf("enter street address: ");
    				scanf("%s", temp);
    				strcpy(pcurrent->street,temp);
    
    				printf("enter city: ");
    				scanf("%s", pcurrent->city);
    
    				printf("enter state: ");
    				scanf("%s", pcurrent->state);	  
    
    				plast=pcurrent;     /*save address to last entry*/                     
    				break;
    
    			case 2:
    				puts("Enter last name to delete: ");
    				scanf("%s", temp);
    					
    
    				break;
    
    			case 3:
    				while(pcurrent != NULL)
    				{
    				printf("\n\n%s\n%s\n",pcurrent->last,pcurrent->first);
    				printf("%s\n%s, %s\n",pcurrent->street,pcurrent->city,pcurrent->state);
    
    				plast=pcurrent;
    				pcurrent=pcurrent->pprevious;
    
    				free(plast);		
    				}break;
    		
    			case 4: free(plast);
    				    exit(1);
    					break;
    			
    		}//end of switch	
    	}//end of loop
    
    	return 0;
    }

  2. #2
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    I think I'll take a stab at this:

    1) store "410 North" Ave as a string. the numbers are character representations. If the data is in a specific format always you could also give the 410 it's own data location in the stucture.
    Take note of the example below.

    Code:
    typedef struct address{
       int PhysAddr;
       char StreetName[20]; //even in the case that you have 9th street.
       address *Next;
       address *Back;
    } Address;
    or in the case where you make the entire address a string, just omit the "int PhysAddr"

    I'd use fgets instead of scanf since fgets will read an entire line up to a newline character. Just make sure you
    Code:
     fflush(sdin);
    just before using EACH fgets or else you could end up with a premature termination of the fgets. fflush removes the last newline character in the input stream buffer that was encountered in the last fgets.
    personally, I like fgets and using the entire address as a string rather than separating it out unless you're doing some serious sorting of the data.


    Now, on to 2.
    Removing a node from a linked list is very straight forward.
    The question you should ask yourself is, "Why if the menu choice is to view a record do we delete it?". Maybe there should be another menu item for deleting records. It would be more elegant if the search would 1) build a linked list of all items found that meet the search criteria. This is great for returning a list of ALL the smiths found in your search.
    i.e.
    Smith, Arn
    Smith, Beth
    Smith, Laura
    Smith, Smitty

    but you didn't ask that....
    So all I'd do in the simple case that you've asked is simply do a sequential search of the list for smith. The first time your search key has a hit, simply remove that node by setting the node's pointer to a temp node,
    i.e
    Code:
     
    Address temp; 
    temp=node;
    then redirecting the previous and next pointers so that the node containing smilth is no longer a part of the list, then free(temp);
    You're done.

    oh...if you wanted to make sure you deleted all the smiths, Put your search/delete alogorithm in a loop. in the loop search the list if a hit is found, remove the node, free mem, repeat search. If search is a miss, then there are no more smiths. exit your loop. Be careful not to free temp if you didn't assign it. freeing already freed memory will crash your app.

    Mike
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  3. #3
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    >> fflush(sdin);
    >And how many posts have you read which tell you this is a bad >idea?

    Thanks Salem. I've never read any posts stating that fflush(stdin) was a bad idea. but I know now.

    Mike
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM