Hello, guys i have an irritiating problem in the following function:

void Sort(Point P, Point &AT50, Point &AO50){
Point AT50_new, AT50_last, AO50_new;

    while (P != NULL){
        if (P->Age <= 50){
            AT50_new = new Member;  
            AT50_new = P;
            AT50_new->next = NULL; // <--from here comes the problem
            if (AT50 != NULL)
                AT50 = AT50_new;
            else    AT50_last->next = AT50_new;
            AT50_last = AT50_new;
        }
        else
            AO50_new = new Member;
            AO50_new = P;
            AT50_new->next = NULL;// <--from here comes the problem
            if (AO50 != NULL)
                AO50 = AO50_new;
            else    AO50_last->next = AO50_new;
            AO50_last = AO50_new;

    P=P->next;
    }
}

The idea of the code is to check every element of a linked list and see if the element have value lower-equal to 50 or higher than 50. After that it has to create two additional lists - one for those lower and equal to 50 and the second for the rest. I spend alot of hours looking at it but i can't find the error i did. If possible could someone find the error and tell me what i did wrong? Thank you in advance!

P.S. Sorry if i'm opening an already disscused threat but i couldn't find any on this topic.

Why allocate new memory for those links? You can just move the link from the original linked list to the new linked list and adjust pointers accordingly. No need to allocate new memory or delete old nodes. Your function is causing a lot of memory leaks because none of the nodes are getting deleted. Just move the node from one list to another solves that problem.

Thank you for the reply. To be frank i am not able to see where and what to do, so if you could please explain a little more about what you meant by "moving the links from the original linked list to the new one". I tried some things, and it went from worse to bad, but still it didn't get the job done. This is what i have now:

void Sort(Point P, Point &AT50, Point &AO50){
Point AT50_new, AO50_new;

    while (P != NULL){
        if (P->Age <= 50){
            AT50_new = P;
            AT50_new->next = AT50;
            AT50= AT50_new;
        }
        else {
            AO50 = P;
            AO50_new->next = AO50;
            AO50= AO50_new;
        }
    P=P->next;
    }
}

I don't know how you declared Point, but below is a function that works assuming Point is a class or structure name, not declared as a pointer. This snippet just inserts the nodes at the beginning of the two linked lists, instead of at the tail. Also note that the originial linked list is not value after this function returns.

void Sort(Point* P, Point* &AT50, Point* &AO50){
    Point* temp;
    while(P)
    {
        temp = P;
        P = P->next;
        if( temp->value <= 50)
        {
            temp->next = AT50;
            AT50 = temp;
        }
        else
        {
            temp->next = AO50;
            AO50 = temp;

        }
    }
}

I'm sorry i didn't post that part.

#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstring>

using namespace std;



struct Member {
    char Name[15];
    char Genre[1];
    float  Age;
    char M_name[15];
    char F_name[15];
    Member *next;
};

typedef Member* Point
Point Head, AT50, AO50;

void ADD_Member(Point &Head){
char ch='y';
Point P, Last;


cout << "Enter new family member: " << endl;
  while ((ch == (char)'Yes') || (ch == (char)'yes') || (ch == (char)'Y') || (ch == (char)'y')) {
        P = new Member;
        cout << "Type the name: ";
        cin >> P -> Name;
        cout << "Type the genre: ";
        cin >> P -> Genre;
        cout << "Type the age: ";
        cin >> P -> Age;
        cout << "Type the mother's name(none if not avaiable): ";
        cin >> P -> M_name;
        cout << "Type the father's name(none if not avaiable): ";
        cin >> P -> F_name;
        P->next = NULL;
        if (Head == NULL)
           Head = P;
        else
           Last->next = P;
        Last=P;
     cout << "New family member? (Yes or No)";
     cin >> ch;
  }
}

void Sort(Point P, Point &AT50, Point &AO50){
Point temp;

    while (P != NULL){
        temp = P;
        if (temp->Age <= 50){
            temp->next = AT50;
            AT50= temp;
        }
        else {
            temp->next = AT50;
            AO50= temp;
        }
        P = P->next;
    }
}

void Print_Sort(Point Q, Point H){

   if (Q != NULL){
      cout << "Members younger than 51 years" << endl;
        Traverse(Q);
   }
   else cout << "No members younger than 51 years" << endl;

   if (H != NULL){
      cout << "Members older than 50 years" << endl;
      Traverse(H);
   }
   else cout << "No members older than 50 years" << endl;

}

int main(){
Head = NULL;
AT50 = NULL;
AO50 = NULL;
ADD_Member(Head);
Sort(Head, AT50, AO50);
Print_Sort(AT50, AO50);

}

It's a little messy but here you go. And about what you replied, i tried it but after i enter few elements with different age, the output of the functions prints only the first element of the list with people age <=50 and not the rest and nothing for the second list with people >50.

Excuse me again.

void Traverse(Point P){
    while (P != NULL){
        cout << "-----------------------------" << endl;
        cout << P->Name << endl;
        cout << P->Age << endl;
        cout << P->F_name << endl;
        cout << P->M_name << endl;
        cout << "-----------------------------" << endl;
    P = P->next;
    }
}
This article has been dead for over six months. Start a new discussion instead.