Thread: program won't continue

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    Philippines
    Posts
    2

    program won't continue

    I have this linked list. I am sorting the names of the contacts. The head and middle insert part are already working, but when I need to insert in the tail part, the program won't continue, I don't know why. Please Help. Thanks!
    here's the code :
    Code:
    #include<stdio.h>
    
    typedef struct phonebook{
    
    	char firstname[20];
    	char lastname[20];
    	int phone;
    	int cellphone;
    	struct phonebook* next;
    	struct phonebook* prev;
    
    }node;
    
    node* add(node* head, node** tail);
    void headinsertone(node** head, node* temp, node** tail);
    void headinsert(node** head, node* temp, node** tail);
    void middleinsert(node* ptr1, node* temp);
    void tailinsert(node** tail, node* temp, node* head);
    void display(node *head, node* tail);
    node* load(node* head);
    node* save(node* head);
    
    
    main(){
    
    	node* head = NULL;
    	node* tail = NULL;
    	int option = 0;
    	
    	
    	head = load(head);
    	
    	while ( option != 3){
    	printf("\t\tMENU\n");
    	printf("[1] CREATE a new contact record.\n");
    	printf("[2] VIEW list. \n");
    	printf("[3] QUIT \n");
    	
    	
    	scanf("%d", &option);
    	
    	switch(option){
    		case 1: head = add(head, &tail);
    			break;
    		
    		case 2: display(head, tail);
    			break;
    			
    		case 3: printf("paalam :)\n");
    		head = save(head);
    			return;
    		
    		default: 
    			printf("INVALID INPUT!\n");
    			
    	}
    	}
    	
    	
    }
    
    node* add (node* head, node** tail){
    
    	node* ptr;
    	node* temp;
    	int flag = 0;
    	
    	
    	ptr = head;
    	
    	temp = (node*) malloc(sizeof(node));
    	
    	printf("Enter new Record:\n\n");
    	
    	printf("Contact LastName: \n");
    	scanf("%s", temp -> lastname);
    	printf("Contact FirstName: \n");
    	scanf("%s", temp -> firstname);
    	printf("Mobile number [phone] : \n");
    	scanf("%i", &(temp -> phone));
    	printf("Mobile number [cellphone] : \n");
    	scanf("%i", &(temp -> cellphone));
    	printf("what's next?");
    	
    	
    	printf("\n\n\n");
    	
    
    	
    	if(ptr == NULL){
    			headinsertone(&head,temp,tail); 
    			printf("insert headone");
    				flag = 1;
    	}else if ( 0 < strcmp(ptr -> lastname,temp -> lastname)){
    	printf("insert head");
    			headinsert(&head,temp, tail); 
    			flag =1;
    	
    	/*else if (0> strcmp(ptr -> lastname,temp -> lastname)){
    			tailinsert(tail, temp,head);
    			flag = 1;
    	}*/
    	}else{
    				
    					while( 0 > strcmp(ptr -> lastname, temp -> lastname)){
    							
    							if( 0 < strcmp(ptr -> next -> lastname, temp -> lastname)){
    							printf("nainsert sa middle");
    								middleinsert(ptr, temp);
    								
    								flag = 1;
    							}else{
    								ptr = ptr -> next;
    							}
    					}
    	
    					if(!flag){
    							
    							//*tail = ptr;
    							tailinsert(tail, temp,head);
    							printf("d 1
    							mk2loy");
    							flag =1;
    							
    					}
    	}
    	
    		return head;
    }				
    	
    
    
    
    node* load(node* head){
    
    		FILE* fp;
    		node* temp;
    		node* ptr;
    		
    		ptr = head;
    		
    		fp = fopen("phonebook.txt", "r");
    	
    		if( ptr != NULL){
    			
    		while(!feof(fp)){
    			temp = (node*) malloc(sizeof(node));
    			
    			fscanf(fp, "%s\n", temp -> lastname);
    			fscanf(fp, "%s\n", temp -> firstname);
    			fscanf(fp, "%d\n", &(temp -> phone));
    			fscanf(fp, "%d\n", &(temp -> cellphone));
    			
    			temp -> next = head;
    			head = temp;
    			
    			
    		}
    		}
    		
    		fclose(fp);
    		
    		return head;
    }
    void headinsertone(node** head, node* temp, node** tail){
    				*head = temp;
    				*tail = *head;
    				(*head) -> next = *tail;
    				(*head) -> prev = *tail;
    				
    }
    void headinsert(node** head, node* temp, node** tail){
    
    		node *ptr;
    		ptr = *head;
    		
    		
    		temp -> prev = *tail;
    		temp -> next = *head;
    		(*head) -> prev = temp;
    		(*tail) -> next= temp;
    		*head = temp;
    	
    }
    void tailinsert(node** tail, node* temp,node* head){
    
    
    		(*tail) -> next = temp;
    		temp -> next = head;
    		temp -> prev = *tail;
    		head -> prev = temp;
    		*tail = temp;
    		
    }
    void middleinsert(node* ptr, node* temp){
    
    		temp -> next = ptr -> next;
    		ptr -> next ->  prev = temp;
    		temp -> prev = ptr;
    		ptr -> next = temp;
    		ptr = temp;
    		
    }
    
    void display(node *head, node* tail){
    		node *ptr;
    		
    		ptr = head;
    		
    		
    		if( ptr == NULL){
    			printf("nothing to display!\n\n");
    		}else{
    				
    				
    				do{
    					
    					
    					printf("Last name : %s \n", ptr -> lastname);
    					printf("First Name : %s \n", ptr -> firstname);
    					printf("mobile number: %d \n", ptr -> phone);
    					printf("cellhpone number: %d \n", ptr -> cellphone);
    			
    					ptr = ptr->next;
    					
    					printf("\n\n");
    				}
    			while(ptr!= head);
    		}				
    }
    
    node* save(node* head){
    
    	FILE* fp;
    	node* ptr;
    	
    	ptr = head;
    	
    	fp = fopen("phonebook.txt", "w");
    	
    	do{
    		fprintf(fp, "%s\n", ptr -> lastname);
    		fprintf(fp, "%s\n", ptr -> firstname);
    		fprintf(fp, "%d\n", ptr -> phone);
    		fprintf(fp, "%d\n", ptr -> cellphone);
    		
    		ptr = ptr -> next;
    	}while(ptr != head);
    	
    	fclose(fp);
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You forgot &tail instead of tail in your function call.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you even have a head and tail if you have a circular list?


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM