### How to Sort Link List

at the line number 65 that's my sort method first i sum up all the value in the nodes
after that i want to sort the Nodes In ascending order but the method is not working

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221`` ``````#include #include using namespace std; // Node Class class Node{ public: void set_next(Node* next_node){Next_Node = next_node;} Node * get_next(){return Next_Node;} char * get_name(){return nod_name;} void set_objects(int,int); void set_name(char*); int get_objects(int); void set_sum(int sums){sum = sums;} int get_sum(){return sum;} private: int sum; char *nod_name; int objects[6]; Node * Next_Node; }; void Node::set_name(char* name){ nod_name = new char[strlen(name+1)]; strcpy(nod_name,name); } void Node::set_objects(int obj,int indux) { objects[indux] = obj; } int Node::get_objects(int indux) { return objects[indux]; } // End Of Node Class // Class List class List{ public: List(); void sort(Node*,int); void display(int); void get_input(int,Node*); int get(int); void add(int[],Node*); private: int size; Node * Head_Node; Node * Current_Node; Node * Last_Current_Node; }; List::List() { size = 0; Head_Node = new Node(); Head_Node->set_next(NULL); Current_Node = NULL; Last_Current_Node = NULL; } void List::sort(Node * sort_node, int number_nodes) { Node * Temp ; char temp_name[25]; Current_Node = Head_Node; int * sum = new int[number_nodes]; for(int i=0;iCurrent_Node->get_objects(a); } } Current_Node->set_sum(sum[i]); Current_Node = Current_Node->get_next(); } Current_Node = Head_Node; Last_Current_Node = Head_Node->get_next(); int temp = 0; // Sorting Nodes for(int i=0;iget_sum() < Last_Current_Node->get_sum()) { cout<<"I'm RUnning"<get_next(); } } Last_Current_Node = Head_Node; Current_Node = Current_Node->get_next(); } } void List::display(int number_nods) { Current_Node = Head_Node; if(Current_Node != NULL) { for(int a=0;aget_name()<<'\t'<get_sum()<get_next(); } } } void List::get_input(int number_nod,Node * Nodes) { int f = 0; char arrs[25]; for(int i=0;i>arrs; Nodes[i].set_name(arrs); for(int a=0;a<6;a++) { f = 0; cout<<"enter Elements "<>arr[a]; for(int j=0;j<6;j++) { if(arr[a] == arr[j]) { f++; } } if(f > 1) { cout<add(arr,&Nodes[i]); } } int List::get(int Indux) { return Current_Node->get_objects(Indux); } void List::add(int objests[],Node * nodss) { for(int i=0;i<6;i++) { nodss->set_objects(objests[i],i); } if(Current_Node == NULL) { nodss->set_next(NULL); Head_Node->set_next(nodss); Last_Current_Node = Head_Node; Current_Node = nodss; Head_Node = Current_Node; } else { nodss->set_next(Current_Node->get_next()); Current_Node->set_next(nodss); Last_Current_Node = Current_Node; Current_Node = nodss; } size++; } int main() { List list; int number_nods = 0; cout<<"How many Nodes You want to Create: "; cin>>number_nods; Node * Nodes = new Node[number_nods]; list.get_input(number_nods,Nodes); list.sort(Nodes,number_nods); list.display(number_nods); _getch(); }``````
Hello someone please a little help here
closed account (3qX21hU5)
Please explain your problem better, the reason why no one is answering is because you have a wall of code yet only 2 lines of text that doesn't even explain what you are having trouble with just that it is not working...

Also you have a memory leak on line 215 `Node * Nodes = new Node[number_nods];`

Remember whenever you call `new` always call `delete`

So when you are done with the program or done with your pointer Nodes call

`delete Nodes;`

Just looked even closer and it was worse then I thought.... You have many memory leaks.
Last edited on
Hi Thank u very much for feed back ok i'll explain what i want to do is that after taking the sum of all nodes objects i campier the sums after camping it i want to sort the nodes in ascending order in main sort method how do i do that
Topic archived. No new replies allowed.