Define a struct AddressBook. The struct should be able to hold a first name, las
ID: 3646565 • Letter: D
Question
Define a struct AddressBook. The struct should be able to hold a first name, last name, street address, city, state, and zip for each person. Using this struct, you will implement a doubly linked list. Your program should allow the following operations:1. Allow the user to input data and store it in an address book. Each node should be inserted at the beginning of the list (so the most recent are displayed first).
2. Print the contents of the address book
3. Edit an address book entry
4. Delete an address book entry
This is what I have so far...
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct person
{
string firstname;
string lastname;
string address;
string city;
string state;
string zip;
};
struct NodeType
{
int x;
//int y;
//string z;
//....
NodeType *nextPtr;
NodeType *prevPtr;
};
NodeType *head = NULL; //this will be a pointer to the first item in the list
void insertAtBeginning();
void insertAtEnd();
void printList(); //will print all items in the list
NodeType * getNodeInfo();
void deleteNode(int x);
NodeType * findNode(int info);
NodeType *getPreviousNode(NodeType *n1);
int main()
{
int bookSz;
struct person
{
string firstname;
string lastname;
string address;
string city;
string state;
string zip;
};
person newPerson;
cout << "Enter first name: ";
cin >> newPerson.firstname;
cout << endl;
cout << "Enter last name: ";
cin >> newPerson.lastname;
cout << endl;
cout << "Enter address: ";
cin >> newPerson.firstname;
cout << endl;
cout << "Enter city: ";
cin >> newPerson.state;
cout << endl;
cout << "Enter Zip Code: ";
cin >> newPerson.zip;
cout << endl;
cout<<"Your full address is "<<endl;
cout<<newPerson.firstname<<" "<<newPerson.lastname<<endl;
cout<<newPerson.address<<endl;
cout<<newPerson.city<<" "<<newPerson.state<<" "<<newPerson.zip<<endl;
cout<<endl<<endl;
printList();
insertAtBeginning();
insertAtBeginning();
printList();
insertAtEnd();
printList();
deleteNode(7);
printList();
}
void insertAtBeginning()
{
NodeType *newNode = getNodeInfo();
newNode -> prevPtr=NULL;
if (head == NULL) //if the list is empty
{
newNode -> nextPtr = NULL; //set this as last item in list
head = newNode; //point head pointer to new node
}
else //the list already contains items
{
newNode -> nextPtr = head; //point new node to what is currently the first item
head -> prevPtr = newNode;
head = newNode; //make this the new first item in the list
}
}
void insertAtEnd()
{
NodeType *newNode = getNodeInfo(); //create a new node
newNode -> nextPtr = NULL; //this will be the last item in the list
if (head == NULL) //if the list is empty
{
head = newNode; //only item in list, so it becomes the first item
}
else
{
NodeType *current = head; //start at the beginning
while (current->nextPtr != NULL) //look for the last item in the list
{
current = current->nextPtr; //move current to next item in list
}
current->nextPtr = newNode; //add the new item to last item in list
}
}
NodeType * getNodeInfo() //returns a pointer to a NodeType struct
{
int num;
cout << "Enter an integer: ";
cin >> num;
NodeType *newNode = new NodeType;
newNode -> x = num;
return newNode;
}
void printList()
{
NodeType *current = head; //start a pointer at the first item in the list
if (current == NULL)
{
cout << "The list is empty!" << endl;
}
else
{
while (current != NULL)
{
cout << current->x << endl;
current = current->nextPtr; //move to the next item
}
}
}
void deleteNode(int x)
{
NodeType *n1 = findNode(x);
if (head == n1) //its the first item in the list
{
head = head ->nextPtr;
delete n1;
}
else
{
(n1 ->prevPtr) ->nextPtr = n1 ->nextPtr;
(n1 ->nextPtr) ->prevPtr = n1 ->prevPtr;
delete n1;
}
}
NodeType * findNode(int info)
{
NodeType *current = head; //start at the beginning the list
bool found = false;
do
{
if (current->x == info)
{
found = true;
}
else
{
current = current->nextPtr; //move to next item in the list
}
}
while (!found && current->nextPtr != NULL); //while I did not find it & I'm not at end
if (!found)
{
current = NULL;
}
return current;
}
NodeType *getPreviousNode(NodeType *n1)
{
NodeType *current = head;
bool found = false;
do
{
if (current->nextPtr == n1)
{
found = true;
}
else
{
current = current->nextPtr; //move to next item in list
}
}
while (!found && current->nextPtr != NULL);
if (!found)
{
current = NULL;
}
return current;
}
Explanation / Answer
you have already a structure struct person { string firstname; string lastname; string address; string city; string state; string zip; }; which holds name,address..etc although i don't find any code error in the program above
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.