Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ * Interquartile Range Given a sorted singly linked list without a tail ( e . g , head - > 1 - > 2

/*
Interquartile Range
Given a sorted singly linked list without a tail (e.g, head ->1->2->3->4),
return the interquartile range of the data set using the slow and fast pointer
approach OR using a methodology that does not iterate over the linked list
twice. You must not iterate over the entire linked list more than once and you
cannot use arrays, vectors, lists or an STL implementation of List ADT in this problem.
Sample Input:
2445678
Sample Output:
3.00
*/
#include
#include
class Node {
public:
int value;
Node* next = nullptr;
};
Node* insertEnd(Node* head, int key)
{
Node* temp = new Node();
temp->value = key;
if(head == nullptr)
head = temp;
else
{
Node* curr = head;
while(curr->next != nullptr)
curr = curr->next;
curr->next = temp;
}
return head;
}
float interQuartile(Node* head){
float IQR =0.0;
Node* Part1= NULL;
Node* Part2= NULL;
Node* Part3= NULL;
Node* Part4= NULL;
if (!head ||!head->next ||!head->next->next ||!head->next->next->next)
{
return 0.0;
}
// Loop that moves pointer
while(true){
if(Part1!= NULL){
Part1= Part1->next;
}
else{
Part1= head;
}
if(Part2!= NULL){
Part2= Part2->next->next;
}
else{
Part2= head->next;
}
if(Part3!= NULL){
Part3= Part3->next->next->next;
}
else{
Part3= head->next->next;
}
if(Part4!= NULL){
Part4= Part4->next->next->next->next;
}
else{
Part4= head->next->next->next;
}
// Breaks loop
if(Part4== NULL || Part4->next == NULL){
break;
}
}
// When Odd
if(Part4== NULL){
IQR = Part3->value - Part1->value;
return IQR;
}
// When Even
else{
IQR =(Part3->value + Part3->next->value)/2.0-(Part1->value + Part1->next->value)/2.0;
return IQR;
}
}
int main(){
Node* head = new Node();
head->value =2;
head->next = new Node();
head->next->value =4;
head->next->next = new Node();
head->next->next->value =4;
head->next->next->next = new Node();
head->next->next->next->value =5;
head->next->next->next->next = new Node();
head->next->next->next->next->value =6;
head->next->next->next->next->next = new Node();
head->next->next->next->next->next->value =7;
head->next->next->next->next->next ->next = new Node();
head->next->next->next->next->next->next->value =8;
head->next->next->next->next->next ->next->next = new Node();
head->next->next->next->next->next->next->next->value =9;
head->next->next->next->next->next ->next->next->next = new Node();
head->next->next->next->next->next->next->next->next->value =10;
float result = interQuartile(head);
std::cout << "Interquartile Ranges: "<< std::fixed << std::setprecision(2)<< result << std::endl;
return 0;
}
This is my code, it works but when I try certain numbers, it gives me a "Exception has occurred. Segmentation fault"
For example if I try the numbers 2445678910 or 18154382101110245655345137556. It gives the same error but work for 18154382101110245655345137556137576 and 2445678 and 1234

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Step: 3

blur-text-image

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 Databases questions

Question

10. What is meant by a feed rate?

Answered: 1 week ago

Question

What are employee assistance programs and wellness programs?

Answered: 1 week ago