Question
Hello, I need to develop a Linked List that performs several standard operations. I am required to use template classes that accept various types including
Hello, I need to develop a Linked List that performs several standard operations. I am required to use template classes that accept various types including ints, doubles, strings, etc.The linked list must be a single-linked list with an external pointer only to the first node in the list. It should have a head pointer, but not a tail pointer. The predefined C++ Standard Template Library (STL) classes are not allowed. It needs to run on Linux machines. The first section of code I have posted is the main.cpp. The following is a LinkedListInterface.h with the functions I'll be needing to implement. Thanks!
#include
const int NUM_FILES = 5; // the total number of files to be read from
const std::string fileArray[NUM_FILES] = { "file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt" }; // the string aray containing the file names
// This will take a string temp and a LinkedList object and will execute an instruction from the string // no return, but writes the results of the instruction into the ofs filestream template
// This function is a platform independent way of reading files of various line ending types. // It's definiton is at the bottom of the file, don't worry if you don't understand it. namespace ta { std::istream& getline(std::istream& is, std::string& line); }
// to determine the type of LL that will be used //0 means LL of ints, 1 means LL of strings //no other data types will be used bool isString;
int main() {
std::ifstream ifs; // create the stream to read in from the files std::ofstream ofs; // create the output stream to write to an output file std::string temp; // used to store the current instruction LinkedList
for (int i = 0; i < NUM_FILES; i++) { ifs.open(fileArray[i]); // open the file to read from ofs.open("out_" + fileArray[i]); // open the file to write to
if (!ifs.is_open()) { // if the file did not open, there was no such file std::cout << "File " << i + 1 << " could not open, please check your lab setup" << std::endl; } else { std::cout << "Reading file" << i + 1 << ".txt..." << std::endl; }
ta::getline(ifs, temp); // get the first instruction if (temp == "STRING") { // first line of file determines the type of the linked list, either string or int isString = true;//set the isString to true sLL = new LinkedList
std::cout << "Beginning out_file" << i + 1 << ".txt write" << std::endl; while (ta::getline(ifs, temp)) { // while there are more instructions to get, try { if (isString) { parse_instruction(temp, ofs, sLL); // parse the instructions using string LinkedList } else { parse_instruction(temp, ofs, iLL); // parse the instructions using int LinkedList } } catch (...) { std::cout << "An exception was thrown that shouldn't have been. "; //return 0;//end the program } } std::cout << "File write complete" << std::endl << std::endl; if (iLL != NULL) { delete iLL; iLL = NULL; } if (sLL != NULL) { delete sLL; sLL = NULL; } ifs.close(); ofs.close(); } std::cout << "end" << std::endl; // indicate that the program has successfuly executed all instructions getchar(); return 0; }
template
// Version of getline which does not preserve end of line characters namespace ta { std::istream& getline(std::istream& in, std::string& line) { line.clear();
std::istream::sentry guard(in, true); // Use a sentry to maintain the state of the stream std::streambuf *buffer = in.rdbuf(); // Use the stream's internal buffer directly to read characters int c = 0;
while (true) { // Continue to loop until a line break if found (or end of file) c = buffer->sbumpc(); // Read one character switch (c) { case ' ': // Unix style, return stream for further parsing return in; case ' ': // Dos style, check for the following ' ' and advance buffer if needed if (buffer->sgetc() == ' ') { buffer->sbumpc(); } return in; case EOF: // End of File, make sure that the stream gets flagged as empty in.setstate(std::ios::eofbit); return in; default: // Non-linebreak character, go ahead and append the character to the line line.append(1, (char)c); } } } }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma once #include
using namespace std;
template
public:
LinkedListInterface(void) {}; virtual ~LinkedListInterface(void) {};
/* insertHead
A node with the given value should be inserted at the beginning of the list.
Do not allow duplicate values in the list. */ virtual void insertHead(T value) = 0;
/* insertTail
A node with the given value should be inserted at the end of the list.
Do not allow duplicate values in the list. */ virtual void insertTail(T value) = 0;
/* insertAfter
A node with the given value should be inserted immediately after the node whose value is equal to insertionNode.
A node should only be added if the node whose value is equal to insertionNode is in the list. Do not allow duplicate values in the list. */ virtual void insertAfter(T value, T insertionNode) = 0;
/* remove
The node with the given value should be removed from the list.
The list may or may not include a node with the given value. */ virtual void remove(T value) = 0;
/* clear
Remove all nodes from the list. */ virtual void clear() = 0;
/* at
Returns the value of the node at the given index. The list begins at index 0.
If the given index is out of range of the list, throw an out of range exception. */ virtual T at(int index) = 0;
/* size
Returns the number of nodes in the list. */ virtual int size() = 0;
/* toString Returns a string representation of the list, with the value of each node listed in order (Starting from the head) and separated by a single space There should be no trailing space at the end of the string
For example, a LinkedList containing the value 1, 2, 3, 4, and 5 should return "1 2 3 4 5" */ virtual string toString() = 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