Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSE 112 - Introduction to Computer Science II CSE 112 - Lab # 10 Linked List using the STL list container - Linked Lists

CSE 112 - Introduction to Computer Science II CSE 112 - Lab # 10 Linked List using the STL list container - Linked Lists Requirements: Using the STL list container, create the list on page 1157 (18-6), and modify the program as described below. Define a list object, and populate it using a loop as shown in 18-6. Call a function to display the list and output the number of nodes in the list. Then, call a function to add to the list the numbers (5, 15, 25, 35, 45, 55, 65, 75, 85) inserting them in numerical order using a loop, an iterator, list.begin, and list.insert. Then call the display function to display the list again and the number of nodes. Then reverse the list and display it again, and output the number of nodes. Then call a function that removes the ends of the list, and then redisplay the list and the number of nodes. . Use an iterator in the solution for adding the fives and for output at a minimum. All output is to be done in the output function. The output function that displays the list must accept a string for the message prior to displaying the list and the number of nodes. Sample output function declaration: void display_list(list &myList, string message); display_list(myList, "The original list is: "); Sample output function call: Output Format and contents: The original list is: 0 10 20 30 40 50 60 70 80 90 The number of list nodes is: 10 The list with the additional numbers is: 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 The number of list nodes is now: 19 The list in reverse is: 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 The number of list nodes is: 19 The list with the ends removed: 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 The number of list nodes is: 17 Process returned 0 (0x0) execution time: 0.062 s Press any key to continue. Grading will be based on meeting all of the lab requirements, adherence to programming standards, operation and accurate output, and programming style including white space and indentation. The grade penalty is 5% for each standards violation or missing requirement. Program 18-6 demonstrates some simple operations with the 11st container. Program 18-6 1 // This program demonstrates the STL list container. 2 #include 3 #include 4 using namespace std; 5 6 int main() 7 ( 8 // Define an empty list. 9 list myList; 11 Add some values to the list. for (int x = 0; x < 100; x + 10) nyList.push_back (x): // Use an iterator to display the values. for (auto it cout < < it < < **; cout < < endl; 10 11 12 13 14 15 16 17 18 19 1158 Chapter 18 Linked Lists C a myList.begin(): it ! myList.end(); it++) Program 18-6 (continued) 2222 (program continues) 1/ Now reverse the order of the elements. myList.reverse(); // Display the values again, with a range-based for loop myList) cout < < element < < " "; The forward_list Container Because a forward list is implemented as a singly linked list, cach node keeps only one pointer: a pointer to the next node. Compared to a list container, which keeps two point- ers per node (one to the next node, and one to the previous node), a forward_list uses less memory than a list. However, you can step only forward through a forward_list. If you need to move both forward and backward, you will need to use a list container. The forward list container provides most of the same member functions as the 11st con- tainer, with a few exceptions. You can find documentation for the forward_list container with a good online reference site, such as www.cppreference.com or www.cplusplus.com. 24 for (auto element 25 26 cout < < endl; 27 28 return 0; 29 ) Program Output 0 10 20 30 40 50 60 70 80 90 90 80 70 60 50 40 30 20 10 0 C

Step by Step Solution

3.46 Rating (159 Votes )

There are 3 Steps involved in it

Step: 1

C program include incl... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Accounting questions

Question

Demonstrate how an ethical culture can be created.

Answered: 1 week ago