Write a C++ program that maintains a sample address book system stored in a sequ
ID: 3636295 • Letter: W
Question
Write a C++ program that maintains a sample address book system stored in a sequential text file and processed using a linked list data structure.
Your program will use a dynamic linked list built using a self-referential structure. The program will allow for the allocation and use of memory as needed. For instance, if the user wants to add a new record, then immediately (at run time, hence dynamically) a new memory allocation is requested and the new record is stored there. If another record is requested, then yet another memory allocation is made and "linked" to the first one to form a linked list data structure.
A contact record contains the following items:
- Last name (string),
- First name (string),
contactlist.txt
Explanation / Answer
#include #include #include const maxLength = 255; struct list { char name[maxLength]; char address[maxLength]; char phone[maxLength]; list *next; }; typedef list address; //prototypes void addtoList (); // Add a name to our address book void delfromList (); // Delete a name from our address book void printName (); // Print an entry in the address book void listNames (); // List all the names in the address book void freeList (); // Free all the linked list list *Head = NULL; //Head of linked list int main() { char input; while (1) { cout name; cout > newName -> address; cout > newName -> phone; newName -> next= Head; //point to the first node Head = newName; //update where the head points } void delfromList() //remove a name { address *delPtr; // Pointer to find name to delete address *prevPtr; // Pointer to name before name char delName[maxLength]; // Name to delete cout > delName[maxLength]; if (Head == NULL) { cout name, delName) == 0) { delPtr= Head; Head= Head->next; delete delPtr; return; } prevPtr = Head; while (prevPtr -> next != NULL) // look for the name { if (stricmp(prevPtr -> next -> name,delName) == 0) //if name is found { delPtr= prevPtr -> next; prevPtr -> next = delPtr -> next; delete delPtr; return; } prevPtr= prevPtr -> next; } cout name[maxLength]; searchPtr = Head; while (searchPtr != NULL) { if (stricmp(searchPtr->name, name) == 0) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.