Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//================= // Name : LinkedList.cpp // Author : Your Name // Version : 1.0 // Copyright : Copyright 2017 SNHU COCE // Description : Lab

//================= // Name : LinkedList.cpp // Author : Your Name // 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: //Internal structure for list entries, housekeeping variables struct Node { Bid bid; Node *next;

// default constructor Node() { next = nullptr; }

// initialize with a bid Node(Bid aBid) { bid = aBid; next = nullptr; } };

Node* head; Node* tail; int size = 0;

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 (1): Initialize housekeeping variables //set head and tail equal to null }

/** * Destructor */ LinkedList::~LinkedList() { // start at the head Node* current = head; Node* temp;

// loop over each node, detach from list then delete while (current != nullptr) { temp = current; // hang on to current node current = current->next; // make current the next node delete temp; // delete the orphan node } }

/** * Append a new bid to the end of the list */ void LinkedList::Append(Bid bid) { // FIXME (2): Implement append logic //Create new node //if there is nothing at the head... // new node becomes the head and the tail //else // make current tail node point to the new node // and tail becomes the new node //increase size count }

/** * Prepend a new bid to the start of the list */ void LinkedList::Prepend(Bid bid) { // FIXME (3): Implement prepend logic // Create new node

// if there is already something at the head... // new node points to current head as its next node

// head now becomes the new node //increase size count

}

/** * Simple output of all bids in the list */ void LinkedList::PrintList() { // FIXME (4): Implement print logic // start at the head

// while loop over each node looking for a match //output current bidID, title, amount and fund //set current equal to next }

/** * Remove a specified bid * * @param bidId The bid id to remove from the list */ void LinkedList::Remove(string bidId) { // FIXME (5): Implement remove logic // special case if matching node is the head // make head point to the next node in the list //decrease size count //return

// start at the head // while loop over each node looking for a match // if the next node bidID is equal to the current bidID // hold onto the next node temporarily // make current node point beyond the next node // now free up memory held by temp // decrease size count //return

// curretn node is equal to next node }

}

/** * Search for the specified bidId * * @param bidId The bid id to search for */ Bid LinkedList::Search(string bidId) { // FIXME (6): Implement search logic

// special case if matching node is the head // make head point to the next node in the list //decrease size count //return

// start at the head of the list

// keep searching until end reached with while loop (next != nullptr // if the current node matches, return it // else current node is equal to next node //return bid }

/** * 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

/** * Prompt user for bid information * * @return Bid struct containing the bid info */ Bid getBid() { Bid bid;

cout

cout

cout > bid.fund;

cout

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

// initialize the CSV Parser csv::Parser file = csv::Parser(csvPath);

try { // loop to read rows of a CSV file for (int i = 0; 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

// add this bid to the end list->Append(bid); } } catch (csv::Error &e) { std::cerr

/** * 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 > choice;

switch (choice) { case 1: bid = getBid(); bidList.Append(bid); displayBid(bid);

break;

case 2: ticks = clock();

loadBids(csvPath, &bidList);

cout

ticks = clock() - ticks; // current clock ticks minus starting clock ticks cout

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

cout

break;

case 5: bidList.Remove(bidKey);

break; } }

cout

return 0; }

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

The focus of these problems will be working with information extracted from a municipal government data feed containing bids submitted for auction of property. All materials for this assignment can be found in the Supporting Materials section below. The data set is provided in two comma-separated files: 1. eBid_Monthly_Sales.csv (larger set of 12,023 bids) 2. eBid_Monthly_Sales_Dec_2016.csv (smaller set of 76 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 provide a starter console program that uses a menu to enable testing of the sort 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.cpp that you have to complete: public: LinkedList(); virtual LinkedList(); void Append(Bid bid); void Prepend(Bid bid); void PrintList(); void Remove(string bidld); Bid Search(string bidld); void Append(Bid bid); void Prepend(Bid bid); void PrintList(); void Remove(string bidld); Bid Search(string bidld); Prompt You will need to perform the following steps to complete this activity: Setup: Begin by creating a new C++ project with a project type of "Hello World C++ Project". a. Name the project "LinkedList". Remember to pick the correct compiler in Toolchains and click Finish. This will create a simple LinkedList.cpp source file under the /src directory. b. Download the starter program files and copy them to the project's /src directory, replacing the existing auto-generated ones. 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 file: to the src folder underneath. c. Because this activity uses C++11 features, you may need to add the -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 code to append a bid to the end of the list. Task 4: Implement prepend logic. Create code to prepend a bid to the front of the list. Task 5: Implement print logic. Create code to print all the bid entries in the list to the console. Task 6: Implement remove logic. Create code to remove the requested bid using the bid ID passed in. Task 7: Implement search logic. Create code 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. \begin{tabular}{|c|c|c|} \hline Example Input & Choice: 4 & Choice: 9 \\ \hline \end{tabular} - Code Reflection: A brief explanation of the code and its purpose, and a brief discussion of your experience in developing it, including any issues that you encountered while completing the exercise and what approaches you took to solve them - Pseudocode or Flowchart: A pseudocode or flowchart description of the code that is clear and understandable and captures accurate logic to translate to the programming language - Specifications and Correctness: Source code must meet its specifications and behave as desired. Correct code produces the correct output as defined by the data and problem; however, you should also produce fully functioning code (with no errors) that aligns with as many of the specifications as possible. You should write your code in such a way that the submitted file executes, even if it does not produce the correct output. You will be given credit for partially correct output that can be viewed and seen to be partially correct. - Annotation / Documentation: All code should also be well-commented. This is a practiced art that requires striking a balance between commenting everything, which adds a great deal of unneeded noise to the code, and commenting nothing. Well-annotated code requires you to: - Explain the purpose of lines or sections of your code, detailing the approach and method you took to achieve a specific task in the code. - Document any section of code that is producing errors or incorrect results. - Modular and Reusable: Programmers should develop code that is modular and reusable. If it contains functionality and responsibility in distinct methods, code is more flexible and maintainable. Your code should adhere to the single responsibility principle-classes and methods should do only one job. If you can replace a method with another that uses a different technique or implementation without impacting (having to refactor or rewrite) other parts of your code, then you have succeeded in creating modular methods. - Readability: Code needs to be readable to a knowledgeable programmer. In this course, readable code requires: - Consistent, appropriate whitespace (blank lines, spaces) and indentation to separate distinct parts of the code and operations - Explicit, consistent variable names, which should clearly indicate the data they hold and be formatted consistently: for example, numOrders (camelCase) or item_cost (underscored) - Organized structure and clear design that separates components with different responsibilities or grouping-related code into blocks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago