Hello guys
#include<stdio.h> #include<stdlib.h> #include<string.h> struct phonebook { char fName[40]; char lName[40]; char phone[10]; struct phonebook *next; }; struct phonebook *start=NULL; struct phonebook* getData() { return ((struct phonebook *)malloc(sizeof(struct phonebook))); } void insertData() { struct phonebook *temp,*newNode; newNode = NULL; temp = start; newNode = getData(); printf("\nEnter First Name: "); scanf("%s",&newNode->fName); printf("Enter Last Name: "); scanf("%s",&newNode->lName); printf("Enter Phone Number: "); scanf("%s",&newNode->phone); if(temp == NULL) { newNode->next = newNode; start = newNode; } else if(strcmp(temp->fName,newNode->fName)>=0) { while(temp->next != start) temp = temp->next; temp->next = newNode; newNode->next = start; start = newNode; } else { while(temp->next != start && strcmp(temp->next->fName,newNode->fName)<=0) temp = temp->next; newNode->next = temp->next; temp->next = newNode; } printf("\n"); } void displayList(struct phonebook *temp) { if(temp == NULL) printf("No Content...."); else { printf("\nFirst Name \tLast Name \tPhone Number"); printf("\n----------------------------------------------"); while(temp->next != start) { printf("\n%s",temp->fName); printf(" \t%s",temp->lName); printf(" \t%s",temp->phone); temp = temp->next; } printf("\n%s",temp->fName); printf(" \t%s",temp->lName); printf(" \t%s",temp->phone); } printf("\n"); } void copyToFile(struct phonebook *temp) { FILE *fp; fp = fopen("PHONEBOOK.txt","w"); if(fp == NULL) { printf("\nFile Not Opened"); } else { if(fp) fprintf(fp,"FName,LName,Number\n"); do { fprintf(fp,"%s,%s,%s\n",temp->fName,temp->lName,temp->phone); temp = temp->next; }while(temp != start); fclose(fp); } printf("\n"); } int main() { int input,i,n,ch; do { printf("\n-------------PHONEBOOK------------"); printf("\n1.Insert Records"); printf("\n2.Display Records\n3.END\n"); printf("\nEnter your choice: "); scanf("%d",&input); switch(input) { case 1: printf("Enter number of records required: "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\nInsert Data for Record %d",i); insertData(); } copyToFile(start); displayList(start); break; case 2: displayList(start); break; case 3: printf("PROGRAM ENDS !!!"); break; default: printf("Invalid Choice !!!"); } }while(input!=3); printf("\n"); } /* -------------PHONEBOOK------------ 1.Insert Records 2.Display Records 3.END Enter your choice: 1 Enter number of records required: 2 Insert Data for Record 1 Enter First Name: renita Enter Last Name: lobo Enter Phone Number: 9856856254 Insert Data for Record 2 Enter First Name: gyanendra Enter Last Name: maurya Enter Phone Number: 9878845235 First Name Last Name Phone Number ---------------------------------------------- renita lobo 9856856254 gyanendra maurya 9878845235 -------------PHONEBOOK------------ 1.Insert Records 2.Display Records 3.END Enter your choice: 2 First Name Last Name Phone Number ---------------------------------------------- renita lobo 9856856254 gyanendra maurya 9878845235 -------------PHONEBOOK------------ 1.Insert Records 2.Display Records 3.END Enter your choice: 3 PROGRAM ENDS !!! -------------------------------- Process exited with return value 10 Press any key to continue . . . */
No comments:
Post a Comment