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:
Code:
#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;
}
Would it be any similar to this?
Code:
void bubbleSort(int *array, int length)
{
for(int i = 0; i < length; i++)
{
int s = i;
for(int c = i+1; c < length; c++)
{
if(array[c]<array[s])
{
s = c;
}
}
swap(array[i], array[s]);
cout<<array[i]<<endl;
}
}