Question

This is a C++ program about link list, please help me complete the codes, thanks!(the .h...

This is a C++ program about link list, please help me complete the codes, thanks!(the .h file has been provided)

/*
 * List.h
 * 
 * Class Description: List data collection ADT.
 * Class Invariant: Data collection with the following characteristics:
 *                   - Each element is unique (no duplicates).
 *                   - (What other characteristic does our List have?)
 *
 * 
 * 
 */

#pragma once

// You can add #include statements if you wish.
#include <string>
#include "Patient.h"

using namespace std;


class List  {

private:

/* 
 * You can add more attributes to this class, 
 * but you cannot remove the attributes below 
 * nor can you change them.
 */

        static const int MAX_ELEMENTS = 5; // Small capacity so can test when data collection becomes full
                                           // ***As we are testing the code of our assignment, we can  
                                           //    change the value given to this constant.***
        Patient elements[MAX_ELEMENTS];    // Data structure with capacity of MAX_ELEMENTS
        int elementCount;                  // Current element count in element array
        int capacity;                      // Actual maximum capacity of element array   

public:

/* 
 * You can add more methods to this interface, 
 * but you cannot remove the methods below 
 * nor can you change their prototype.
 * 
 */

        // Default constructor
        List();

        // Description: Returns the total element count currently stored in List.
        int  getElementCount() const;

        // Description: Insert an element.
        // Precondition: newElement must not already be in data collection.  
        // Postcondition: newElement inserted and elementCount has been incremented.   
        bool insert(const Patient& newElement);

        // Description: Remove an element. 
        // Postcondition: toBeRemoved is removed and elementCount has been decremented. 
        bool remove( const Patient& toBeRemoved );
        
        // Description: Remove all elements.
        void removeAll();
   
        // Description: Search for target element.
        //              Returns a pointer to the element if found,
        //              otherwise, returns NULL.
        Patient* search(const Patient& target);
   

}; // end List.h
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Please upvote for the answer.

//Patient.h

#pragma once

#include <string>

using namespace std;

class Patient

{

public:

Patient();

Patient(string name);

~Patient();

private:

string m_strName;

public:

void SetName(string str);

string GetName();

bool operator==(const Patient& target);

};

//Patient.cpp

#include "Patient.h"

Patient::Patient()

{

}

Patient::Patient(string name)

{

m_strName = name;

}


Patient::~Patient()

{

}

void Patient::SetName(string str)

{

m_strName = str;

}

string Patient::GetName()

{

return m_strName;

}

bool Patient::operator==(const Patient & target)

{

bool isSame = (m_strName == target.m_strName);

return isSame;

}



//List.h

#pragma once

/*

* List.h

*

* Class Description: List data collection ADT.

* Class Invariant: Data collection with the following characteristics:

*                   - Each element is unique (no duplicates).

*                   - (What other characteristic does our List have?)

*

*

*

*/

#pragma once

// You can add #include statements if you wish.

#include <string>

#include "Patient.h"

using namespace std;


class List {

private:

/*

* You can add more attributes to this class,

* but you cannot remove the attributes below

* nor can you change them.

*/

static const int MAX_ELEMENTS = 5; // Small capacity so can test when data collection becomes full

  // ***As we are testing the code of our assignment, we can

  //   change the value given to this constant.***

Patient elements[MAX_ELEMENTS];    // Data structure with capacity of MAX_ELEMENTS

int elementCount;                  // Current element count in element array

int capacity;                      // Actual maximum capacity of element array   

public:

/*

* You can add more methods to this interface,

* but you cannot remove the methods below

* nor can you change their prototype.

*

*/

// Default constructor

List();

// Description: Returns the total element count currently stored in List.

int getElementCount() const;

// Description: Insert an element.

// Precondition: newElement must not already be in data collection.

// Postcondition: newElement inserted and elementCount has been incremented.   

bool insert(const Patient& newElement);

// Description: Remove an element.

// Postcondition: toBeRemoved is removed and elementCount has been decremented.

bool remove(const Patient& toBeRemoved);

// Description: Remove all elements.

void removeAll();

// Description: Search for target element.

//              Returns a pointer to the element if found,

//              otherwise, returns NULL.

Patient* search(const Patient& target);

void DisplayList();

}; // end List.h

//List.cpp

#include "List.h"

#include<iostream>

#include<string>

using namespace std;

List::List()

{

elementCount = 0;

}

int List::getElementCount() const

{

return elementCount;

}

bool List::insert(const Patient & newElement)

{

if (elementCount >= MAX_ELEMENTS)

{

cout << "Element can't be inserted" << endl;

return false;

}

elements[elementCount] = newElement;

elementCount++;

}

bool List::remove(const Patient & toBeRemoved)

{

bool isElementFound = false;

int i = 0;

for ( i = 0;i < elementCount && !isElementFound;i++)

{

if (elements[i] == toBeRemoved)

{

//element found in the array.

isElementFound = true;

}

}

if (isElementFound)

{

cout << "Patient - " << elements[i].GetName() << " Will be removed" << endl;

for (--i;i < elementCount-1;i++)

{

elements[i] = elements[i + 1];

}

elementCount -= 1;

}

return false;

}

void List::removeAll()

{

elementCount = 0;

}

Patient * List::search(const Patient & target)

{

for (int i = 0;i < elementCount;i++)

{

if (elements[i] == target)

{

return &elements[i];

}

}

return NULL;

}

void List::DisplayList()

{

cout << "Paitents count: " << elementCount << endl;

for (int i = 0;i < elementCount;i++)

cout << elements[i].GetName()<<endl;

cout << endl;

}

//Main Program

#include "List.h"

#include "Patient.h"

#include<iostream>

using namespace std;

int main()

{

List obj;

cout<<"Initial Count" << obj.getElementCount() << endl;

Patient p1("John");

Patient p2("Mary");

Patient p3("David");

Patient p4("Stark");

Patient p5("keith");

Patient p6("Tom");

obj.insert(p1);

obj.insert(p2);

obj.DisplayList();

obj.insert(p3);

obj.insert(p4);

obj.insert(p5);

obj.DisplayList();

obj.insert(p6);

obj.remove(p4);

obj.DisplayList();

obj.insert(p6);

obj.DisplayList();

Patient* p = obj.search(p3);

if (p != NULL)

cout << "patient search result- " << p->GetName() << endl;

p = obj.search(p4);

if (p == NULL)

cout << "Patient not found" << endl;

obj.removeAll();

obj.DisplayList();

}

//sample output

Add a comment
Know the answer?
Add Answer to:
This is a C++ program about link list, please help me complete the codes, thanks!(the .h...
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
  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

    The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...

  • The purpose of this program is to help reinforce container class concepts and linked list concepts...

    The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible. Please make sure code can pass any tests. sequence3.h // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is...

  • Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every...

    Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every member function must include a description in the header file including precondition and postcondition. Rule 3: every member variable must include a description of what the variable holds. Rule 4: Classes should be capitalized. Bag, not bag. Person, not person. (I know this is different than how the book does it) Rule 5: member variables of classes should be preceded by “m_”. If it’s...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

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