/* * This is a skeleton class that should serve as a guide for building a linked list. * */ //The ususal include directives that we need. #include using namespace std; //Here is the declaration for the linked list class. notice that it contains a nested class which //is for a node object. class LinkedList { private: //This nested class allows for the a LinkedList object to have access to the node class without //declaring it as a separate public class in a different program, which would violate data //encapsulation principles (since we don't want to be able to modify our list from outside the //program). If you want to learn more about exactly how this works see a C++ data structures //book, otherwise just trust that this will work. class Node { public: char data; Node *next; }; //We also declare a pointer that will point to the first node in the list once it is created. //You can think of it as keepinf track of whether the list is empty or has some head node. Node *first; //Declarations for the functions you will make this week. public: LinkedList(); void insert(char a); void print(); ~LinkedList(); }; int main() { //You should test your linked list here. } LinkedList::LinkedList() { //Constructor, all this should do is set first. } //This is the method for inserting data into the list. For now, regardless of whether the data is //already in some node, we will make a new node for it (so if we already have 'a' as the data for //some node in the list insert('a') will make a second node with 'a' as its data). We will revise //this in the final project. The algorithm will be similar to when you created a list in data //structures. void LinkedList::insert(char a) { //First create a pointer to a a new node, and set all the variables appropriately. Hint: you //will need to use the new command here to allocate memory for the new node because if you do //not then the new node you created will go out of scope (and thus not be accessible) at the //end of the funciton. //Next place the node in the correct poisiton in the list. The algorithm should be famiiar from //data structures. } //This function prints the data in the nodes of the list in order. The algorithm should be the same //as what you used when you printed the entries a linked list in data structures. Hint you will not //need to worry about new or delete declarations here. Also,make sure it does something intelligent //if there are no nodes in the list. void LinkedList::print(){ } //This is the destructor of the list (the syntax shoul be familiar from chapter 15). This function //is EXTREMELY important because it is where you will delete all the new nodes you create in the //insert method. If these nodes are not deleted, then you will slowly leak memory everytime you run //the program. Hint: when deleting the nodes delete from the beginning of the list, but be careful //about what first then needs to point to. LinkedList::~LinkedList() { }