Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help fixing my code. input1.txt: string head world digital remove tail remove [2] hello print remove tail output: print input2.txt: int tail 23 2

Need help fixing my code.

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 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::print() { node *curr = head;

while(curr != nullptr) { if(curr == nullptr) { cout value; }

else { cout value 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() {

ifstream ifs("input1.txt"); ofstream output("output.txt"); string type = ""; string location = ""; string in = ""; linkedlist listInt; linkedlist listString; linkedlist listChar; ifs >> type; ifs >> location; cout

else if (type == "int"){ if (location == "head"){ if (in == "removetail"){ listInt.removeAtTail(); } else if (in == "removehead"){ listInt.removeAtHead(); } else{ listInt.addAtHead(stoi(in)); } } else{ if (in == "removetail"){ listInt.removeAtTail(); } else if (in == "removehead"){ listInt.removeAtHead(); } else{ listInt.addAtTail(stoi(in)); } } listInt.print(); }

else if (type == "char"){ if (location == "head"){ if (in == "removetail"){ listChar.removeAtTail(); } else if (in == "removehead"){ listChar.removeAtHead(); } else{ listChar.addAtHead(in[0]); } } else{ if (in == "removetail"){ listChar.removeAtTail(); } else if (in == "removehead"){ listChar.removeAtHead(); } else{ listChar.addAtTail(in[0]); } } listChar.print(); }

} 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 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

Spatial Database Systems Design Implementation And Project Management

Authors: Albert K.W. Yeung, G. Brent Hall

1st Edition

1402053932, 978-1402053931

More Books

Students also viewed these Databases questions

Question

5. Identify three characteristics of the dialectical approach.

Answered: 1 week ago

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago