C++ Program: DeleteNode vs. Erase

Verified

Added on  2019/09/16

|2
|485
|494
Practical Assignment
AI Summary
This practical assignment requires writing a C++ program to compare the performance of the `deleteNode` operation from a custom `NumberList` class (similar to one found in Gaddis' textbook) against the `erase` operation from the Standard Template Library (STL) `list` header file. The student must first clearly state the problem, formulate a hypothesis about which operation will be faster, design an experiment to test the hypothesis (ensuring both lists contain identical data), present the experimental results, and finally draw a conclusion, stating whether the hypothesis was correct and explaining why. The assignment provides links to STL list documentation and includes a sample `NumberList` class and its `deleteNode` function for reference.
Document Page
Write a program in C++ to reach a conclusion as to whether or not the deleteNode operation
found in the Numberlist class (Program 17.4–Gaddis) is faster than the erase operation found in
the Standard Template Library list header file. To come to an accurate conclusion, you need to
make sure that both linked list data structures are made up of the exact same data.
1. Clearly state the problem that you are trying to solve.
2. Hypothesis – What do you believe is the answer?
3. Experiment – Describe how your program tests your hypothesis?
4. Results – What results did your experiment yield?
5. Conclusion – Was your hypothesis correct or incorrect? Explain why or why not.
STL List References –
http://www.cplusplus.com/reference/list/list/erase/
https://msdn.microsoft.com/en-us/library/1fef72t6.aspx
Numberlist class (Program 17.4--Gaddis)
// This program demonstrates the deleteNode member function.
2 #include <iostream>
3 #include "NumberList.h"
4 using namespace std;
5
6 int main()
7 {
8 // Define a NumberList object.
9 NumberList list;
10
11 // Build the list with some values.
12 list.appendNode(2.5);
13 list.appendNode(7.9);
14 list.appendNode(12.6);
15
16 // Display the list.
17 cout << "Here are the initial values:\n";
18 list.displayList();
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
19 cout << endl;
20
21 // Delete the middle node.
22 cout << "Now deleting the node in the middle.\n";
23 list.deleteNode(7.9);
24
25 // Display the list.
26 cout << "Here are the nodes left.\n";
27 list.displayList();
28 cout << endl;
29
30 // Delete the last node.
31 cout << "Now deleting the last node.\n";
32 list.deleteNode(12.6);
33
34 // Display the list.
35 cout << "Here are the nodes left.\n";
36 list.displayList();
37 cout << endl;
38
39 // Delete the only node left in the list.
40 cout << "Now deleting the only remaining node.\n";
41 list.deleteNode(2.5);
42
43 // Display the list.
44 cout << "Here are the nodes left.\n";
45 list.displayList();
46 return 0;
47 }
Program Output
Here are the initial values:
2.5
7.9
12.6
Now deleting the node in the middle.
Here are the nodes left.
2.5
12.6
Now deleting the last node.
Here are the nodes left.
2.5
Now deleting the only remaining node.
Here are the nodes left.
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]