This project involves creating a phone book application that stores a list of (name, phone number) pairs. The application includes functions to initialize the phone book, insert entries, retrieve phone numbers, print entries, and dump the entries to a text file. This README provides an overview of the implementation.
- Entry Struct: Represents an entry with a name and a phone number, both as 30-character arrays.
- PhoneBook Struct: Contains a pointer to an array of entries, the number of inserted elements, and the actual size of the array.
- Functions:
init()
: Initializes the phone book.destroy()
: Deallocates the memory allocated for the phone book.insert()
: Inserts or updates a name and phone number in the phone book.get()
: Retrieves the phone number for a given name.print()
: Prints all entries in the phone book.dump()
: Writes all entries to a text file.
- Entry Struct:
- Contains two 30-character arrays: one for the name and one for the phone number.
- PhoneBook Struct:
- Contains a pointer to an array of entries.
- Contains an integer for the number of inserted elements.
- Contains an integer for the actual size of the array.
-
init(int size)
: Initializes the phone book by allocating memory for the entries' array on the heap. The size of the array is determined by the parameter. It also initializes the count of inserted elements to 0. -
destroy()
: Deallocates the memory allocated for the entries' array to prevent memory leaks. -
insert(const char *name, const char *phone)
: Inserts a name and phone number into the phone book. If the name already exists, it updates the phone number. It returns a boolean value indicating whether the insertion was successful. The insertion fails if the array is full. -
get(const char *name)
: Retrieves the phone number for a given name. Returns a NULL pointer if the name is not found. -
print()
: Prints all entries in the phone book to the console. -
dump(const char *filename)
: Writes all entries to a text file. The file name is given as a parameter to this function.
- Initialize a phone book with a specified size.
- Insert several (name, phone number) pairs into the phone book.
- Retrieve and print a phone number by name.
- Print all entries in the phone book to the console.
- Dump all entries to a specified text file.
- Destroy the phone book to deallocate memory.
- Initialization: Create a phone book with a specified size.
- Insertion: Add new entries or update existing ones.
- Retrieval: Get the phone number associated with a specific name.
- Printing: Display all phone book entries on the screen.
- Dumping: Write all phone book entries to a text file.
- Cleanup: Free the allocated memory when done.
Ensure that the application handles all edge cases and avoids undefined behavior. Proper memory management and error handling are essential for preventing memory leaks and ensuring robustness.