Question
Use the Scientific Method to discover which data structure has a faster Delete operation. For this exercise, you will compare the deleteNode function found in
Use the Scientific Method to discover which data structure has a faster Delete operation. For this exercise, you will compare the deleteNode function found in the NumberList class to the erase function for the vector found in the standard vector header file (#include
Scientific Method:
Question: Which data structure has a faster delete operation?
Hypothesis: [State your Hypothesis]
Experiment: Test your Hypotheses by writing a program.
Conclusion: Was your hypothesis correct?
Communicate your results to the class (Note: For both the Linked List and the Vector, you should assign the same pseudo-random data to it.)
1. deleteNode function in NumberList class
void NumberList::deleteNode(double num)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
Vector Erase Example.cpp
#include
#include
using namespace std;
int main ()
{
vector
// Push the 1st 10 numbers onto the vector
for (int i=1; i<=10; i++)
numbers.push_back(i);
// erase the 6th element
numbers.erase (numbers.begin()+5);
// erase the first 3 elements:
numbers.erase (numbers.begin(),numbers.begin()+3);
cout << "numbers contains:";
for (unsigned i=0; i cout << ' ' << numbers[i]; cout< system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started