Complementary task for topic: 11

M Nemeth · 2023-08-29 15:21:04.634218'

Linked list: Contact list

Linked list: Contact list

Implement a simple contact management system using a singly linked list. The contact management system will allow users to add new contacts, search for contacts, list all contacts, and delete contacts.

Hint: If we know the data stored in the list, we just need to put the puzzle together (add element, delete element, serch for element...)

Solution
#include 
#include 
#include 

// Define the structure for a contact
struct Contact {
    char name[50];
    char phone[20];
    struct Contact* next;
};

// Function to create a new contact node with the given data
struct Contact* createContact(const char* name, const char* phone) {
    struct Contact* newContact = (struct Contact*)malloc(sizeof(struct Contact));
    strcpy(newContact->name, name);
    strcpy(newContact->phone, phone);
    newContact->next = NULL;
    return newContact;
}

// Function to add a new contact to the linked list
void addContact(struct Contact** head, const char* name, const char* phone) {
    struct Contact* newContact = createContact(name, phone);
    newContact->next = *head;
    *head = newContact;
}

// Function to search for a contact by name
struct Contact* searchContact(struct Contact* head, const char* name) {
    struct Contact* current = head;
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            return current; // Contact found
        }
        current = current->next;
    }
    return NULL; // Contact not found
}

// Function to list all contacts
void listContacts(struct Contact* head) {
    if (head == NULL) {
        printf("Contact list is empty.\n");
        return;
    }

    printf("Contact List:\n");
    struct Contact* current = head;
    while (current != NULL) {
        printf("Name: %s, Phone: %s\n", current->name, current->phone);
        current = current->next;
    }
}

// Function to delete a contact by name
void deleteContact(struct Contact** head, const char* name) {
    struct Contact* current = *head;
    struct Contact* prev = NULL;

    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            if (prev != NULL) {
                prev->next = current->next;
            } else {
                *head = current->next;
            }
            free(current);
            printf("Contact '%s' deleted from the list.\n", name);
            return;
        }

        prev = current;
        current = current->next;
    }

    printf("Contact '%s' not found in the list.\n", name);
}

// Function to destroy the entire contact list and free memory
void destroyContactList(struct Contact** head) {
    struct Contact* current = *head;
    struct Contact* next;

    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }

    *head = NULL; // Set head to NULL to indicate the list is now empty
}

int main() {
    struct Contact* contacts = NULL;

    addContact(&contacts, "John Doe", "123-456-7890");
    addContact(&contacts, "Alice Smith", "987-654-3210");
    addContact(&contacts, "Bob Johnson", "111-222-3333");

    listContacts(contacts);

    struct Contact* searchedContact = searchContact(contacts, "Alice Smith");
    if (searchedContact != NULL) {
        printf("Contact Found - Name: %s, Phone: %s\n", searchedContact->name, searchedContact->phone);
    } else {
        printf("Contact not found.\n");
    }

    deleteContact(&contacts, "Alice Smith");
    listContacts(contacts);

    destroyContactList(&contacts);
    listContacts(contacts); // This will print "Contact list is empty."

    return 0;
}



Explanation

< < previous    next > >