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

write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) {    if (list.find(ch))        cout << "found ";    else        cout << "did not find ";    cout << ch << endl; } int main() {    LinkedList   list;    list.add('x');    list.add('y');    list.add('z');    cout << list;    find(list, 'y');    list.del('y');    cout...
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() {...
Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
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 =...
*In C++ Please! This problem uses the concept of linked list What is a C++ structure?...
*In C++ Please! This problem uses the concept of linked list What is a C++ structure? Store product-id, product-name, and price per unit in a C++ structure. Use a C++ class with a member variable of that structure and provide read and write member functions for the product details. Suppose the product names are stored in alphabetical order, Write the C++ insert function to insert a new product ‘tooth brush’ in that linked list. Suppose the product names are stored...
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...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
PLEASE INCLUDE THE SOURCE CODE AND OUTPUT PLEASE AND THANKS!!This assignment covers recursion and linked list...
PLEASE INCLUDE THE SOURCE CODE AND OUTPUT PLEASE AND THANKS!!This assignment covers recursion and linked list which include the following tasks: 2. a. Using C/C++, construct a single linked list of 8 nodes and assign random numbers as the nodes’ values. Then print the list from the first node to the last. Finally, free all memories of the linked list. b. Using C/C++, construct a single linked list of 8 nodes and assign random numbers as the nodes’ values. Then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT