Instructions
Requirements and Specifications
Source Code
#include "phentries.h"
#include
#include
#include
/* Create a new entry node */
PhEntry *phentries_create(int area_code, int phone_number, const char *last_name,
const char *first_name)
{
PhEntry *entry;
entry = (PhEntry *)malloc(sizeof(PhEntry));
entry->areaCode = area_code;
entry->phoneNumber = phone_number;
strcpy(entry->lastName, last_name);
strcpy(entry->firstName, first_name);
entry->nextPhEntry = NULL;
entry->prevPhEntry = NULL;
return entry;
}
/* Display an individual entry information */
void phentries_print_entry(PhEntry *entry)
{
printf("Area Code: %d\n", entry->areaCode);
printf("Phone Number: %d\n", entry->phoneNumber);
printf("Last Name: %s\n", entry->lastName);
printf("First Name: %s\n", entry->firstName);
}
/* Insert a new entry on top of the list */
void phentries_insert_entry(PhEntry **entries, PhEntry *entry)
{
entry->nextPhEntry = *entries;
if (entry->nextPhEntry != NULL)
entry->nextPhEntry->prevPhEntry = entry;
*entries = entry;
}
/* Find the entry that holds the given last name */
PhEntry *phentries_find_by_lastname(PhEntry *entries, char last_name[])
{
PhEntry *current;
current = entries;
while (current != NULL)
{
if (strcmp(current->lastName, last_name) == 0)
return current;
current = current->nextPhEntry;
}
return NULL;
}
/* Find the entry that holds the given phone number*/
PhEntry *phentries_find_by_phone(PhEntry *entries, int area_code, int phone)
{
PhEntry *current;
current = entries;
while (current != NULL)
{
if (current->areaCode == area_code && current->phoneNumber == phone)
return current;
current = current->nextPhEntry;
}
return NULL;
}
/* Find the first entry that has the given area code */
PhEntry *phentries_find_by_area(PhEntry *entries, int area_code)
{
PhEntry *current;
current = entries;
while (current != NULL)
{
if (current->areaCode == area_code)
return current;
current = current->nextPhEntry;
}
return NULL;
}
/* Deallocate all entry nodes */
void phentries_delete_all(PhEntry **entries)
{
PhEntry *next;
PhEntry *current;
current = *entries;
while (current != NULL)
{
next = current->nextPhEntry;
free(current);
current = next;
}
*entries = NULL;
}
/* Display all entries */
void ph_entries_print_all(PhEntry *entries)
{
PhEntry*current;
current = entries;
while (current != NULL)
{
printf("Area Code: %d, Phone: %d, Last Name: %s, First Name: %s\n",
current->areaCode,
current->phoneNumber,
current->lastName,
current->firstName);
current = current->nextPhEntry;
}
}