Question

Implement an application that uses linked lists. To be completed by a group of 3 people. Assignment: This is a group assignme

contactlist.cpp (with implementation of all member functions) and main.cpp (with the logic of the phone book operations). It

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;  // head now points to the new node
        newNode->next = nullptr;
    }
    else // otherwise look for the right place to insert node
    {
        nodePtr = head;
        previousNode = nullptr;
        while (nodePtr != nullptr /***** && condition you need to keep the list sorted *****/)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }
        if (previousNode == nullptr) // if the new node is to be the first in the list
        {
            head = newNode;
            newNode->next = nodePtr;
        }
        else // otherwise insert after the previous node
        {
            previousNode->next = newNode;
            newNode -> next = nodePtr;
        }
    }
}

/*************************************************************************************** 
   Delete the node whose information is given as a parameter. If not found, just returns.
****************************************************************************************/ 

void deleteNode (string name)
{
   ContactNode *nodePtr;
   ContactNode *previousNode;

   if (!head) // if the list is empty, do nothing
      return;

   /***** write code to test if the first node is the one *****/
   {
      nodePtr = head->next;
      delete head;
      head = nodePtr;
   }
   else
   {
      nodePtr = head;
      // skip all nodes whose values don't match
      while (nodePtr != nullptr /***** && test for node values) *****/
      {
         previousNode = nodePtr;
         nodePtr = nodePtr->next;
      }
      if (nodePtr) // if nodePtr is not at the end
      {
         previousNode->next = nodePtr->next; // link the previous node to the node after
         delete NodePtr;  // delete the node
      }
   }
}

/*************************************************************************************** 
     This function destroys a linked list one one node at a time
****************************************************************************************/ 
destroy()
{
    ContactNode *nodePtr;
    ContactNode *nextNode;
    ContactNode = head;

    while (nodePtr != nullptr)
    {
        nextNode = nodePtr->next; // save the pointer to the next node
        delete nodePtr;  // delete the current node
        nodePtr = nextNode; // move on the next node on the list
    }
}

contact.h

#ifndef CONTACT_H_

#define CONTACT_H_

#include <string>

class Contact {

private:

struct ContactNode

{

std::string first_name;

       std::string last_name;

std::string phoneNumber;

ContactNode *next;

};

ContactNode *head;

public:

     Contact();

virtual ~Contact();

   void insertContact(std::string first_name, std::string last_name, std::string

phoneNumber);

void deleteContact(std::string first_name, std::string last_name);

void printContacts();

};

#endif /* CONTACT_H_ */

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

// contact.h
#ifndef CONTACT_H_
#define CONTACT_H_

#include <string>

class Contact {

private:

struct ContactNode
{
std::string first_name;
std::string last_name;
std::string phoneNumber;
ContactNode *next;
};

ContactNode *head;

public:

Contact();
virtual ~Contact();
void insertContact(std::string first_name, std::string last_name, std::string phoneNumber);
void deleteContact(std::string first_name, std::string last_name);
void printContacts();

};

#endif /* CONTACT_H_ */

//end of contact.h

// contactlist.cpp

#include "contact.h"
#include <iostream>

using namespace std;

//linked list operation

// constructor to create an empty list
Contact::Contact()
{
head = nullptr;
}

/***************************************************************************************
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 Contact::insertContact(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 *****/
newNode->first_name = first_name;
newNode->last_name = last_name;
newNode->phoneNumber = phoneNumber;
newNode->next = nullptr;

if (!head) // head points to nullptr meaning list is empty
{
head = newNode; // head now points to the new node
newNode->next = nullptr;
}
else // otherwise look for the right place to insert node
{
nodePtr = head;
previousNode = nullptr;
// loop that continues till we find the node which has first_name comes after the first_name to insert alphabetically
// or first_names are equals and last_name comes after last_name to insert.
while ((nodePtr != nullptr) && ((nodePtr->first_name < first_name) || (nodePtr->first_name == first_name && nodePtr->last_name <= last_name)))
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}

if (previousNode == nullptr) // if the new node is to be the first in the list
{
head = newNode;
newNode->next = nodePtr;
}
else // otherwise insert after the previous node
{
previousNode->next = newNode;
newNode -> next = nodePtr;
}
}
}

/***************************************************************************************
Delete the node whose information is given as a parameter. If not found, just returns.
****************************************************************************************/

void Contact::deleteContact(std::string first_name, std::string last_name)
{
ContactNode *nodePtr;
ContactNode *previousNode;

if (!head) // if the list is empty, do nothing
return;

/***** write code to test if the first node is the one *****/
// if head's first_name and last_name equals the input first_name and last_name, delete the head node
if(head->first_name == first_name && head->last_name == last_name)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head;
// skip all nodes whose values don't match the first_name and last_name
while ((nodePtr != nullptr) && (nodePtr->first_name != first_name || nodePtr->last_name != last_name))
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}

if (nodePtr) // if nodePtr is not at the end
{
previousNode->next = nodePtr->next; // link the previous node to the node after
delete nodePtr; // delete the node
}
}
}

/***************************************************************************************
This function destroys a linked list one one node at a time
****************************************************************************************/
// destructor to delete all the nodes of the list
Contact::~Contact()
{
ContactNode *nodePtr;
ContactNode *nextNode;
nodePtr = head;

while (nodePtr != nullptr)
{
nextNode = nodePtr->next; // save the pointer to the next node
delete nodePtr; // delete the current node
nodePtr = nextNode; // move on the next node on the list
}
}

// function to display the contacts of the list
void Contact:: printContacts()
{
ContactNode *nodePtr = head; //set nodePtr to head

// loop over the list
while(nodePtr != nullptr)
{
// display the node's details
cout<<nodePtr->first_name<<" "<<nodePtr->last_name<<" "<<nodePtr->phoneNumber<<endl;
nodePtr = nodePtr->next; // go to next node
}
cout<<endl;
}

//end of contactlist.cpp

// main.cpp
#include <iostream>
#include "contact.h"
using namespace std;

int main()
{
Contact list;
list.insertContact("John","Smith","1234567890");
list.insertContact("Aaron","Hayes","2345678901");
list.insertContact("Shaun","Marsh","3456789021");
list.insertContact("Shaun","Hope","4567890213");
list.insertContact("Kelly","Larson","5678902134");
cout<<"List:"<<endl;
list.printContacts();

list.deleteContact("Shaun","Hope");
list.deleteContact("Aaron","Hayes");
list.deleteContact("Shaun","Marsh");
list.deleteContact("Kelly","Bolt");

cout<<"List:"<<endl;
list.printContacts();

return 0;
}

//end of main.cpp

Output:

List: Aaron Hayes 2345678901 John Smith 1234567890 Kelly Larson 5678902134 Shaun Hope 4567890213 Shaun Marsh 3456789021 List:

Add a comment
Know the answer?
Add Answer to:
linked list operation /*************************************************************************************** This function creates a new node with the information give as a...
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
  • 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...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • 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...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode ha...

    Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode has its unique function member GetEmail() which returns the string variable "studentEmail". TtuStudentNode has its overriding "PrintContactNode()" which has the following definition. void TtuStudentNode::PrintContactNode() { cout << "Name: " << this->contactName << endl; cout << "Phone number: " << this->contactPhoneNum << endl; cout << "Email : " << this->studentEmail << endl; return; } ContactNode.h needs to have the function...

  • Hello I need some help with my CC+ project. My current project erasing the first two...

    Hello I need some help with my CC+ project. My current project erasing the first two phone numbers and only printing the 3rd phone number out in the list. Any idea on how to correct. Here is the error. Contacts.h #ifndef Contact_H #define Contact_H #include<string> using namespace std; class ContactNode{ public: ContactNode(); ContactNode(string name, string phone); void InsertAfter(ContactNode*); string GetName(); string GetPhoneNumber(); ContactNode* GetNext(); void PrintContactNode(); private: string contactName; string contactPhoneNum; ContactNode* nextNodePtr; }; #endif Contacts.cpp #include <iostream> #include "Contacts.h"...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot...

    USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot be viewed from outside of the private class. //only the class functions within the class can access private members. private: string name; string email; string phone; //the public class is accessible from anywhere outside of the class //however can only be within the program. public: string getName() const { return name; } void setName(string name) { this->name = name; } string getEmail() const {...

  • #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...

    #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {     Contact item;     NodePtr next;     friend class ContactList; }; class ContactList { public:     ContactList(char* clfile) ;     ~ContactList();     void display       (ostream & output) const;     int   insert        (Contact record_to_insert);     int   insert        (ContactList contact_list);     int   remove        (Contact record_to_delete);     int   size          () const;     int   save          () const;     void find_by_lname (ostream & output, string lname) const;     void...

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