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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <conio.h>
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;i<number_nodes;i++)
	{
		sum[i] = 0;
		if(Current_Node != NULL)
		{
		for(int a=0;a<6;a++)
		{
			sum[i] += this->Current_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;i<number_nodes;i++)
	{
		
		for(int a=0;a<number_nodes;a++)
		{
			if(Current_Node != NULL && Last_Current_Node != NULL)
			{
		if(Current_Node->get_sum() < Last_Current_Node->get_sum())
		 {
			 cout<<"I'm RUnning"<<endl;
			 Temp = Current_Node;
			 Current_Node = Last_Current_Node;
			 Last_Current_Node = Temp;

		    
		 }
			
		Last_Current_Node = Last_Current_Node->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;a<number_nods;a++)
		{
			cout<<Current_Node->get_name()<<'\t'<<Current_Node->get_sum()<<endl;
		
		Current_Node = Current_Node->get_next();
	 }
	}
}

void List::get_input(int number_nod,Node * Nodes)
{
	int f = 0;
	char arrs[25];
	for(int i=0;i<number_nod;i++)
	{
		int arr[6] = {0};
	cout<<"Enter The Name OF the Node: ";
	cin>>arrs;
	Nodes[i].set_name(arrs);
	  for(int a=0;a<6;a++)
		{
		  f = 0;
		  cout<<"enter Elements "<<a+1<<": ";
          cin>>arr[a];
		  for(int j=0;j<6;j++)
		  {
          if(arr[a] == arr[j])
          {
			  
		   f++;
		   
		  }
		  }
		    if(f > 1)
		  {
           cout<<arr[a]<<" Dublicated"<<endl;
		   a--;
			  continue;
		  }

			
          }
		  this->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.