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
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";
}
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.
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.
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
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:
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;
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?