Question

#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 find_by_fname (ostream & output, string fname) const;

    void find (ostream & output, Contact record_to_find ) const;

    int   make_empty    ();

private:

    NodePtr   head;

    int       num_items;

    char      clfile_pathname[512];

    bool      find( Contact item, NodePtr & where );

    bool      insert_at( NodePtr & prev, Contact new_item );  

};

#include <fstream>

#include <stdlib.h>

#include <string.h>

#include "contactlist.h"

using namespace std;

bool ContactList::insert_at( NodePtr & prev, Contact new_item )

{

    NodePtr tmpPtr = new Node;

    if ( tmpPtr == NULL )

        return false;

    tmpPtr->item = new_item;

    tmpPtr->next = prev;

    prev = tmpPtr;

    return true;  

}

ContactList::ContactList(char* clfile)

{

    ifstream fin;

    char line[512];

    Contact temp;

    num_items = 0;

    head = NULL;

    strcpy(clfile_pathname, clfile);

    fin.open(clfile);

    if ( fin.fail()) {

        throw "error";

    }

    while ( !fin.eof() ) {

        fin.getline(line, 511);

        if ( fin.bad() )

            throw "error";

         else if ( 0 == temp.set(line) )

            continue;

        else {

            insert(temp);

        }

    }

}

ContactList::~ContactList()

{

    make_empty();

}

void ContactList::find_by_lname (ostream & output,string lname) const

{

    NodePtr current;

    Name    tempname;

    if ( NULL == head )

        return;

    current = head;

    while ( current != NULL ) {

        current->item.get_name(tempname);

        if ( tempname.last() < lname ) {

            current = current->next;

        }

        else if ( tempname.last() > lname ) {

            return;

        }

        else {

            output << current->item;

            current = current->next;

        }

    }

}

void ContactList::find_by_fname (ostream & output, string fname) const

{

    NodePtr current;

    Name    tempname;

    if ( NULL == head )

        return;

    current = head;

    while ( current != NULL ) {

        current->item.get_name(tempname);

        if ( tempname.first() != fname ) {

            current = current->next;

        }

        else {

            output << current->item;

            current = current->next;

        }

    }

}

void ContactList::find (ostream & output, Contact record_to_find ) const

{

    NodePtr current;

    if ( NULL != head ) { current = head;

        while ( current != NULL && !(current->item > record_to_find)) {

            if ( match(current->item, record_to_find) ) {

                output << current->item << "\n";

            }

            current = current->next;

        }

    }

}

int ContactList::remove( Contact item )

{

    NodePtr current;

    NodePtr prev;

    int     count = 0;

    if ( NULL != head ) {

        current = head;

        prev = NULL;

        while ( current != NULL ) {

            if ( match(current->item, item) ) {

                if ( NULL == prev ) {

                    head = current->next;

                    current->next = NULL;

                    delete current;

                    current = head;

                    count++;

                }

                else {

                    current = current->next;   

                    prev->next->next = NULL;   

                    delete prev->next;         

                    prev->next = current;      

                    count++;

                }

            }

            else {

                prev = current;

                current = current->next;

            }

        }

    }

    num_items -= count;

    return count;

}

int ContactList::insert( Contact new_item)

{

    NodePtr current, previous;

    bool success = false;

    if ( NULL == head || head->item > new_item ) {

        success = insert_at( head, new_item);

        if (success)

            num_items++;

        return 1;

    }

    if ( head->item == new_item )

        return 0;

    previous = head;      

    current = head->next;

    while ( current != NULL && !success ) {

        if ( current->item < new_item ) {

            previous = current;

            current = current->next;

        }

        else if ( current->item > new_item ) {

            if ( (success = insert_at(previous->next, new_item)) )

                num_items++;

            return 1;

        }

        else

            return 0;

    }   

    if ( (success = insert_at(previous->next, new_item)) )

        num_items++;

    return 1;

}  

int   ContactList::insert (ContactList contact_list)

{

    NodePtr current = contact_list.head;

    int count = 0;

    while ( current != NULL ) { count += insert(current->item);

        current = current->next;

    }

    return count;

}

void ContactList::display ( ostream & output) const

{

    NodePtr current = head;

    while ( current != NULL) {

        cout << current->item << "\n";

        current = current->next;

    }

}

int ContactList::size() const

{

    return num_items;

}

int ContactList::make_empty()

{

    NodePtr temp_ptr;  

    for ( int i = 1; i <= num_items; i++){

        temp_ptr = head;

        head = head->next;

        temp_ptr->next = NULL;

        delete temp_ptr;

    }

    return 1;

}

int ContactList::save() const

{

    ofstream fout;

    char    backupfile[512];

    char    syscmmd[256];

    int     count = 0;

    sprintf(backupfile, "%s.%s", clfile_pathname,"bkp");

    sprintf(syscmmd, "mv -f %s %s", clfile_pathname, backupfile );

    if ( system (syscmmd ) < 0 )

        return -1;

    fout.open(clfile_pathname);

    if ( fout.fail() )

        return -2;

    NodePtr current = head;

    while ( current != NULL) {

        fout << current->item.convert2csv() << "\n";

        current = current->next;

        count++;

    }

    fout.close();

    return count;

}
------------------------------

1.Put the letters of the true statements on the answersheet with the reason.
(a) The ContactList is a linked list data structure.
(b) The ContactList is maintained in sorted order, by last name only.
(c) The ContactList is not in sorted order.
(d) The ContactList is in sorted order using the last name as a first key and the first name as a second key.

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

(a) True . The ContactList is a linked list data structure where Contact nodes are used for insertion and deletion operations.

(c) True. The ContactList is not in sorted order because the insert_at () function is used to insert new Contact node at a particular position and insert() function is used to insert new contact node at the end of the list.

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
#include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...
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
  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

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

  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...

    #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:        int data;        Node* nextNodePtr;           public:        Node(){}               void setData(int d){            data = d;        }               int getData(){            return data;        }               void setNextNodePtr(Node* nodePtr){                nextNodePtr = nodePtr;        }                ...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

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

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

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
Active Questions
ADVERTISEMENT