Question

Assignment: Chapter 18 introduces you to linked lists. In this homework assignment you will use the...

Assignment:

Chapter 18 introduces you to linked lists. In this homework assignment you will use the algorithms presented in this chapter to implement a list of names that should be kept in alphabetical order.

Section 18.2 defines the Linked List Operations based on a list of numbers. You will change this code to work with a list of names.

struct ListNode

{

string firstNname;

string lastName;

struct ListNode *next;

};

Rename class NumberList to something more appropriate like StringList or NamesList. You will also need to change the methods to work with strings instead of numbers. You will only need two of the list operations: insertNode (should keep the list sorted by last name), and displayList (should display first followed by last name, one per line). Change them to work with the new type. Keep the constructor and destructor. Note that the destructor needs to delete one node at a time as shown in Destroying the List section.

Your main program should instantiate an object of the NamesList and ask the user to enter a list of names (first and last), which you will insert in alphabetical order by calling the insertNode method. When the user is done entering the names, call the displayList method to display the names, which should be in alphabetical order by last name.

Follow lab documentation guidelines and attach the output at the end of main.cpp.

Turn in main.cpp and list.h with the class interface and implementation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code for above program is given below, I have commented in line for the explanation, the output is attached below the code,

Code:-

#include <iostream>

using namespace std;

// struct to store datatype of ListNode
struct ListNode
{
string firstName;
string lastName;
struct ListNode* next;
};
// first pointer as null
ListNode* first=NULL;

// calss NameList where all the functions are stored
class NameList
{
public:
string firstName;// variable to store firstName
string lastName;// variable to store lastName
NameList(){} // empty constructor
// parametrized constructor
NameList(string firstName,string lastName)
{
this->firstName = firstName;
this->lastName = lastName;
}
public:
  
// function to store node
void insertNode(NameList n)
{
// create a node
ListNode* node = new ListNode();
node->lastName = n.lastName;// store first and last name in the node
node->firstName = n.firstName;
// if list is empty
if(first==NULL)
{
first = node;
node->next = NULL;
}
// if list is not empty
else{
ListNode* temp;// temparory pointers to traverse the list
ListNode* temp2;
// temp points to first node
temp = first;
  
// until we are not at the last or we don't find a greater lastName
while(temp!=NULL && temp->lastName<n.lastName)
{
temp2=temp; // store this node address
temp = temp->next;// move to next node
}
// if we reached the last node, it means there is no name greater lastName than this one
if(temp==NULL)
{
// our previous node is last node which will point to the new node
temp2->next = node;
// next of new node will be null
node->next = NULL;
}
// if we found a greater lastName at first place only
else if(temp==first)
{
// then new node will point to where temp is pointing,
// i.e. the node with greater lastName
node->next = temp;
first=node;// first will point to new node
  
}
// if we found a greater lastName
else
{
// then new node will point to where temp2 is pointing,
// i.e. the node with greater lastName
node->next =temp2->next;
// temp2 will point to new node
temp2->next = node;
  
}
}
}
// display method to display the llst
void display()
{
ListNode* temp = first;
// until we don't reach the end
while(temp!=NULL)
{
// display the current name
cout<<temp->firstName<<" "<<temp->lastName;
cout<<endl;
// then move to nex node
temp=temp->next;
}
}
// destructor
~NameList() {
cout<<"Destructor called!";
}
};

int main()
{
string lastName;
string firstName;
int N;
NameList obj;
  
cout<<"How many names do you want to enter?";
cin>>N;
cout<<"Enter the list of name: ";
for(int i=0;i<N;i++)
{
cout<<endl;
cout<<"Enter the first name of person "<<i+1<<" : ";
cin>>obj.firstName;
cout<<"Enter the last name of person "<<i+1<<" : ";
cin>>obj.lastName;
obj.insertNode(obj);// calling the insertNode method
  
}
  
cout<<"Names in alphabetical order of last name are: \n";
obj.display();// displaying the list
  
return 0;
}

Output:-

Hope it clears your doubt and you like it :)

Add a comment
Know the answer?
Add Answer to:
Assignment: Chapter 18 introduces you to linked lists. In this homework assignment you will use the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a phone...

    This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a phone book using a linked list to keep people's names and phone numbers organized in alphabetical order. 1. Define a structure to hold the contact information including: name, phone number, a pointer to the next node in the list. Example: struct ContactNode { string name; string phoneNumber; ContactNode *next; } 2. Define a class containing the structure and the appropriate methods to update and retrieve...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Ever...

    C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a "next" pointer. Textbook Туре String String String Attribute title author ISBN Textbook* next Library class should have a head node as an attribute to keep the list of the books. Also, following member...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • Write a java program implementing the Linked list. It should be on an small office who...

    Write a java program implementing the Linked list. It should be on an small office who has 5 employees. The program ask the user for ID, First name, Last name and what field the work in(eg: accounting, programmer, HR etc). Each employee (with all the information of that paticular employee) should be placed in one node in the program. The program should repeat and ask the user for all 5 employees information. Also when you display the Linked list it...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design:...

    Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design: You will create a class and then use the provided test program to make sure it works. This means that your class and methods must match the names used in the test program. Also, you need to break your class implementation into multiple files. You should have a car.h that defines the car class, a list.h, and a list.cpp. The link is a struct...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT