Mar 3, 2013 at 2:20pm UTC
Hey! I'm currently having trouble sorting a circular linked list in c++.
I want to sort said linked list, and here's what I've got so far. I'd really appreciate any input regarding anything in the code really, seeing as I'm a beginner at this. I hope you somewhat get an idea what I want to do.
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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct listNode;
typedef listNode* list_type;
struct person
{
char lastName[20];
char firstName[20];
char number[20];
};
struct listNode
{
person data;
list_type next;
};
void display(listNode*);
void sort(listNode*);
int main ()
{
list_type node;
list_type root = 0;
node = root;
int counter = 0;
person info;
char garb[1];
ifstream infile ("indata.txt" );
if ( infile.is_open())
{
while (infile.good())
{
infile.getline(info.lastName,20, ',' );
infile.getline(garb,1, ' ' );
infile.getline(info.firstName,20, ':' );
infile.getline(info.number,20);
if (node == 0)
{
node = new listNode;
node->next = NULL;
node->data = info;
counter++;
}
else
{
node->next = new listNode;
node = node->next;
node->data = info;
node->next = NULL;
counter++;
}
}
node->next = root;
infile.close();
}
else
{
cout <<"unable to open file." <<endl;
}
display(listNode*);
sort(listNode*);
display(listNode*);
return 0;
}
void sort(listNode*)
{
int counter = 0;
listNode* = next;
listNode* = curr;
listNode* = tmp;
for (int i=0; i < counter; i++)
{
for (int j=0; j < counter; j++)
{
if (curr->data > curr->next->data)
{
tmp->data = curr->data;
curr->data = curr->next->data;
curr->next-> = tmp->data;
counter++
}
}
}
}
}
void display(listNode*)
{
listNode* = curr;
cout << "Printing.." << endl;
while (curr != 0)
{
cout << curr->data << endl;
curr = curr->next;
}
cout << "..Done!" << endl;
Forgot to mention. The input I get is on this form:
1 2 3 4 5
Kverulando, Kajsa:0123-4567
Allegrettho, Albert:0701-23456
Glissando, Gloria:0234-5678
Hysterico, Hilding:013-1234
Presto, Pelle:013-1243
I want:
1 2 3 4 5
Allegrettho Albert 0701-23456
Glissando Gloria 0234-5678
Hysterico Hilding 013-1234
Kverulando Kajsa 0123-4567
Presto Pelle 013-1243
As you can see it's supposed to sort on the lastname first, and then firstname, and last the number.
Last edited on Mar 3, 2013 at 2:23pm UTC
Mar 3, 2013 at 3:00pm UTC
You fail to sort a circular list because there is no such thing as a sorted circular list (except for a list with all items equal). A list is sorted if X->data >= X->next->data for all nodes X. Thus root->data >= root->next->data >= root->next->next->...->next->data == root.data
That aside, I have no idea what your sort is trying to do? Which sorting algorithm are you trying to implement?
Last edited on Mar 3, 2013 at 3:01pm UTC