For this C++ assignment, we will continue to work with linked lists. Linked lists are the fundamental dynamic data structure. Dynamic means a data structure that can change size (add or remove data) while the program is running. In contrast, arrays are a static data structure - once created the size of an array can't change.
You can find a sample linked list here in List.cpp
The node class in the text is similar to this code and reviewing it may be of assistance in completing the assignment.
Make the following changes to the program:
a. Add at least two additional data elements to the node class. That is currently a node only stores a name and a pointer to the next node in the list. Make it so that each node can store at least two more pieces of data. One piece of data should be an integer called id. The other one(s) is up to you.
b. Modify the setdata() function in the class so that the programmer can set all of the data elements of a node object. (If you want you can create separate set functions for each type of data: setname(), setID(), etc. or have one setdata() function that takes several arguments).
c. Modify the print() function in the node class so that it prints all of the data in a node, not just the name.
d. Add a function called append() to the node class that takes a pointer to a node as an argument and appends the new node to the tail end of a linked list. Hint, one approach is to make append() recursive; have it 'walk' down the list until it funds the NULL value and insert the node there.
e. Add a function called length() to the node class that calculates and returns the length of the linked list - i.e. the number of nodes in the list.
f. Add a function called clear() to the node class that calls the delete operation on every node in a linked list to restore the memory that was being used. Hint, if you begin by deleting the first node, you won't have any way to get to the next node - so that doesn't work.
Write a main() program that demonstrates how each of these functions work.
List.cpp: Simple linked list program to be edited:
------------------------------------------------------------------------------------
#include
#include
using namespace std;
class node{
private:
node *next;
string name;
public:
void setnext(node *n){next = n;} // inline function
void setdata(string n){name = n;} // inline function
void print(); // defined below
};
void node::print(){
cout << name << endl;
if(next != NULL)
next -> print();
}
int main(){
node *head, *temp; // pointers to a node object
temp = new node(); // create a new node object
temp -> setdata("Sally"); // add data
temp -> setnext(NULL); // make the next object Null
head = temp; // make head point to the beginning of the list
temp = new node(); // create another new node object
temp -> setdata("Fred"); // add data
temp -> setnext(head); // 'point' the new object to the beginning of the list
head = temp; // make head point to the new beginning of the list
temp = new node(); // repeat
temp -> setdata("Anne");
temp -> setnext(head);
head = temp;
head->print(); // print the list
}