Hello, I am making an address book using linked lists. I must read in a few (5-10) addresses from a file that I am supposed to make. Then, I must be able to add to this list by using user input within my program, and print out the entire address book. Here is my code so far, sorry for the beginner syntax, not to familiar with C:
/*MY QUESTION IS, HOW DO I CREATE THE FILE I MENTIONED ABOVE, READ IT INTO MY PROGRAM, AND THEN ADD USER INPUTTED ADDRESSES ALSO? THANKS FOR ANY HELP.*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
char name[25];
char address [40];
char phone [20];
struct node *next;
struct node *link;
};
/*Prototyping the necessary functions*/
void show_list(struct *node);
void enter_selection(struct *node);
main()
{
while (1)
{ struct node *pstart, *pprev, *pnow;
int cont;
pstart=pprev=NULL;
{pnow=(struct node *)malloc(sizeof(struct node));
if (pnow==NULL)
printf("Unable to allocate memory");
if (pstart==NULL)
pstart=pnow;
else pprev->next=NULL;
enter_selection(pnow);
pnow->next=NULL;
pprev=pnow;
show_list(pstart);
}
printf("\n Would you like to enter another? (1=YES/0=NO)");
scanf("%d", &cont);
while (cont!=1 && cont!=0)
{ printf ("Sorry, 1 for YES, 0 for NO\n");
scanf("%d", &cont);
}
if (cont==0)
return 0;
}
return 0; }
/*A function that asks the user to input the data into the address book*/
void enter_selection (struct node *pnow)
{
int choice;
printf("\t\t\tMAIN MENU\n\n");
printf("1. Search for a person's address\n");
printf("2. Add a person to your address book\n");
printf("3. Delete a person from your address book\n");
printf("4. View the names of people between two last names\n");
printf("5. View your entire address book\n");
printf("6. End the program\n");
printf("Please enter your selection (1-6):");
scanf("%d", &choice);
if (choice==2)
{fflush(stdin);
printf("Enter the data: Last Name, First Name:");
gets(pnow->name);
printf("Enter a street address, without hitting return:");
gets(pnow->address);
printf("Enter a phone number (xxx-xxx-xxxx):");
gets(pnow->phone);
printf("Enter an integer:");
fflush(stdin);
scanf("%d", &pnow->data);}
}
/*A function that displays the contents of the address book*/
void show_list(struct node *ps)
{
printf(" NAME\t\t\tADDRESS\t\t\t\t\t PHONE\n");
do {
printf("%d %s \t%s \t\t %s \n", ps->data, ps->name, ps->address, ps->phone);
ps=ps->next;
}while (ps !=NULL);
return;
}
/* A function that sorts the data that is within*/
struct node *search(struct node *pfirst, char *nm)
{ struct node *p;
p=pfirst;
while (p!=NULL)
{if (strcmp(nm, p->name)==0)
return p;
p=p->next; }
return NULL;
}
/*A function that will allow the user to insert a new item to the address book*/
struct *insert_node(struct node *p, int item)
{
struct node *pnew, *pprev, *pcur;
pnew=(struct node*) malloc(sizeof(struct node));
if (p==NULL)
{
p=pnew;
p->data=item;
p->link=NULL;
}
else if (p->data>item)
{ pnew->link=p;
p=pnew;
p->data=item;
}
else
{
pcur=p;
while (pcur->data <item)
{pprev=pcur;
pcur=pcur->link;
if(pcur==NULL)
break;
}
pprev->link=pnew;
pnew->link=pcur;
pnew->data=item;
}
return p;
}