i wrote this program to input 12 intigers (mustuse linked lists), sort it while its being inserted, then take the average, then take out the numbers that are greater then the average and make a new lists, then take a second list of 12 intigers, then lastly merge those 2 lists into one that eliminates duplicates.
the program will build but when i run it (2003 builder) i get 'fatal error' pop up as soon as i press enter after putting in the first intiger. It is very frustrating. There is nothing more specific, just fatal error. Can anyone find the issue? I am a newbie, please be patient with me
#include <iostream>
using namespace std;
class node_type //dec of class
{
public:
int num;
node_type *next;
};
void SortInsert (node_type** head,int data, bool nodeup);
int main ()
{
node_type *prev, *current, *current2, *newnode;
node_type *head, *head2, *head_merged, *temp;
int i, number;
float sum, average;
sum = 0;
head2 = new node_type;
head = new node_type;
cout << "Please enter 12 integers\n";
for (i=1; i<=12; i++)
{
cout << i << "=" <<endl << endl;
cin >> number;
SortInsert (&head, number, 0);
}
cout << "entered numbers:\n";
current = head;
while (current != NULL)
{
cout << current->num <<endl;
sum += current->num;
current = current->next;
}
average = sum/12;
cout << "\n The average of the numbers you inputed is " << average << "\n";
//here we remove numbers less then the average.
current = head;
while(current != NULL)
{
if (current->num < average)
{
if (current == head)
head = current->next;
else
prev->next = current->next;
}
prev = current;
current = current->next;
}
cout << "This is the list after we took out all of the number less than the average \n";
current = head;
while(current !=NULL)
{
cout << current->num <<endl;
current = current ->next;
}
head2 = NULL;
cout << "Now please enter 12 more numbers to be merged into the previous list\n";
for (i=1; i<=12; i++)
{
cout << i << "=" << endl << endl;
cin >> number;
SortInsert (&head2, number, 0);
}
cout << "Enterded Numbers: \n";
current = head2;
while(current !=NULL)
{
cout << current->num <<endl;
current = current->next;
}
//here create a new list with data from the first 2 and remove dups.
head_merged = NULL;
current = head;
while (current!= NULL)
{
SortInsert(&head_merged, current ->num, 1);
current = current->next;
}
current = head2;
while(current != NULL)
{
SortInsert(&head_merged, current ->num, 1);
current = current->next;
}
cout << "The 2 lists merged together, duplicates removed:\n";
current = head_merged;
while(current != NULL)
{
cout <<current->num << endl;
current = current->next;
}
cin >> number; //keep the window open after program runs
return 0;
}
//sorting stufff
void SortInsert(node_type** head, int data, bool nodup)
{
node_type *current, *prev, *newnode;
newnode = new node_type;
newnode-> num = data;
current = *head;
while ((current != NULL && current->num <=data))
{
if(current->num == data && nodup)
return;
prev = current;
current = current->next;
}
if(*head == current)
{
*head = newnode;
newnode->next = current;
}
else if (current == NULL)
{
prev->next = newnode;
newnode->next = NULL;
}
else
{
prev->next = newnode;
newnode->next = current;
}
}