Sorting a Linked List

Hi I'm trying to sort a linked list I created using a function but I have no idea how to sort it. Can anyone help me?

Here's my Code:

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef _ASSIGNMENT_7_CPP_
#define _ASSIGNMENT_7_CPP_

#include <iostream>                        
#include <cstdlib>                         
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct Node{
    string parts;
    int ID;    
    int date;
    float price;

    Node *next;  
    
    Node(); 
};

Node::Node()
{
    next=NULL; 
}

void Insert(Node *&Front,Node *&Rear, string n_parts, int date, int n_Id, float n_price, int count)
{
    Node *n_Info = new Node();  
  
    n_Info->parts = n_parts;   
    n_Info->date = date;
    n_Info->price = n_price; 
    n_Info->ID = n_Id;

    if(count ==0)
    {        
        Front = Rear = n_Info;
    }

    else if(count > 0)
    {
        Rear->next = n_Info;     
        Rear = Rear -> next;
    }
    
    do
    {  
        if (n_Info == NULL)
        cout << "End of list" << endl;
        else
       {  
          cout<< n_Info->parts<<endl;
          cout<< n_Info->ID<<endl;
          cout<< n_Info->date<<endl;
          cout<< n_Info->price<<endl;
          cout << endl;
          
          n_Info = n_Info->next;
       }
  }
  while (n_Info != NULL);

}

void Flush(Node *&Front,Node *&Rear, int L_size)
{
    Node *temp_node = Front;

    for(int i =0; i<L_size; i++)
    {
        Front = Front->next;
        delete temp_node;
        temp_node=Front;
    }

}

float Avg(float n_price)
{
	static float sum = 0, i = 0;
	
	sum += n_price;
	i++;
	return (sum/i);
}

float Highest(float n_price)
{	
    static float  max = 0;
    
    if(n_price > max)
	max = n_price;
	
	return max;
}

float Lowest(float n_price)
{	
    static float min = n_price;
    
    if(n_price < min)
	min = n_price;
	
	return min;
	
}

int main()
{
    
    Node *Front = NULL;
    Node *Rear = NULL;                          
    int L_size = 0;                         
    
    ifstream myfile1("a7.txt");
        
    char temp[100];
    string temp_parts;
    int temp_ID;
    int temp_date;
    float temp_price;
    
    float average, high, low;

    while(myfile1.peek() != EOF)
    {
        myfile1.getline(temp, 100);
        temp_parts = temp;

        myfile1.getline(temp, 100);
        temp_ID = atoi(temp);
        
        myfile1.getline(temp, 100);
        temp_date = atoi(temp);      
        
        myfile1.getline(temp, 100);
        temp_price = atof(temp);

        myfile1.getline(temp, 100);

        Insert(Front, Rear, temp_parts, temp_date, temp_ID, temp_price, L_size);
        average = Avg(temp_price);
        high = Highest(temp_price);
        low =  Lowest(temp_price);
        L_size++;
        
    }
   
    cout<<fixed<<setprecision(2)<<"Average Price: "<<average<<endl;
    cout<<fixed<<setprecision(2)<<"Most Expensive: "<<high<<endl;
    cout<<fixed<<setprecision(2)<<"Most Inexpensive: "<<low<<endl;
    
    Flush(Front, Rear, L_size); 

                   
    return 0;                    
}
#endif 
I tried to use this code(for singly list) to sort but it seems to not work.

Edit I made a little modification but it still doesn't work.

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
void sortNodes(Node * ptr) {
	
	for(bool didSwap = true; didSwap; ) {
		didSwap = false;
		for(ptr = ptr; ptr->next != NULL; ptr = ptr->next) {
			if(ptr->date > ptr->next->date) {
				swap(ptr->parts, ptr->next->parts);
				swap(ptr->ID, ptr->next->ID);
				swap(ptr->date, ptr->next->date);
				swap(ptr->price, ptr->next->price);
				didSwap = true;
			} 
		}
	}
	
      do
    {  
        if (ptr == NULL)
        cout << "End of list" << endl;
        else
       {  
          cout<< ptr->parts<<endl;
          cout<< ptr->ID<<endl;
          cout<< ptr->date<<endl;
          cout<< ptr->price<<endl;
          cout << endl;
          
          ptr = ptr->next;
       }
   }
     while (ptr != NULL);

}
Last edited on
your bubble sort thing works if you preserve the starting point.

don't mess with ptr during the sort. ptr starts you at the beginning of the list during each pass through the loop. create a local Node pointer:
 
Node *tptr;

and work that instead of ptr. you will have to replace lots of stuff like:
 
for(tptr = ptr; tptr->next != NULL; tptr = tptr->next) {


your use of for() is very creative.
Topic archived. No new replies allowed.