Sorting List

Jul 6, 2011 at 3:50pm
Hey everyone, I've created two linked lists that hold a name and a GPA (both lists hold the same data) and I need to sort them according to GPA. I'm having trouble figuring out how to move both pieces of data at the same time. Maybe I'm just misunderstanding how linked lists work but I was thinking of doing it like a bubblesort
                  temp = temp1->data;
                  temp1->data = temp2->data;
                  temp2->data = temp;

But how does ALL of the data move instead of just the name or the GPA?
Here's the code I have so far. I removed an unsorted print function just to make it a little shorter.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void getdata(string& lastname,float& gpa_value);
const int nil = 0;

class node_type
{
public:
    string name;
    float gpa;
    node_type *next;
};

void main()
{
	node_type *uno, *duo, *p, *r, *newnode, *newnode2;
	int i;
	float gpa_value;
	string lastname;

	uno = new node_type;
	p = uno;
	getdata(lastname,gpa_value);
	(*uno).name = lastname;
	(*uno).gpa = gpa_value;
	(*uno).next = nil;

	duo = new node_type;
	r = duo;
	(*duo).name = lastname;
	(*duo).gpa = gpa_value;
	(*duo).next = nil;
 	for (i=0; i<=8;++i)
	{
	    getdata(lastname,gpa_value);
		newnode = new node_type;
		newnode2 = new node_type;
		(*newnode).name = lastname;
		(*newnode).gpa = gpa_value;
		(*newnode).next = nil;
		(*newnode2).name = lastname;
		(*newnode2).gpa = gpa_value;
		(*newnode2).next = nil;
		(*p).next = newnode;
		(*r).next = newnode2;
		p = newnode;
		r = newnode2;
	}
}
//==============================
void getdata(string& lastname,float& gpa_value)
{
	cout << "Enter last name \n" ;
	cin >> lastname;
	cout << lastname << "\n";
	cout << "Enter gpa \n" ;
	cin >> gpa_value;
	cout << gpa_value << "\n";
}
Jul 6, 2011 at 4:07pm

When you are moving nodes around in your linked list you are just changing the pointers to each other, all data is alreay in the nodes you are pointing to.

Following code needs to be changed to.

1
2
3
temp = temp1->next;
temp1->next = temp2->next;
temp2->next = temp;
Jul 6, 2011 at 4:14pm
what is temp1 and temp2? which nodes they are pointing to and which nodes are pointing to these? This needs to be taken care of or else the list will break.
Jul 6, 2011 at 4:22pm
The first node would = temp1 and the second node would = temp2. Then for loops and an if statement if(temp1->data > temp2->data) so if the GPA in temp1 was higher than the GPA in temp2, temp would = temp1.next, temp1 would go in temp2's place with
temp1->next = temp2->next
, and then temp2 would be put in its corresponding spot with
temp2->next = temp
.
Jul 6, 2011 at 4:32pm
there is more to this:

A->temp1->temp2->B

1. you have to swap temp1 and temp2
2. A should point to temp2
3. temp2 should point to temp1
4. temp1 should point to B

only swapping temp1 and temp2 would not do.

Jul 7, 2011 at 6:07am




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

int sort[5];
int temp,i,j;

clrscr();

printf("Enter list of array: ");

for(i=0;i<5;i++){
scanf("%d",&sort[i]);
}

/* Sorting for loop */

for(i=0;i<3;i++){
    for(j=0;j<3;j++){

       if(sort[i] > sort[j]){

            temp=sort[j];
            sort[j]=sort[i];
            sort[i]=temp;

           }
}

/* end of sorting */

for(i=0;i<5;i++){
printf("%d",&sort[i]);
}

Last edited on Jul 7, 2011 at 6:18am
Jul 7, 2011 at 5:07pm
Thanks for everyone's tips and I got the sort working, but now I'm trying to merge both the lists not using STL but it's not working quite right. Front of the first list is Uno and front of the second is Duo so couldn't I just do something like:
		for(temp1 = uno; temp1!=NULL; temp1 = temp1->next)
		{
			if ((*temp1).next = NULL)
			{
				(*temp1).next = duo;
			}
		}


Where you just go until you're at the end of list 1, and then make its .next = head of list 2?
Last edited on Jul 7, 2011 at 5:09pm
Jul 7, 2011 at 5:58pm
closed account (D80DSL3A)
It looks like you intend to append list 2 to list1, not merge them.
Why not find the last node in the first list:
for(temp1 = uno; temp1->next!=NULL; temp1 = temp1->next);
That should leave temp1 pointing to the last node. Then assign temp1->next = duo;

Side note: Are you familiar with the indirect member reference operator -> ?
In many places you use (*ptr).mbr, where you could just use ptr->mbr.
Example: (*newnode).next = nil; is the same as newnode->next = nil;
Also, there is a pre-defined constant NULL (so you don't have to define your own nil):
newnode->next = NULL;
Last edited on Jul 7, 2011 at 5:58pm
Jul 7, 2011 at 6:50pm
Thanks for the reply fun. Yes I've changed my entire code to use NULL and I kind of flip flopped between using (*node).next and node->next for no particular reason. I'll go back and make it all consistent before I finish up. I'll try that suggestion now, although for some reason I think I already did and something didn't work.

Edit: That worked, and is incredibly simple. I have no idea how I goofed that one up. Time to sort alphabetically. I assume it's the same as sorting numerically, except it uses ascii code to sort letters?

Edit2: Yup

Marked as solved.
Last edited on Jul 7, 2011 at 6:59pm
Topic archived. No new replies allowed.