Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/ / Test program to evaluate linked list performance / / Written 1 0 / 4 / 1 9 by Michael Stiber / / #include
Test program to evaluate linked list performance
Written by Michael Stiber
#include
#include
#include
#include
#include
Replace the following include with alternative linked list class. Currently
it test CDLinkedList
#include "CDLinkedList.h
#include "transposelist.h
#include "mtflist.h
using namespace std;
int main
Alter the following declaration to change the linked list class
name.
CDLinkedList theList;
TransposeList theList;
MtfList theList;
const int numValues ;
const int numAccesses ;
Create a linked list of the numbers numValues
for int i numValues ; i ; i
theList.addi;
Reset the traversal counter, just in case
theList.resetTraverseCount;
Now, access the elements randomly many times
int theNumber;
defaultrandomengine generator;
uniformintdistribution uniform numValues ;
normaldistribution normalnumValues numValues ;
As the statistic of comparison, we use a uniform
distribution. For sequential search, even a "smart" algorithm
shouldn't be able to improve performance.
for int i ; i numAccesses; i
Access a random item by value
theNumber uniformgenerator;
asserttheListcontainstheNumber;
cout "Average number of nodes traversed per access uniform:
theList.getTraverseCount doublenumAccesses endl;
Reset the traversal counter.
theList.resetTraverseCount;
We use a normal distribution so that some values are accessed
much more frequently. It will be peaked around numValues and fall off
rapidly above and below. Note that there is some chance of
generating a number outside the legal range, so we test and get a
new number if that happens this is because a uniform
distribution goes to infinity A smart algorithm could in
principle take advantage of the higher frequency of access of
certain items to lower the average access time. On the other hand,
without any "smarts", the mean number of nodes traversed should still
be the mean of the distribution, the same as for the uniform distribution.
for int i ; i numAccesses; i
theNumber ;
do
theNumber intnormalgenerator;
while theNumber theNumber numValues;
asserttheListcontainstheNumber;
cout "Average number of nodes traversed per access normal:
theList.getTraverseCount doublenumAccesses endl;
end LinkedListStats
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