cmsc 0 Light Poster

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 :

#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("Rachel\n\n");
					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);
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.