Question
Lab 3-2 Linked List Requirement Details The focus of these problems will be working with information extracted from a municipal government data feed containing bids
Lab 3-2 Linked List Requirement Details
The focus of these problems will be working with information extracted from a municipal government data feed containing bids submitted for auction of property. The data set is provided in two comma-separated files:
- eBid_Monthly_Sales.csv (larger set of 17,937 bids)
- eBid_Monthly_Sales_Dec_2016.csv (smaller set of 179 bids)
This assignment is designed to explore linked lists so you will implement a singly linked-list to hold a collection of bids loaded from a CSV file. We will build a simple console program that uses a menu to enable testing of the logic you will complete. In this version the following menu is presented when the program is run:
Menu:
1. Enter a Bid
2. Load Bids
3. Display All Bids
4. Find Bid
5. Remove Bid
9. Exit
Enter choice:
The LinkedList.cpp program is partially completed - it contains empty methods representing the programming interface used to interact with the linked list. You will need to add logic to the methods to implement the necessary behavior. Here is the public API for LinkedList that you have to complete:
public:
LinkedList();
virtual ~LinkedList();
void Append(Bid bid);
void Prepend(Bid bid);
void PrintList();
void Remove(string bidId);
Bid Search(string bidId);
You will need to perform the following stepsfor this activity:
Setup: Begin by creating a new C++ Project with a Project Type of "Hello World C++ Project"
- Name the project 'LinkedList', remember to pick the correct compiler in Toolchains and click Finish. This will make asimple LinkedList.cpp source file under the /src directory.
- Download the starter program files and copy them to the project's /src directory, replacing the existing auto-generated one. Remember to right-click on the project in the Project Explorer pane on the left and 'Refresh' the project so it adds all the new files to the src folder underneath.
- Because this activity uses C++ 11 features you must follow the instructions under "C++ Compiler Version" in the C++ Development Installation guide to add -std=c++11 compiler switch to the Miscellaneous settings
Task 1: Internal structure for list entries, housekeeping variables.
Create the structure, internal to the class, that will hold the bid entries. Consider what other variables are needed to help manage the list.
Task 2: Initialize housekeeping variables.
Remember to initialize the housekeeping variables in the constructor.
Task 3: Implement append logic.
Create logic to append a bid to the end of the list.
Task 4: Implement prepend logic.
Create logic to prepend a bid to the front of the list.
Task 5: Implement print logic.
Create logic to print all the bid entries in the list to the console.
Task 6: Implement remove logic.
Create logic to remove the requested bid using the bid Id passed in.
Task 7: Implement search logic.
Create logic to search for the requested bid using the bid Id passed in. Return the bid structure if found or an empty bid structure if not found.
//============================================================================
// Name: LinkedList.cpp
// Author: Karina Shelton
// Version: 1.0
// Copyright: Copyright 2017 SNHU COCE
// Description : Lab 3-3 Lists and Searching
//============================================================================
#include
#include
#include
#include "CSVparser.hpp"
using namespace std;
//============================================================================
// Global definitions visible to all methods and classes
//============================================================================
// forward declarations
double strToDouble(string str, char ch);
// define a structure to hold bid information
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid() {
amount = 0.0;
}
};
//============================================================================
// Linked-List class definition
//============================================================================
/**
* Define a class containing data members and methods to
* implement a linked-list.
*/
class LinkedList {
private:
// FIXME (1): Internal structure for list entries, housekeeping variables
public:
LinkedList();
virtual ~LinkedList();
void Append(Bid bid);
void Prepend(Bid bid);
void PrintList();
void Remove(string bidId);
Bid Search(string bidId);
int Size();
};
/**
* Default constructor
*/
LinkedList::LinkedList() {
// FIXME (2): Initialize housekeeping variables
}
/**
* Destructor
*/
LinkedList::~LinkedList() {
}
/**
* Append a new bid to the end of the list
*/
void LinkedList::Append(Bid bid) {
// FIXME (3): Implement append logic
}
/**
* Prepend a new bid to the start of the list
*/
void LinkedList::Prepend(Bid bid) {
// FIXME (4): Implement prepend logic
}
/**
* Simple output of all bids in the list
*/
void LinkedList::PrintList() {
// FIXME (5): Implement print logic
}
/**
* Remove a specified bid
*
* @param bidId The bid id to remove from the list
*/
void LinkedList::Remove(string bidId) {
// FIXME (6): Implement remove logic
}
/**
* Search for the specified bidId
*
* @param bidId The bid id to search for
*/
Bid LinkedList::Search(string bidId) {
// FIXME (7): Implement search logic
}
/**
* Returns the current size (number of elements) in the list
*/
int LinkedList::Size() {
return size;
}
//============================================================================
// Static methods used for testing
//============================================================================
/**
* Display the bid information
*
* @param bid struct containing the bid info
*/
void displayBid(Bid bid) {
cout << bid.bidId << ": " << bid.title << " | " << bid.amount
<< " | " << bid.fund << endl;
return;
}
/**
* Prompt user for bid information
*
* @return Bid struct containing the bid info
*/
Bid getBid() {
Bid bid;
cout << "Enter Id: ";
cin.ignore();
getline(cin, bid.bidId);
cout << "Enter title: ";
getline(cin, bid.title);
cout << "Enter fund: ";
cin >> bid.fund;
cout << "Enter amount: ";
cin.ignore();
string strAmount;
getline(cin, strAmount);
bid.amount = strToDouble(strAmount, '$');
return bid;
}
/**
* Load a CSV file containing bids into a LinkedList
*
* @return a LinkedList containing all the bids read
*/
void loadBids(string csvPath, LinkedList *list) {
cout << "Loading CSV file " << csvPath << endl;
// initialize the CSV Parser
csv::Parser file = csv::Parser(csvPath);
try {
// loop to read rows of a CSV file
for (int i = 0; i < file.rowCount(); i++) {
// initialize a bid using data from current row (i)
Bid bid;
bid.bidId = file[i][1];
bid.title = file[i][0];
bid.fund = file[i][8];
bid.amount = strToDouble(file[i][4], '$');
//cout << bid.bidId << ": " << bid.title << " | " << bid.fund << " | " << bid.amount << endl;
// add this bid to the end
list->Append(bid);
}
} catch (csv::Error &e) {
std::cerr << e.what() << std::endl;
}
}
/**
* Simple C function to convert a string to a double
* after stripping out unwanted char
*
* credit: http://stackoverflow.com/a/24875936
*
* @param ch The character to strip out
*/
double strToDouble(string str, char ch) {
str.erase(remove(str.begin(), str.end(), ch), str.end());
return atof(str.c_str());
}
/**
* The one and only main() method
*
* @param arg[1] path to CSV file to load from (optional)
* @param arg[2] the bid Id to use when searching the list (optional)
*/
int main(int argc, char* argv[]) {
// process command line arguments
string csvPath, bidKey;
switch (argc) {
case 2:
csvPath = argv[1];
bidKey = "98109";
break;
case 3:
csvPath = argv[1];
bidKey = argv[2];
break;
default:
csvPath = "eBid_Monthly_Sales_Dec_2016.csv";
bidKey = "98109";
}
clock_t ticks;
LinkedList bidList;
Bid bid;
int choice = 0;
while (choice != 9) {
cout << "Menu:" << endl;
cout << "1. Enter a Bid" << endl;
cout << "2. Load Bids" << endl;
cout << "3. Display All Bids" << endl;
cout << "4. Find Bid" << endl;
cout << "5. Remove Bid" << endl;
cout << "9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
bid = getBid();
bidList.Append(bid);
displayBid(bid);
break;
case 2:
ticks = clock();
loadBids(csvPath, &bidList);
cout << bidList.Size() << " bids read" << endl;
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
cout << "time: " << ticks << " milliseconds" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;
case 3:
bidList.PrintList();
break;
case 4:
ticks = clock();
bid = bidList.Search(bidKey);
ticks = clock() - ticks; // current clock ticks minus starting clock ticks
if (!bid.bidId.empty()) {
displayBid(bid);
} else {
cout << "Bid Id " << bidKey << " not found." << endl;
}
cout << "time: " << ticks << " clock ticks" << endl;
cout << "time: " << ticks * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
break;
case 5:
bidList.Remove(bidKey);
break;
}
}
cout << "Good bye." << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started