Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- Read in a file (input.txt) that has at least two lines (you can assume 2 lines

ID: 3668676 • Letter: #

Question

- Read in a file (input.txt) that has at least two lines (you can assume 2 lines). Insert each line into a node creating single linked list (using the definitions in node.h). Then delete the first and last lines. Then print out the remaining list to both the screen and a file named output.txt.

Most of this information can be found in your text book, so you do not have to reinvent the wheel....just understand what you are writing. (Names have been changed to force you to read and understand the List Toolkit listed in the textbook.) Please use the names of member variables and member functions in the attached node.h. For this assignment, you may use the <string> STL but please do not use other Standard Template Libraries like <vector>, or <list>.

You shouldn't have to change node.h but if you do, make sure that you document any changes. Your main.cpp should be really quite short, and your node.cpp should be very similar to the one in the book....please don't just cut and paste...try to understand why you are writing lines of code.

Here is the code for node.h

Using node.h to create node.cpp and main.cpp

Explanation / Answer

node.h (modified and updated)

#ifndef NODE_H
#define NODE_H

#include <cstdlib> //needed for NULL
#include <string>
using namespace std;
namespace HW4 {
  
class node {
public:
// Node data type
typedef string dataType;

//Default Constructor
node();
  
//Constructor
node(dataType &newData);
  
//Accessor Functions
dataType getData();
node* getNextPtr();
const node* getNextPtr() const;
  
// Mutator Functions
void setData(node::dataType &newData);
void setNextPtr(node* &newNextPtr);

private:
dataType data;
node* nextPtr;
}; //End of Class

/****************************************
Non-member functions
The Textbook chooses to make these functions instead of member functions. This is a style call. I would have probably made them member functions, but kept them this way to match your book code. Note that since they are not member functions, you will need to use member accessor and mutator functions in your code!
*****************************************/
/*Returns value of the length of the list*/
int listLength(node* headPtr);
  
/*Inserts a single node at the front of a list*/
void insertAtFront(node*& headPtr,node::dataType &newData);
  
/*Inserts a single node just after the pointer sent in the parameter */
void insertAfterNode(node* previous_ptr,node::dataType &newData);
  
/*Removes a node at the front of the list */
void removeAtFront(node*& headPtr);
  
/*Remove a single node just after the pointer sent in the parameter*/
void removeAtNode(node* previousPtr);
  
/*Displays to the stream listed, an entire list from the pointer location to the end of the list*/
void printList(ostream &stream, node* headPtr);
} //End of Namespace

#endif

node.cpp

#include <bits/stdc++.h>
#include "node.h"

using namespace std;
typedef string dataType;

// implementing node methods
HW4::node::node() {
   nextPtr = NULL;
}

HW4::node::node(dataType &newData) {
   nextPtr = NULL;
   data = newData;
}

dataType HW4::node::getData() {
   return data;
}

HW4::node* HW4::node::getNextPtr() {
   return nextPtr;
}

void HW4::node::setData(dataType &newData) {
   data = newData;
}

void HW4::node::setNextPtr(node* &newNextPtr) {
   nextPtr = newNextPtr;
}

int HW4::listLength(node* headPtr) {
   int length = 0;
   node* traverser = headPtr;
   while (traverser != NULL) {
       traverser = traverser -> getNextPtr();
       length = length + 1;
   }
   return length;
}

void HW4::insertAtFront(node* &headPtr, dataType &newData) {
   HW4::node* newNode = new HW4::node();
   newNode -> setData(newData);
   newNode -> setNextPtr(headPtr);
   headPtr = newNode;
}

void HW4::insertAfterNode(node* previousPtr, dataType &newData) {
   if (previousPtr == NULL) {
       cout << "Insertion failed due to NULL previous pointer" << endl;
       return;
   }

   // create new node
   HW4::node* newNode = new HW4::node(newData);

   node* nextOfPrvs = previousPtr -> getNextPtr();
   previousPtr -> setNextPtr(newNode);
   newNode -> setNextPtr(nextOfPrvs);
}

void HW4::removeAtFront(node* &headPtr) {
   if (headPtr == NULL) {
       cout << "Failed to remove from front as head pointer is NULL" << endl;
       return;
   }

   HW4::node* nodeToDelete = headPtr;
   headPtr = headPtr -> getNextPtr();

   // free memory
   delete nodeToDelete;
}

void HW4::removeAtNode(node* previousPtr) {
   if (previousPtr == NULL) {
       cout << "Failed to remove at node: previous pointer is NULL" << endl;
       return;
   }

   if (previousPtr -> getNextPtr() == NULL) {
       cout << "Failed to remove at node: next of previous pointer is NULL" << endl;
       return;
   }

   HW4::node* nodeToDelete = previousPtr -> getNextPtr();

   // next of node to be deleted
   HW4::node* nextToBeDelete = nodeToDelete -> getNextPtr();

   // create link from previous to one skipped node
   previousPtr -> setNextPtr(nextToBeDelete);

   // free memory
   delete nodeToDelete;
}

void HW4::printList(ostream &stream, node* headPtr) {
   while (headPtr != NULL) {
       stream << headPtr -> getData() << endl;
       headPtr = headPtr -> getNextPtr();
   }
}

main.cpp

#include <bits/stdc++.h>
#include "node.h"

using namespace std;
typedef string dataType;

// helper function to delete at end
void removeAtEnd(HW4::node* &headPtr) {

   // if head is the last node
   if (headPtr -> getNextPtr() == NULL) {
       headPtr = NULL;
       return;
   }

   HW4::node* traverser = headPtr;
   HW4::node* previousPtr = NULL;
   while (traverser -> getNextPtr() != NULL) {
       previousPtr = traverser;
       traverser = traverser -> getNextPtr();
   }

   HW4::removeAtNode(previousPtr);
}

// helper function to insert at end
void insertAtEnd(HW4::node* &headPtr, dataType &newData) {
   // if list is empty
   if (headPtr == NULL) {
       headPtr = new HW4::node(newData);
       return;
   }

   // traverse list till end
   HW4::node* traverser = headPtr;
   while (traverser -> getNextPtr() != NULL) {
       traverser = traverser -> getNextPtr();
   }

   HW4::insertAfterNode(traverser, newData);
}

int main() {
   ifstream file("input.txt", ios::in);
   ofstream out("output.txt");

   HW4::node* headPtr = NULL;

   // take input from file and add each line to end of the list
   string str;
   while (getline(file, str)) {
       insertAtEnd(headPtr, str);
   }

   // delete first line
   removeAtFront(headPtr);

   // delete last line
   removeAtEnd(headPtr);

   // print to console
   printList(cout, headPtr);

   // print to "output.txt" file
   printList(out, headPtr);
   return 0;
}