Currently working on a project and I would appreciate all the help that I can get.
Here is the first part of the assignment:
Instructions:
Here is what I have so far, this is the StringList.h file.. Everything seems to be coming back error free.Code:In this file, you declare a class named StringList. – StringList is a modified version of NumberList class (Chapter 17), that is designed to store C++ strings in a linked list. – Therefore, each node must store a string, not a number. – The node structure must be declared within class, and a private attribute head points to the starting node. – The class provides a default constructor that creates an empty linked list, and a destructor that releases all the nodes. – The class also provides public member functions for inserting and deleting a node (see below for details). – It also provides a public member function for displaying the list.
Code:#ifndef STRINGLIST_H #define STRINGLIST_H #include <iostream> #include <string> #include <string.h> #include <stdio.h> using namespace std; class StringList { private: //Declare a structure for the list struct listNode { string name; string a; struct listNode *next; }; //Points to the starting Node listNode *head; public: //Constructor StringList() { head = NULL; } //Destructor ~StringList(); //List Operations void insertNode(string a); void deleteNode(string a); void displayList() const; void deleteFront(); void deleteBack(); void clear(); }; #endif
Here is the second part of the assignment, this I'm extremely confused about.
Code:In this file, you provide definitions for the default constructor and the destructor for StringList. – Make sure the destructor visits every node in the list and deletes every one of them from the heap. – Define insertFront function to insert a new node into the front of the list. Therefore, the new node will be the new first node in the list. This function takes a string as a parameter. – Define insertBack function to insert a new node into the back of the list. Therefore, the new node will be the new last node in the list. This function takes a string as a parameter. – Define deleteFront function to delete the first node from the list. This function takes no parameter. – Define deleteBack function to delete the last node from the list. This function takes no parameter. – Define display function that displays the current contents (strings) of the list (display strings in a single line, separated by a space). This function takes no parameter.
Here is what I have so far, I would appreciate if someone can guide in the right direction and teaching me how to define those four functions. Here is my StringList.cpp
Code:#include <iostream> #include "StringList.h" using namespace std; //Constructor definition StringList::StringList() { head = nullptr; } //Destructor definition StringList::~StringList() { delete head; clear(); } //deleteFront StringList::deleteFront() { if(head != NULL) { listNode *tmp = head -> next; delete head; head = tmp; } } StringList::displayList() { listNode *nodePtr; if(!head) { cout << "List is empty"; return; } nodePtr = head; cout << "Here is the list: \n\t"; while (nodePtr) { cout << nodePtr -> a << " -> "; } nodePtr = nodePtr -> next; }




Reply With Quote
