how to sort a linked list using selection sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


#include <iostream>
#include <string>
#include "Employee.h";
#include "EmployeeList.h";

using namespace std;

void main()
{

	EmployeeList a;

	a.AddNode(43,"rene","briggs");
	a.AddNode(23,"brian","shay");
	a.AddNode(98,"poppy","doll");
	a.AddNode(76,"greg","flynt");
	
	a.bubbleSort();
	system("pause");

}


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


#include <iostream>
#include <string>
#include "Employee.h";

using namespace std;
#ifndef EMPLOYEELIST_H
#define EMPLOYEELIST_H


class EmployeeList
{
    private:
	
    Employee *head;//employee pointer(head pointer)

    public:
	
	//default constructor
     EmployeeList()
     {
         head = NULL;
     }
   


	//Add a node to the end of the list
     void AddNode(int id, string fName, string lName)
     {		

	Employee *node  = new Employee(id,fName,lName);	
   
	//if memory was sucessfully allocated
	if (node != NULL)
	{
	    //if this is the first node to be added 
	    //or if list is empty
	    if (head == NULL)
	    {
		head = node;//make the first element
	    }
	    else
	    {   ///if the list is not empty

	       Employee *temp = head;            
		
                while(temp->getNext() != NULL)
	       {				
		   temp = temp->getNext();//points temp to the next element		
	        }

	        temp->setNext(node);//the end is reached we insert the element
	     }
	}

	else

	{
		cout << "Unable to create new node." << endl;
	}
}
			
			
	

	//display list 
    void displayList()
	{
		Employee *temp = head;
		while (temp != NULL)
		{
		temp->display();//Employee's display method called here
		temp = temp->getNext();
		}

	}
    

void bubbleSort(){
      Employee*holder, *previous, *position;
      Employee *temp = head;//assigns the head to a temporary pointer
      EmployeeList *list;

      Employee *previousnode = NULL;//set the previous node to point to null

      while (temp != NULL)//while not the end of the list
      {
	for (holder=temp; holder!=NULL && holder->next!=NULL;  holder=holder-         >next){

	previous = holder;

	   for(position=holder->next; position!=NULL; position=position->next){

		if(position->getIdNumber()< previous->getIdNumber()){

		       previous = position;
		}
	    }

		temp = holder;
		holder = previous;
		head = holder;
		previous = temp;

	 }
	     list->displayList();
			
      }

   }

};
#endif 


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

#include <string>
#include <iostream>

using namespace std;

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

class Employee
{

private: 

	int idNumber;
	string firstName;
	string lastName;



	//Employee * next;

public:
//define accessors, constructors, destructor,display method and mutators
Employee * next;
//default constructor
Employee()
{
	idNumber = 1; //default value for employeeID
	firstName = "John"; //default value for employee First Name
	lastName ="Doe"; //default value for employee Last name


	next = NULL; 
}



Employee(int id,string fname,string lname)
{
	idNumber = id;
	firstName = fname;
	lastName =lname; 
	next = NULL; 
}

//destructor
~Employee()
{
	cout << "\nEmployee Node  ["<< getIdNumber() << " " << getFirstName()<< " "<<getLastName() << "]  Destroyed"<<endl;

}


int getIdNumber()
{
	return idNumber;
}

//LastName mutator
void setIdNumber(int id)
{
	idNumber = id;
}

//add mutator for idNumber below


//lastName accessor
string getLastName()
{
	return lastName;
}

//LastName mutator
void setLastName(string lName)
{
	lastName = lName;
}


//firstName accessor
string getFirstName()
{
	return firstName;
}

//LastName mutator
void setFirstName(string fName)
{
	firstName = fName;
}


//firstName accessor
Employee* getNext()
{
	return next;
}


//LastName mutator
void setNext(Employee *nxt)
{
	next = nxt;
}


void display()
{
//display should output to user like this [1-John-Jones]->

	cout<<idNumber<<"-"<<firstName<<"-"<<lastName<<endl;
}

};///end of class


#endif 
Last edited on
Topic archived. No new replies allowed.