Question

In: Computer Science

Please complete following c++ code asap using following prototypes complete each missing part / Linked list...

Please complete following c++ code asap

using following prototypes complete each missing part

/ Linked list operations
int getLength() const {return length;}
void insertNode(College);
bool deleteNode(string);
void displayList() const;
bool searchList(string, College &) const;
};

main.cpp

/*
  Build and procees a sorted linked list of College objects.
The list is sorted in ascending order by the college code.
Assume that the college code is unique.
*/
#include <iostream>
#include <fstream>
#include <string>
#include "LinkedList.h"

using namespace std;

void buildList(const string &filename, LinkedList &list);
void deleteManager(LinkedList &list);
void searchManager(const LinkedList &list);
void displayManager(const LinkedList &list);

int main()
{

string inputFileName = "colleges.txt";
LinkedList list;

buildList(inputFileName, list);
displayManager(list);
searchManager(list);
deleteManager(list);
displayManager(list);
return 0;
}

/*****************************************************************************
This function reads data about colleges from a file and inserts them
into a sorted linked list. The list is sorted in ascending order by code
*****************************************************************************/
void buildList(const string &filename, LinkedList &list)
{
ifstream fin(filename);
cout << "Reading data from \"" << filename << "\"";

if(!fin)
{
cout << "Error opening the input file: \""<< filename << "\"" << endl;
exit(EXIT_FAILURE);
}

int rank, cost;
string code, name;

while(fin >> rank)
{
fin >> code;
fin.ignore(); // to ignore space in front of name
getline(fin, name, ';');
fin >> cost;
// create a College object and initialize it with data from file
College aCollege(rank, code, name, cost);
/* Write your code here: call insertNode */
}

fin.close();
}

/*****************************************************************************
Delete manager: delete items from the list until the user enters Q to quit
deleting
Input Parameter: list
*****************************************************************************/
void deleteManager(LinkedList &list)
{
string targetCode = "";

cout << "\n Delete\n";
cout << "=======\n";

while(targetCode != "Q")
{
cout << endl << "Enter a college code (or Q to stop deleting) : \n";
cin >> targetCode;
cout << endl;

if(targetCode != "Q")
{
if(/* Write your code here: call deleteNode */)
cout << targetCode << " has been deleted!\n";
else
cout << "College \"" << targetCode << "\" was not found in this list." << endl;
}
}
cout << "___________________END DELETE SECTION_____\n";
}

/*****************************************************************************
Search manager: search the list until the user enters Q to quit searching
Input Parameter: list
*****************************************************************************/
void searchManager(const LinkedList &list)
{
string targetCode = "";
College aCollege;

cout << "\n Search\n";
cout << "=======\n";

while(targetCode != "Q")
{
cout << "\nEnter a college code (or Q to stop searching) : \n";
cin >> targetCode;

if(targetCode != "Q")
{
if(/* Write your code here: call searchList */)
aCollege.vDisplay();
else
cout << "College \"" << targetCode << "\" was not found in this list." << endl;
}
}
cout << "___________________END SEARCH SECTION _____\n";
}

/*****************************************************************************
Display manager: diplay a header, formatted list content, and footer,
depending on the user's choice;
displays the number of nodes (always)
Input Parameter: list
*****************************************************************************/
void displayManager(const LinkedList &list)
{
string action;
  
cout << "\nDisplay list [Y/N]? ";
cin >> action;

if(action == "Y" || action == "y")
{
cout << "\n====== ==== ============================= =========\n"
<< " Code Rank Name Cost \n"
<< "====== ==== ============================= =========\n";
  
/* Write your code here: call displayList */
  
cout << "====== ==== ============================= =========\n";
}
cout << "Number of colleges in this list: " << /* Write your code here: call getLength */ endl;
}

Solutions

Expert Solution

Linked list operations :

int getLength() const {return length;}
void insertNode(College);
bool deleteNode(string);
void displayList() const;
bool searchList(string, College &) const;

main.cpp :

/*
Build and process a sorted linked list of College objects.
The list is sorted in ascending order by the college code.
Assume that the college code is unique.
*/

#include <iostream>
#include <fstream>
#include <string>
#include "LinkedList.h"

using namespace std;

void buildList(const string &filename, LinkedList &list);
void deleteManager(LinkedList &list);
void searchManager(const LinkedList &list);
void displayManager(const LinkedList &list);

int main()
{

string inputFileName = "colleges.txt";
LinkedList list;

buildList(inputFileName, list);
displayManager(list);
searchManager(list);
deleteManager(list);
displayManager(list);
return 0;
}

/*****************************************************************************
This function reads data about colleges from a file and inserts them
into a sorted linked list. The list is sorted in ascending order by code
*****************************************************************************/


void buildList(const string &filename, LinkedList &list)
{
ifstream fin(filename);
cout << "Reading data from \"" << filename << "\"";

if(!fin)
{
cout << "Error opening the input file: \""<< filename << "\"" << endl;
exit(EXIT_FAILURE);
}

int rank, cost;
string code, name;

while(fin >> rank)
{
fin >> code;
fin.ignore(); // to ignore space in front of name
getline(fin, name, ';');
fin >> cost;

// create a College object and initialize it with data from file
College aCollege(rank, code, name, cost);

list.insertNode(aCollege); // The list object of LinkedList calls insert method to insert College object  in the sorted order of the code
}

fin.close();
}

/*****************************************************************************
Delete manager: delete items from the list until the user enters Q to quit
deleting
Input Parameter: list
*****************************************************************************/

void deleteManager(LinkedList &list)
{
string targetCode = "";

cout << "\n Delete\n";
cout << "=======\n";

while(targetCode != "Q")
{
cout << endl << "Enter a college code (or Q to stop deleting) : \n";
cin >> targetCode;
cout << endl;

if(targetCode != "Q")
{
if(list.deleteNode(targetCode)==true) // The list object of LinkedList calls delete method to delete College object of given code and returns true if code is present // otherwise  returns false
cout << targetCode << " has been deleted!\n";
else
cout << "College \"" << targetCode << "\" was not found in this list." << endl;
}
}
cout << "___________________END DELETE SECTION_____\n";
}

/*****************************************************************************
Search manager: search the list until the user enters Q to quit searching
Input Parameter: list
*****************************************************************************/

void searchManager(const LinkedList &list)
{
string targetCode = "";
College aCollege;

cout << "\n Search\n";
cout << "=======\n";

while(targetCode != "Q")
{
cout << "\nEnter a college code (or Q to stop searching) : \n";
cin >> targetCode;

if(targetCode != "Q")
{
if(list.searchList(targetCode, College(0, targetCode, "", 0))== true) // The list object of LinkedList calls searchlist method for given code and College object
aCollege.vDisplay();
else
cout << "College \"" << targetCode << "\" was not found in this list." << endl;
}
}
cout << "___________________END SEARCH SECTION _____\n";
}

/*****************************************************************************
Display manager: diplay a header, formatted list content, and footer,
depending on the user's choice;
displays the number of nodes (always)
Input Parameter: list
*****************************************************************************/

void displayManager(const LinkedList &list)
{
string action;
  
cout << "\nDisplay list [Y/N]? ";
cin >> action;

if(action == "Y" || action == "y")
{
cout << "\n====== ==== ============================= =========\n"
<< " Code Rank Name Cost \n"
<< "====== ==== ============================= =========\n";
  
list.displayList() // The list object of LinkedList calls the display method to display College objects in the sorted order of the code
  
cout << "====== ==== ============================= =========\n";
}
cout << "Number of colleges in this list: " << /* Write your code here: call getLength */ endl;
}

Explanation :

  • I have assumed that the prototypes of the linked list functions are inbuilt as they are not specified if built or not.
  • list.insertNode(aCollege) :  The list object calls insert method of the LinkedList class to insert provided parameter "aCollege" object of the College type.
  • list.deleteNode(targetCode): The list object calls the delete method of the LinkedList class to delete College having provided parameter "targetCode" of the string type. It returns true if College of the provided code is present and deleted otherwise if College of the provided code is not present or not deleted it returns false.
  • if(list.deleteNode(targetCode)==true) : So if the method list.deleteNode(targetCode) returns true if block is executed otherwise else block is executed.
  • list.searchList(targetCode, College(0, targetCode, "", 0)) : The list object calls searchList method of the LinkedList class to search College having provided parameter "targetCode" of the string type as the first parameter. Here, we have provided second parameter of type College by passing constructor of College Class with dummy values with rank = 0, code = targetCode, cost = 0 , name = "" . It returns true if College of the provided code is present and deleted otherwise if College of the provided code is not present or not deleted it returns false.
  • if(list.searchList(targetCode, College(0, targetCode, "", 0))== true) : So if the method list.searchList(targetCode, College(0, targetCode, "", 0)) returns true if block is executed otherwise else block is executed.
  • list.displayList() : The list object calls display method of the LinkedList class to display details of all Colleges of College type present in the list.

Related Solutions

using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
C++ program Complete the following functions for linked list. You are not allowed to alter the...
C++ program Complete the following functions for linked list. You are not allowed to alter the names or the function prototypes. #ifndef _ULL #define _ULL #include <iostream> #include "nodeType.h" using namespace std; void initializeList(nodeType *&head, nodeType *&tail, int&count); //Initialize the list to an empty state. //Postcondition: head = NULL, tail = NULL, count = 0; bool isEmptyList(const nodeType *head) ; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false. void print(const...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
Modify this linked list code to work with string. Insert the following items into the list...
Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; class ListNode { public:     int value;     ListNode *next;     ListNode(int nodeValue) {       value...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions...
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions below main. 1. Write a function harmonicMeans that repeatedly asks the user for two int values until at least one of them is 0. For each pair, the function should calculate and display the harmonic mean of the numbers. The harmonic mean of the numbers is the inverse of the average of the inverses. The harmonic mean of x and y can be calculated...
Please complete the following list of assignments and upload using this link. Please limit the number...
Please complete the following list of assignments and upload using this link. Please limit the number of files you create to a max of 2, one excel and one word document. P 1-1 FASB Statement of Financial Accounting Concepts No. 2 indicates several qualitative characteristics of useful accounting information. Following is a list of some of these qualities, as well as a list of statements and phrases describing the qualities. a. Benefits > costs b. Decision usefulness c. Relevance d....
Please edit the code with linked lists in C++ Please provide your BookData.txt file as well...
Please edit the code with linked lists in C++ Please provide your BookData.txt file as well BookRecord.cpp file #include "BookRecord.h" #include <stdio.h> #include <string.h> #include <iostream> #include <fstream> using namespace std; BookRecord::BookRecord() {    strcpy_s(m_sName, "");    m_lStockNum = 0;    m_iClassification = 0;    m_dCost = 0.0;    m_iCount = 0; } BookRecord::BookRecord(const char* name, long sn, int cl, double cost) {    strcpy_s(m_sName, name);    m_lStockNum = sn;    m_iClassification = cl;    m_dCost = cost;    m_iCount...
Complete the following program:LinkedQueue.zip package jsjf; /** * Represents a node in a linked list. */...
Complete the following program:LinkedQueue.zip package jsjf; /** * Represents a node in a linked list. */ public class LinearNode<T> {    private LinearNode<T> next;    private T element;    /**    * Creates an empty node.    */    public LinearNode()    {        next = null;        element = null;    }    /**    * Creates a node storing the specified element.    * @param elem element to be stored    */    public LinearNode(T elem)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT