Create a structure called AddressNode 1. The structure should contain a string f
ID: 3539241 • Letter: C
Question
Create a structure called AddressNode
1. The structure should contain a string for name and a pointer called AddressNode that points to %u201Cnext%u201D (the next element in the list).
Create classed called ABook.
1. Create the Default Constructure. (public)
2. Create the Deconstructor. (public)
3. AddressNode* topPtr. (private) %u2013 Should be a pointer to the top of the list.
ABook Methods (each of these methods must be created in your program)
1. ABook::ABook() %u2013 Default Constructor
2. ABook::Insert(string NewItem) %u2013 Add item to the Linked List
3. ABook::SortedInsert(string NewItem) %u2013 Add item to the Linked List (sorted)
4. ABook::Remove(string& item) %u2013 Remove element from the top of the stack and return the item.
5. ABook::~ABook() %u2013 Deconstructor. Removes all elements from the list.
Main Method
1. Declare new List (Hint: ABook Book;)
2. Declare new string newName.
3. Declare new string nameToRemove.
4. Call Book.Insert(%u201CPrecious%u201D); - Insert Precious into list.
5. Call Book.SortedInsert(%u201CKen%u201D); - Insert Ken into listed (sorted)
6. Call Book.SortedInsert(%u201CEileen%u201D); - Insert Eileen into list (sorted)
7. Call Book.SortedInsert(%u201CFrank%u201D); - Insert Frank into list (sorted)
8. Use Book.Remove to remove each name one at a time and display to screen.
9. Include: system("PAUSE"); after your output to pause the screen.
Example output of your program
Sorted List:
Eileen
Frank
Ken
Precio
THIS IS THE PROGRAM BUT IT IS NOT COMPILLING CORRECTLY:
#include <stdlib.h>
#include <iostream>
#include <string>
#include <list>
using std::cout;
using std::endl;
using std::string;
using namespace std;
//create and define the structure of the node
struct AddressNode //one element of list
{
string name; //data item for names
AddressNode* next; //pointer to next element
};//end struct node
////////////////////////////////////////////////////////////////
class ABook{//a list of links
private:
AddressNode* topPtr; //pointer to first link
public:
ABook() //default constructor
{ topPtr = NULL; } //no first link
ABook::~ABook(); //deconstructor. Removes all elements from the list.
void ABook::Insert(string NewItem); // Add item to the Linked List
void ABook::SortedInsert(string NewItem);//Add item (sorted)
void ABook::Remove(string& item); /* Remove element from top
of the stack and return the item.*/
}; //end class ABook
////////////////////////////////////////////////////////////////////////////
/*
ABook::ABook(string newname, AddressNode* topPtr=0){
//constructs an empty list
// name = newname
// next = topPtr;
topPtr = NULL;
} // end ABook
*/
//--------------------------------------------------------------//
ABook::~ABook(){
//deletes all the elements in the last as long as its not empty
while( topPtr != NULL )
{
AddressNode *newNode = topPtr->next;
delete newNode;
newNode = topPtr;
}
} //end ABook deconstructor
//--------------------------------------------------------------//
void ABook::Insert(string NewItem){
//add items to linked list
string NewItem;
AddressNode* newNode = new node; //make a new item
newNode->name = NewItem; //give it data
newNode->next = topPtr; //it points to next link
topPtr = NewItem; //now first points to this
}//end ABook::Insert (function)
//--------------------------------------------------------------//
void ABook::SortedInsert(string NewItem){
Addressnode* current = topPtr; //set ptr to first link
cout << "Sorted List:"<<endl;
while( current != NULL ) //quit on last link
{
cout << current->name << endl; //print data
current = current->next; //move to next link
}
}//end ABook::SortedInsert(function)
//--------------------------------------------------------------//
void ABook::Remove(string& item){
string item;
AddressNode *item;
if(topPtr != NULL)
{
item = topPtr;
topPtr = topPtr->;
delete item;
}
}
//--------------------------------------------------------------//
int main(int argc, char** argv) {
ABook Book; //make linked list
string newName;
string nameToRemove;
cout<<"Sorted List: "<<endl;
Book.Insert("Precious"); // Insert Precious into list.
Book.SortedInsert("Eileen"); // Insert Eileen into list (sorted)
Book.SortedInsert("Frank"); // Insert Frank into list (sorted)
Book.SortedInsert("Ken"); // Insert Ken into listed (sorted)
Book.Remove();
return (EXIT_SUCCESS);
}//end main function
Explanation / Answer
//CORRECTED CODE
//RUN AND CHECK YOURSELF
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
//create and define the structure of the node
struct AddressNode //one element of list
{
string name; //data item for names
AddressNode* next; //pointer to next element
};//end struct node
////////////////////////////////////////////////////////////////
class ABook{//a list of links
private:
AddressNode* topPtr; //pointer to first link
public:
ABook() //default constructor
{ topPtr = NULL; } //no first link
~ABook(); //deconstructor. Removes all elements from the list.
void Insert(string NewItem); // Add item to the Linked List
void SortedInsert(string NewItem);//Add item (sorted)
void Remove(string& item); /* Remove element from top
of the stack and return the item.*/
}; //end class ABook
////////////////////////////////////////////////////////////////////////////
/*
ABook::ABook(string newname, AddressNode* topPtr=0){
//constructs an empty list
// name = newname
// next = topPtr;
topPtr = NULL;
} // end ABook
*/
//--------------------------------------------------------------//
ABook::~ABook(){
//deletes all the elements in the last as long as its not empty
while( topPtr != NULL )
{
AddressNode *newNode = topPtr->next;
delete newNode;
newNode = topPtr;
}
} //end ABook deconstructor
//--------------------------------------------------------------//
void ABook::Insert(string NewItem){
//add items to linked list
AddressNode* newNode = new AddressNode; //make a new item
newNode->name = NewItem; //give it data
newNode->next = topPtr; //it points to next link
topPtr = newNode; //now first points to this
}//end ABook::Insert (function)
//--------------------------------------------------------------//
void ABook::SortedInsert(string NewItem){
AddressNode* current ;
AddressNode* newNode = new AddressNode;
newNode->name = NewItem;
if (topPtr == NULL || topPtr->name.compare(NewItem)>=0)
{
newNode->next = topPtr;
topPtr = newNode;
}
else
{
/* Locate the node before the point of insertion */
current = topPtr; //set ptr to first link
while (current->next!=NULL && current->next->name.compare(NewItem)<0)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}//end ABook::SortedInsert(function)
//--------------------------------------------------------------//
void ABook::Remove(string& item){
if(topPtr != NULL)
{
item = topPtr->name;
topPtr = topPtr->next;
}
}
//--------------------------------------------------------------//
int main(int argc, char** argv) {
ABook Book; //make linked list
string nameToRemove;
int len=0;
Book.Insert("Precious"); // Insert Precious into list.
Book.SortedInsert("Eileen"); // Insert Eileen into list (sorted)
Book.SortedInsert("Frank"); // Insert Frank into list (sorted)
Book.SortedInsert("Ken"); // Insert Ken into listed (sorted)
cout<<"Sorted List: "<<endl;
// remove words one by one, 4 words in total
for(int i=0;i<4;i++)
{
Book.Remove(nameToRemove);
cout<<nameToRemove<<endl;
}
system("PAUSE");
return 0;
}//end main function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.