Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My output files keep coming out empty with no errors. please help debug my code to print the following output. input1.txt: string head world digital

My output files keep coming out empty with no errors. please help debug my code to print the following output.

image text in transcribed

input1.txt:

string head world digital remove tail remove [2] hello print remove tail

output:

print

input2.txt:

int tail 23 2 remove [0] 65 5 remove [3] remove [2] 0 45 remove tail 15

output:

2 0 15

input3.txt: char head c remove tail remove tail h w o p e remove head remove tail remove head r

output:

r o w

my code:

#include #include #include #include using namespace std;

template struct node { T data; node *next; };

template class linkedlist { private: node *head; int size;

public: linkedlist(); ~linkedlist(); bool isEmpty() { return head == nullptr; } int getSize() { return size; } void addAtHead(T value); void addAtTail(T value); void removeAtHead(); void removeAtTail(); void addAtPos(T value, int pos); void removeAtPos(int pos); void print(); };

template linkedlist::linkedlist() { head = nullptr; size = 0; }

template linkedlist::~linkedlist() { node *cu = head; while (cu != nullptr) { node *temp = cu; cu = cu->next; delete temp; } }

template void linkedlist::addAtHead(T value) { node *temp = new node(); temp->data = value; temp->next = head; head = temp; size++; }

template void linkedlist::removeAtHead() { if (isEmpty()) return; node *temp = head; head = head->next; delete temp; size--; }

template void linkedlist::addAtTail(T value) { node *temp = new node(); temp->data = value; temp->next = nullptr;

node *cu = head; if (cu == nullptr) { head = temp; } else { while (cu->next != nullptr) { cu = cu->next; } cu->next = temp; } size++; }

template void linkedlist::removeAtTail() { if (isEmpty()) return;

node *cu = head; node *prev = nullptr; if (cu->next == nullptr) { head = nullptr; delete cu; } else { while (cu->next != nullptr) { prev = cu; cu = cu->next; } prev->next = nullptr; delete cu; } size--; } template void linkedlist::removeAtPos(int pos) { if (isEmpty() || pos = size) return; if (pos == 0) { removeAtHead(); return; } if (pos == size - 1) { removeAtTail(); return; } node *cu = head; node *prev = nullptr; int count = 0; while (cu != nullptr && count next; count++; } prev->next = cu->next; delete cu; size--; }

template void linkedlist::print() { node *curr = head;

while(curr != nullptr) { cout data next;

} }

//outputFile *cu = head; while (cu != nullptr) { cout data next; }*/

template void linkedlist::addAtPos(T value, int pos) { node *temp = new node(); temp->data = value; temp->next = nullptr;

node *cu = head; node *prev = nullptr;

if (pos > size) return; else if (pos == 0) { head = temp; temp->next = cu; } else { for (int i = 0; i next; } prev->next = temp; temp->next = cu; } size++; }

int main() { linkedlist linkedlistStr; linkedlist linkedlistInt; linkedlist linkedlistChar; ifstream inFile; ofstream outFile; inFile.open("input1.txt"); outFile.open("output1.txt"); string type; string insertOption; inFile >> type >> insertOption; string value; while (inFile >> value) { if (value == "remove") { string option; inFile >> option; if (option == "head") { if (type == "string") linkedlistStr.removeAtHead(); else if (type == "int") linkedlistInt.removeAtHead(); else linkedlistChar.removeAtHead(); } else if (option == "tail") { if (type == "string") linkedlistStr.removeAtTail(); else if (type == "int") linkedlistInt.removeAtTail(); else linkedlistChar.removeAtTail(); } else { int index = stoi(option.substr(1, option.length() - 2)); if (type == "string") linkedlistStr.removeAtPos(index); else if (type == "int") linkedlistInt.removeAtPos(index); else linkedlistChar.removeAtPos(index); } } else { if (type == "string") { if (insertOption == "head") linkedlistStr.addAtHead(value); else linkedlistStr.addAtTail(value); } else if (type == "int") { if (insertOption == "head") linkedlistInt.addAtHead(stoi(value)); else linkedlistInt.addAtTail(stoi(value)); } else { if (insertOption == "head") linkedlistChar; return 0; } } } }

to get students familiar with Linked List operations and type template. 1. Input files - The first input line will contain a string indicating the data type of the linked list - Options will either be 'string', 'int' or 'char' - Each input file will only contain a single data type - The second input line will indicate how to insert new data into the linked list. - Options will be either 'head' or 'tail'. - If it says 'head', add elements only to the beginning of the linked list for this input file. If it says 'tail', add elements only to the end of the linked list for this input file. - Each input file will only contain a single insert option - Each element will be on its own line. - There are three additional operations: 'remove head', 'remove tail', and 'remove [index]' If the line says 'remove head', remove the first element in the linked list If the line says 'remove tail', remove the last element in the linked list. If the line says 'remove [index], remove the element at [index] in the linked list - If the index is 0 , remove the head of the linkedlist - If the index is equal to or greater than the amount of elements in the linked list, remove the tail of the linkedlist - Otherwise, remove the element at the specified index - Example: linked list currently is hello, computer, science, world - The operation 'remove [1]' would remove computer since its index is 1 - When reading the input, In and should be removed before processing the string. 2. Assumptions - The keywords 'head', 'tail', and 'remove' will never appear as data. - No empty lines, but there can have empty files; empty input files should be empty output files. 3. Output files - The output file should display every element in the linked list on a single line, separated by a space. - Empty input files should result in an empty output file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions