Next the user enters a threshold value. Then I create two new lists, smaller and greater with values either smaller than the threshold
or greater than the threshold. The result are three linked lists; one smaller, one greater, and one that contains any data that was equal to the threshold.
I print out the the smaller and greater lists, then sort the lists in ascending order and print the sorted ists out.
My problem is that I receive a segmentation fault error after entering the threshold value.
My guess is that the problem is with the get_input function but I just do not see the error.
What is wrong with my function or lists?
Thank you,
Angie
- Code: Select all
#include <iostream>
#include <cstdlib>
using namespace std;
struct Node
{
int data;
Node *link;
};
void get_input(Node*& head_ptr);
//Gets list of integers from user. Calls list_insert().
void list_insert(Node* ptr, const int& entry);
//Inserts data into the list at the end.
void display_and_count(Node*& head_ptr);
//Prints out the integers in the list.
void split_them_up(Node*& head_ptr, Node*& smaller, Node*& greater, int threshold);
//Takes original list and moves data into three lists; one with values lower than the threshold,
//one with only values equal to the threshold(the original list),
//and one with values greater than the threshold.
void remove_after(Node* head_ptr);
//removes the node after the one pointed to by head_ptr.
void place(Node*& head_ptr, int x);
//Sorts the items in the list in increasing order.
int main()
{
Node* head_ptr, *smaller, *greater;
head_ptr = new Node;
head_ptr = NULL;
smaller = new Node;
smaller = NULL;
greater = new Node;
greater = NULL;
int threshold = 0;
get_input(head_ptr);
display_and_count(head_ptr);
cout << "What threshold value would you like to use?\n";
cin >> threshold;
cout << "Threshold value: " << threshold;
split_them_up(head_ptr, smaller, greater, threshold);
display_and_count(head_ptr);
cout << "SMALLER: ";
display_and_count(smaller);
cout << "GREATER: ";
display_and_count(greater);
cout << endl;
place(smaller, x);
cout << "Sorted smaller list: ";
display_and_count(smaller);
place(greater, x);
cout << "Sorted greater list: ";
display_and_count(greater);
return 0;
}
void get_input(Node *&head_ptr)
{
Node* temp, *temp2;
temp = new Node;
int input;
cout << "Enter a list of positive integers. Enter a zero at the "
<< "end of the list.\n";
cin >> input;
if(input == 0)
{
cout << "Empty list.\n";
exit(1);
}
temp->data = input;
temp->link = head_ptr;
head_ptr = temp;
while(input != 0)
{
temp2 = temp;
cin >> input;
if(input == 0)
break;
temp = new Node;
temp->data = input;
temp2->link = temp;
}
temp->link = NULL;
}
void list_insert(Node* ptr, const int& entry)
{
Node* temp;
if(ptr == NULL)
{
cout << "Invalid insertion";
exit(1);
}
temp = new Node;
temp->data = entry;
temp->link = ptr->link;
ptr->link = temp;
}
void display_and_count(Node*& head_ptr)
{
int count = 0;
Node* temp;
temp = head_ptr;
cout << "Original list: ";
do
{
if(temp == NULL)
break;
else
{
cout << temp -> data << " ";
temp = temp -> link;
count++;
}
}
while(temp != NULL);
if(count > 0)
cout << " has " << count << " nodes." << endl;
else
cout << "is empty.\n";
}
void split_them_up(Node*& head_ptr, Node*& smaller, Node*& greater, int threshold)
{
Node* temp = head_ptr, *temp_small = NULL, *temp_great = NULL, *temp_head = NULL;
for(temp = head_ptr; temp != NULL; temp = temp->link)
if(temp->data < threshold)
{
temp_small - new Node;
temp_small->data = temp->data;
smaller = temp_small;
remove_after(head_ptr);
}
else if(temp->data > threshold)
{
temp_great = new Node;
temp_great->data = temp->data;
greater = temp_great;
remove_after(head_ptr);
}
}
void remove_after(Node* head_ptr)
{
Node* temp;
int info;
if(head_ptr == NULL)
{
cout << "End.\n";
exit(1);
}
else
{
temp = head_ptr->link;
if(temp == NULL)
{
cout << "End.\n";
exit(1);
}
else
{
info = temp->data;
head_ptr->link = temp->link;
delete temp;
}
}
}
void place(Node*& head_ptr, int x)
{
Node* pointer, *shadow, *r;
bool found = false;
pointer = head_ptr;
shadow == NULL;
while((pointer != NULL) && (!found))
{
if(x < pointer->data)
found = true;
else
{
shadow = pointer;
pointer = pointer->link;
}
}
if(shadow == NULL)
{
r = new Node;
r->data = x;
r->link = head_ptr;
head_ptr = r;
}
else
{
r = new Node;
r->data = x;
r->link = shadow->link;
shadow->link = r;
}
return;
}
