linked list help

I have this project to input 12 numbers each into two linked list and sort them. Then with the first list i have to take the average and delete all numbers > than the average and print out the new list. Finally i have to merge the new first list and second list into a final list. The first and second list work fine but for some reason the loop for the deletion wont work and i can't get the lists to merge. please help if you can.

#include <iostream>

using namespace std;

void getData(int& number);
void getData2(int& number);

class node_type
{
public:
int num;
node_type *next;
};

int main()
{
node_type *first, *p, *q, *newnode,*last, *first2, *p2, *q2, *newnode2;
int number;
float sum = 0;
float average;

first = new node_type;
p = new node_type;
last = new node_type;
getData(number);
(*first).num = number;
(*first).next = 0;

for (int i = 1; i < 12; i++)
{
getData(number);

newnode = new node_type;
(*newnode).num = number;

if (first == 0)
{
first = newnode;
}
else if ((*newnode).num < (*first).num)
{
(*newnode).next = first;
first = newnode;
}
else if ((*first).next == 0)
{
(*first).next = newnode;
}
else
{
q = first;
p = (*q).next;

while ((p != 0) && (*p).num < ((*newnode).num))
{
q = p;
p = (*p).next;
}
(*q).next = newnode;
(*newnode).next = p;
}
}
q = first;

cout << "The first list contains: " << endl;

while (q != 0)
{
cout << (*q).num << endl;
sum = sum + (*q).num;
q = (*q).next;
}
average = sum/12;
q = first;

cout << "The average of all the integers in the first list is: " << average << endl;

cout << "The new first list with deletions contains: " << endl;

while (q != 0)
{
if ((*q).num < average)
{
cout << (*q).num << endl;
q = (*q).next;
}
}
last = q;
(*last).next = 0;

first2 = new node_type;
p2 = new node_type;
q2 = new node_type;
getData2(number);
(*first2).num = number;
(*first2).next = 0;



for (int j = 1; j < 12; j++)
{
getData2(number);

newnode2 = new node_type;
(*newnode2).num = number;

if (first2 == 0)
{
first2 = newnode2;
}
else if ((*newnode2).num < (*first2).num)
{
(*newnode2).next = first2;
first2 = newnode2;
}
else if ((*first2).next == 0)
{
(*first2).next = newnode2;
}
else
{
q2 = first2;
p2 = (*q2).next;

while ((p2 != 0) && (*p2).num < ((*newnode2).num))
{
q2 = p2;
p2 = (*p2).next;
}
(*q2).next = newnode2;
(*newnode2).next = p2;
}
}
(*last).next = first2;
q2 = first2;

cout << "The second list contains: " << endl;

while (q2 != 0)
{
cout << (*q2).num << endl;
q2 = (*q2).next;
}

(*last).next = first2;

cout << "The final list contains: " << endl;

while (q != 0)
{
cout << (*q).num << endl;
q = (*q).next;
}
return 0;
}
void getData(int& number)
{
cout << "Enter an integer." << endl;
cin >> number;
}
void getData2(int& number)
{
cout << "Enter an integer for the second list." << endl;
cin >> number;
}
First:

Please use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/

It is very hard to read.

Where is the code to delete the node? I cannot find that keyword.

I would suggest to put things like create, merge, delete... in it's own functions. That would make the whole thing way easier.
Topic archived. No new replies allowed.