Code:
#include <stdio.h>
#include <stdlib.h>
enum SSN_ID_type
{
SSN, ID
};
typedef union SSN_ID_u
{
int SSN;
int ID;
} SSN_or_ID;
typedef struct pnumber
{
int area;
int prefix;
int suffix;
} phone_number;
typedef struct homeAddress
{
char* street;
char* zip_code;
char* city;
char* state;
}home_address;
typedef struct cs_node
{
//int number_of_nodes;
home_address address;
int id;
char* first;/* declaration of firstname string*/
char* last;/* declaration of lastname string*/
phone_number phone;
enum SSN_ID_type type;
SSN_or_ID SSN_ID;
struct cs_node* next;
} contact;
contact *start_ptr=NULL;
char *get_string()/* the call string function for first and last name*/
{
char a;
char* string1;
int counter = 1;
string1 = (char*)malloc(sizeof(char));
while(((a = getchar())!='\n') && counter != -1)
{
string1 =(char*) realloc(string1, sizeof(char)*counter);
if(string1 == NULL)
{
counter =-1;
}
else
{
string1[counter -1]=a;
counter++;
}
}
if(counter != -1)
{
string1= (char*) realloc(string1, sizeof(char)*counter);
string1[counter-1] = '\0';
}
return(string1);
}
void add_contact ()
{ contact *temp; // Temporary pointers
char input[100];
temp = (contact*)malloc(sizeof(contact));
char id_choice = 'A';
// Reserve space for new node and fill it with data
//temp = new contact;
getchar();
printf("Enter contacts first name: ");
temp->first=get_string();
printf("Enter contacts last name : ");
temp->last=get_string();
do
{
printf("S) Enter the social security number for this contact\n");
printf("I) Enter the student ID for this contact\n");
printf(">> ");
fgets(input,sizeof(input), stdin);
sscanf(input,"%c",&id_choice);
//scanf("%c", &id_choice);
if(id_choice == 'S')
{
printf(">> social security number: ");
fgets(input,sizeof(input), stdin);
sscanf(input,"%d",&temp->SSN_ID.SSN);
//scanf("%d",&temp->SSN_ID.SSN);
temp->type=SSN;
}
else if(id_choice == 'I')
{
printf(">> ID: ");
fgets(input,sizeof(input), stdin);
sscanf(input,"%d",&temp->SSN_ID.ID);
temp->type=ID;
}
else
{
printf("Let's try that again.\n");
}
} while(id_choice != 'S' && id_choice != 'I');
// Set up link to this node
printf("Enter the phone number of this person\n>> ");
fgets(input,sizeof(input), stdin);
sscanf(input,"%i-%i-%i",&temp->phone.area,
&temp->phone.prefix,
&temp->phone.suffix);
temp->next=start_ptr;
start_ptr=temp;
//count->number_of_nodes++;
return ;
}
void display_contact(contact*temp)
{
printf("Name: %s %s\n",temp->first,temp->last);
if(temp->type==SSN){
printf("SSN:%d\n",temp->SSN_ID.SSN);
}
else{
printf("ID:%d\n",temp->SSN_ID.ID);
}
printf("Phone Number:%d-%d-%d\n",temp->phone.area,temp->phone.prefix,temp->phone.suffix);
// printf("Street:%s\nCity:%s\nState:%s\nZip Code:%s\n",temp->address.street,temp->address.city,temp->address.state,temp->address.zip_code);
}
//////****Delete function*******////////////////////////
void delete_contact()
{
int sel,input;
int input_a,input_p,input_r;
contact *curr_ptr,*prev_ptr;// declares current pointer and previous pointer.
prev_ptr = NULL; //sets previous pointer to null
printf("\nDelete Options:\n 1)Social Security #\n 2)ID number\n 3)phone number\n");
scanf("%d",&sel);
for (curr_ptr = start_ptr;curr_ptr != NULL;prev_ptr = curr_ptr, curr_ptr = curr_ptr->next)
{
if(sel==1){
printf("Enter contatcs SSN:\n");
scanf("%d",&input);
if (curr_ptr->SSN_ID.SSN == input){ //checks if input criteria matches a contacts
if(start_ptr==curr_ptr && prev_ptr==NULL){//deletes contacts if its the head/start node
start_ptr= curr_ptr->next;
free(curr_ptr);
}if(curr_ptr==prev_ptr->next && curr_ptr->next!=NULL){//deletes contact if its a middle node
prev_ptr->next = curr_ptr->next; //previous nodes pointer skips the removed node
free(curr_ptr);
}
if(curr_ptr->next==NULL && prev_ptr->next==curr_ptr){//deletes contact if contats is the last node
prev_ptr->next==NULL;
}
if(start_ptr=NULL){// deletes contact if its the only node
free(curr_ptr);
}
}
}
}
}
void display_contacts()
{
contact* tmp_node=start_ptr;
int i = 0;
while(tmp_node != NULL){
printf("%d) ", i);
display_contact(tmp_node);
tmp_node = tmp_node->next;
i++;
}
}
int main()
{
int selection = 0;
do {
printf("\n1) Enter a new contact\n");/* Main menu- gives users selections*/
printf("2) Display the contacts\n");
printf("3) Delete a contact\n");
printf("4) Quit\n");
printf(">> ");
scanf("%d", &selection);
if(selection == 1)
{
add_contact();
}
else if(selection == 2)
{
display_contacts();
}
else if(selection==3)
{
delete_contact();
}
else if(selection != 4)
{
printf("Quit.\n\n");
}
} while(selection != 5);
return 0;
}