Implementing a Contact Directory Using Sequential Lists

Building upon an existing sequential list implementation, we can create a comprehensive contact directory system. This system typically requires operations such as adding contacts, removing entries, searching for specific inidviduals, updating information, viewing all records, and exiting the application.

Header File Definition

First, create a header file called directory.h to declare all necessary functions. Define a structure IndividualRecord to store contact attributes including name, gender, age, phone number, and address.

#pragma once
#define MAX_NAME_LENGTH 100
#define MAX_GENDER_LENGTH 4
#define MAX_PHONE_LENGTH 11
#define MAX_ADDRESS_LENGTH 100

// Forward declaration
typedef struct SeqList AddressBook;

// Contact data structure
typedef struct IndividualRecord
{
    char name[MAX_NAME_LENGTH];
    char gender[MAX_GENDER_LENGTH];
    int age;
    char phone[MAX_PHONE_LENGTH];
    char address[MAX_ADDRESS_LENGTH];
} ContactData;

// Function declarations
void InitializeDirectory(AddressBook* book);
void InsertContact(AddressBook* book);
void RemoveContact(AddressBook* book);
void DisplayContacts(AddressBook* book);
void SearchContact(AddressBook* book);
void UpdateContact(AddressBook* book);
void CleanupDirectory(AddressBook* book);

Data Type Configuration

Modify the sequential list to work with contact data by changing the array type:

typedef ContactData ElementType;

// Dynamic sequential list - allocate as needed
typedef struct SeqList
{
    ElementType* buffer;
    int count;      // Number of valid elements
    int capacity;   // Total storage capacity
} SeqList;

typedef SeqList AddressBook;

Implementation File

Create directory.c for function implementations. Initialization and cleanup can reuse existing sequential list functionality:

#include "SL.h"

// Initialize contact directory
void InitializeDirectory(AddressBook* book)
{
    SLInit(book);
}

// Clean up directory resources
void CleanupDirectory(AddressBook* book)
{
    SLDestroy(book);
}

Adding New Contacts

The insertion function collects user input and stores it in the sequential list:

void InsertContact(AddressBook* book)
{
    assert(book);
    ContactData newEntry;
    
    printf("Enter name:\n");
    scanf("%s", newEntry.name);
    printf("Enter gender:\n");
    scanf("%s", newEntry.gender);
    printf("Enter age:\n");
    scanf("%d", &newEntry.age);
    printf("Enter phone number:\n");
    scanf("%s", newEntry.phone);
    printf("Enter address:\n");
    scanf("%s", newEntry.address);

    SLPushFront(book, newEntry);
}

Contact Search Helper

A utility function to locate contacts by name:

int LocateContactByName(AddressBook* book, char targetName[])
{
    int index = 0;
    for (index = 0; index < book->count; index++)
    {
        if (strcmp(targetName, book->buffer[index].name) == 0)
        {
            return index;
        }
    }
    return -1; // Not found
}

Removing Contacts

Deletion requires finding the contact first, then using the sequential list's erase function:

void RemoveContact(AddressBook* book)
{
    printf("Enter contact name to remove:\n");
    char searchName[MAX_NAME_LENGTH];
    scanf("%s", searchName);
    
    int position = LocateContactByName(book, searchName);
    if (position < 0)
    {
        printf("Contact not found\n");
        return;
    }
    
    SLErase(book, position);
    printf("Removal successful\n");
}

Searching Functionality

void SearchContact(AddressBook* book)
{
    char lookupName[MAX_NAME_LENGTH];
    printf("Enter name to search:\n");
    scanf("%s", lookupName);
    
    int result = LocateContactByName(book, lookupName);
    if (result < 0)
    {
        printf("No matching contact found\n");
    }
    else
    {
        printf("Contact located\n");
        printf("Name:%s Gender:%s Age:%d Phone:%s Address:%s \n",
               book->buffer[result].name,
               book->buffer[result].gender,
               book->buffer[result].age,
               book->buffer[result].phone,
               book->buffer[result].address);
    }
}

Display All Contacts

void DisplayContacts(AddressBook* book)
{
    int iterator = 0;
    for (iterator = 0; iterator < book->count; iterator++)
    {
        printf("Name:%s Gender:%s Age:%d Phone:%s Address:%s \n",
               book->buffer[iterator].name,
               book->buffer[iterator].gender,
               book->buffer[iterator].age,
               book->buffer[iterator].phone,
               book->buffer[iterator].address);
    }
}

Updating Contact Information

void UpdateContact(AddressBook* book)
{
    char updateName[MAX_NAME_LENGTH];
    printf("Enter name to update:\n");
    scanf("%s", updateName);
    
    int location = LocateContactByName(book, updateName);
    if (location < 0)
    {
        printf("Contact not found\n");
    }
    else
    {
        ContactData updatedInfo;
        printf("Enter new name:\n");
        scanf("%s", updatedInfo.name);
        printf("Enter new gender:\n");
        scanf("%s", updatedInfo.gender);
        printf("Enter new age:\n");
        scanf("%d", &updatedInfo.age);
        printf("Enter new phone number:\n");
        scanf("%s", updatedInfo.phone);
        printf("Enter new address:\n");
        scanf("%s", updatedInfo.address);

        SLInsert(book, location, updatedInfo);
        printf("Update completed\n");
    }
}

Main Application Interface

Create a menu system for user interaction:

void displayMenu()
{
    printf("*************** CONTACT DIRECTORY ***************\n");
    printf("*** 1.Add Contact   2.Remove Contact ***\n");
    printf("*** 3.Search        4.Update Contact ***\n");
    printf("*** 5.View All      0.Exit System    ***\n");
    printf("***********************************************\n");
}

int main()
{
    AddressBook directory;
    InitializeDirectory(&directory);
    int selection = 0;
    
    do
    {
        displayMenu();
        printf("Make your selection: ");
        scanf("%d", &selection);
        printf("\n");
        
        switch (selection)
        {
        case 1:
            printf("Adding new contact:\n");
            InsertContact(&directory);
            break;
        case 2:
            printf("Removing contact:\n");
            RemoveContact(&directory);
            break;
        case 3:
            printf("Searching for contact:\n");
            SearchContact(&directory);
            break;
        case 4:
            printf("Updating contact:\n");
            UpdateContact(&directory);
            break;
        case 5:
            printf("Displaying all contacts:\n");
            DisplayContacts(&directory);
            break;
        case 0:
            printf("Exit successful");
            break;
        default:
            printf("Invalid option! Please try again\n");
        }

    } while (selection != 0);

    CleanupDirectory(&directory);
    return 0;
}

Tags: data-structures c-programming sequential-list contact-manager implementation

Posted on Sun, 21 Jun 2026 17:55:23 +0000 by MaxBodine