Question: LAB 11: Introduction To Linked Lists [ sLList.cpp ] Create a program named sLList that will work in a console/command line window. Do NOT submit

LAB 11: Introduction To Linked Lists [ sLList.cpp ] Create a program named sLList that will work in a console/command line window. Do NOT submit until you complete the last step.

STEP 1 of 3: A Simple Linked List Create a program named sLList.cpp that contains 5 objects of a tod structure, creates a linked list with them, and prints them in chronological order. Heres the tod struct you should use:

struct tod { int h; // hour, 0  23 int m; // minute, 0-59 int s; // second 0  59 char d[32]; // description tod* next; // link } 

Use the following coutTod() function as a method to print out to cout the contents of nodes made as tod objects:

void tod::coutTod() { // Function to print times and description in the object.  std::cout << h << ':'; if (m < 10) { std::cout << '0'; std::cout << m << ':'; } else { std::cout << m << ':'; } if (s < 10) { std::cout << '0'; std::cout << s << std::endl; } else { std::cout << s << std::endl;} std::cout << d << std::endl; } 

Make a method todSet(h,m,s,d) that uses four parameter and sets the values for the h, m, s, and d members of the tod object.

Declare and set 5 objects of the above structure in main(), but do not do so in an array -- create separate objects, including these two:

tod noon; noon.todSet(12,0,0,noon); tod midnight; midnight.todSet(0,0,0,midnight); 

The three additional objects should be for lunchtime, suppertime, and bedtime, initialized to values of your choosing.

Create a headPtr pointer in main(), as an object of tod*. Initialize it to the memory address of midnight. This starts the list with the midnight object. E.g.,

tod* headPtr = 0; // created an empty linked list, and using the value 0 to initialize null to the pointer (0 can be used in many C++ compilers to be the value for nullptr). headPtr = &midnight;

Now complete the list by setting the next member of each struct object to its proper value. E.g.:

midnight.next = &noon; // noon follows midnight

Repeat with statements similar to the above statement until every one of the 5 struct objects has its next member initialized. Remember to set the last object's next member to zero, to mark it as the end of the list. E.g.:

bedtime.next = 0; // using the value 0, set the next pointer to null

The initialized start pointer and the 5 objects' next members constitute a chronologically ordered linked list. Print the list in chronological order using a for-loop to traverse the linked list and sent each object individually to a coutTod function. The traversal loop should have the following general form:

for (tod *p=headPtr; p->next!=0; p=p->next) 

where p represents a pointer that moves throughout the linked list and indicates the current link list node being examined in the list.

STEP 2 of 3: Linked List Maintenance Modify sLList.cpp by adding code that adds and removes objects of the tod structure. Every time an object is added or removed, output the modified list. Make these modifications:

Add breakfast. Before the closing brace of the main() program, add a new section of code. In this new code, declare an object called "breakfast", and initialize its h, m, s, and d to data values of your choosing. Add it into the linked list by first setting the next value of the new object to the next value of the preceding object (presumably midnight), and then resetting the next value of the preceding object to the memory address of the new object. Be sure to do these in the right order! Use a cout to print out a string saying that breakfast was added to the list, and output the list.

Add class start. After the code added in step 1 above, add another new section of code. In this new code, declare an object called "class starts", and initialize its h, m, s, and d to data values of your choosing. Add it into the linked list, as above. Use a cout to print a label saying that the new object was added to the list, and output the list.

Add class end. After the code added in step 2 above, add another new section of code. In this new code, declare an object called "class ends", and initialize its h, m, s, and d to data values of your choosing. Add it into the linked list, as above. Use a cout to print a label saying that the new object was added to the list, and output the list.

Remove all mealtimes. After the code added in step 3 above, add another new section of code. In this new code, remove all mealtimes (i.e., breakfast, lunchtime, and suppertime) from the linked list. Use acout to print a label saying that mealtimes were removed from the list, and output the list.

Add mealtimes back in. After the code added in step 4 above, add another new section of code. In that new code, add all mealtimes back into the linked list. Note that their objects already exist, so you just have to reset the appropriate next values. Use cout to print a label saying that mealtimes were added back in, and output the list.

When adding or deleting more than one node, do so ONE AT A TIME. If you add or remove more than one node at at time, you're not doing this right. If you use other than TWO statements to add ONE node, you're not doing this right. If you use other than TWO statements to remove ONE node, you're not doing this right.

STEP 3 of 3: More Linked List Maintenance

Write an outer loop, either a while() or a for() loop, to print the current list and to prompt the user to add another course. Allow the user to quit, in which case you should print the linked list one more time, and exit the program. Inside that outer loop, you will need to use one ore possibly more nested loops. That nested loop is to be a linked list traversal loop. Again, the traversal loop should have the following general form:

 for (tod *p=headPtr; p->next!=0; p=p->next) 

where p represents a pointer that moves throughout the linked list and indicates the current link list node being examined in the list.

If the user elects to add one more course, create a new object with the new keyword. Otherwise, break from the outer loop.

Read the input data from the keyboard for the new object, using the cin.getline function or cin >>

Read input items and store them into the object with its own separate user prompt and its own cin statement. Each item corresponds to a member of the object. For example, an input item for the h member should be read at its own prompt, an input item for the m member should be read at its own prompt, an item for the s member should be read at its own prompt, and an input item for the d member should be read at its own prompt. Be sure to issue cin.ignore(1000, 10); before cin.getline, only if the previously executed cin statement was a cin >> statement -- this processes the ENTER key symbol in the input buffer, which cin >> does not process.

As an alternative, per your choice, your cin >> statements can follow other cin >> statements, without.ignore's, so that all input for one course can be done on a single line, space-separated. But be sure to include a prompt that explains to the user what to do.

Add the newly created object to the front of the linked list. You should use logic similar to this:

newNode->next = headPtr; // adds/links the new node to what is currently at the start of the list headPtr = newNode; // makes the start of the list the new node that was just added 

Before the program terminates, traverse the linked list and deallocate the memory used for each of the objects with the delete keyword (because they were instantiated with the new keyword).

Please do not use global variables.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!