Sorting linked list by name, id, grade

closed account (GARovCM9)
This is my node file and based on this I'm trying to write a sort function.
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
#include "Node.h"
#include <iostream>
#include <string>
using namespace std;

Node::Node(const char* _name, int _ID, int _grade)
{
	this->name = _name;
	this->ID = _ID;
	this->grade = _grade;
	this->next_Node = NULL;
}
const char* Node::getName()			
{
	return this -> name;
}
int Node::getID()					
{
	return this -> ID;
}
int Node::getGrade()				
{
	return this -> grade;
}
Node *Node::getNextNode()		
	return this -> next_Node;
}
void Node::setNextNode(Node* next_Node)	
{
	this->next_Node = next_Node;
}

And based on this node file I'm trying to make functions like
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
#include "LinkedList.h"
#include <iostream>

using namespace std;
using namespace Linked;
List::List() {
	head = NULL;
}
List::~List() {
	Node* Delete_Node = head;
	while (Delete_Node != NULL) {
		head = Delete_Node->getNextNode();
		delete Delete_Node;
		Delete_Node = head;
	}
}
void List::insertfront(const char* name, int ID, int grade) {
	Node* New_Node = new Node(name, ID, grade);
	if (head != NULL)
		New_Node->setNextNode(head);
	head = New_Node;
}
void List::PrintList() {
	if (head == NULL) {
		cout << "List is empty" << endl;
	}
	else {
		Node* Print_Node = head;
		cout << "PRINTING:" << endl;
		while (Print_Node != NULL) {
			cout << Print_Node->getName() << "	" << Print_Node->getID() << "	" << Print_Node->getGrade() << endl;
			Print_Node = Print_Node->getNextNode();
		}
	}
}
void List::sort(const char* type)

But for void List::sort(const char* type) code, I can't figure out how I should handle the given codes to make it work so that in main function, it can be handled like list.sort(name); list.sort(ID); This is what I did do far to make this sort code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void List::sort(const char* type)
{
	Node* Prev_Node = head;
	Node* Next_Node = Prev_Node->getNextNode();
	while (Prev_Node->getNextNode() != NULL)
	{
		if (strcmp(Prev_Node->getName(), Next_Node->getName()) > 0)
		{
			Next_Node->setNextNode(Next_Node);
			Prev_Node->setNextNode(Next_Node);
		}
		Prev_Node = Prev_Node->getNextNode();
	}
	
}
Variations in sorting is usually achieved by passing in a compare function.

C++ way https://www.cplusplus.com/reference/algorithm/sort/
C way https://www.cplusplus.com/reference/cstdlib/qsort/

So you end up with 4 very short functions to do things like
1
2
3
bool sort_by_name(const Node *node1, const Node *node2) {
    return strcmp(node1->getName(), node2->getName()) > 0;
}


And your sorting function has something like
1
2
3
4
5
void List::sort(bool (*cmp)() ) {
    ....
    if ( cmp(Prev_Node,Next_Node) ) {
    }
}



In your main, you would have
1
2
3
4
5
6
List mylist;
...
mylist.sort(sort_by_name);
...
mylist.sort(sort_by_id);


closed account (GARovCM9)
Thank you for answer, it really helps me. But I have curiosity about how to change the position for Prev_Node and Next_Node, can you explain it for me? (example: head-Prev_Node-Next_Node-NULL -----> head-Next_Node-Prev_Node-NULL) and also the sort function gets const char* type
for example for main function,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

const char* name = "name";
const char* ID = "ID";
const char* grade = "grade";

void main() {
	List list;				
	list.PrintList();		
	list.insertfront("Alex", 2023453, 89);	
	list.insertfront("Jack", 2024567, 67);	
	list.PrintList();		
	list.sort(name);		
	list.sort(ID);			
	list.sort(grade);		
	list.PrintList();		
}
Last edited on
Topic archived. No new replies allowed.