Mr. Green
February 21st, 2005, 03:08 PM
I'm trying to sort a singly linked list and I can't get the algorithm right
I'm trying to sort an already full list. it seems like it would be a lot easier to sort a list as it's being filled, but I can't figure that algorithm out either. here's some of my code so you can see what I mean:
POLYNOMIAL.H
------------
//this is the header file for class Polynomial and struct term
using namespace std;
struct term
{
int co; //coefficient of the term
int exp; //exponent of the term
term* link;
};
typedef term* termptr;
class Polynomial
{
public:
Polynomial (const Polynomial& p1);
~Polynomial();
void operator = (Polynomial p1);
Polynomial() {head = NULL;}
void sort();
const termptr get_head() const {return head;}
void set_head(termptr t1) {head = t1;}
Polynomial operator + (Polynomial& p1);
friend istream& operator >> (istream& ins, Polynomial& p1);
friend ostream& operator << (ostream& outs, Polynomial& p1);
private:
termptr head;
};
POLYNOMIAL.cc
-------------
//definitions for class Polynomial
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cctype>
#include "polynomial.h"
using namespace std;
Polynomial::Polynomial(const Polynomial& p1)
{
termptr temp, temp1;
temp = p1.get_head();
if (!p1.get_head())
return;
temp1 = new term;
temp1->co = temp->co;
temp1->exp = temp->exp;
temp1->link = NULL;
head = temp1;
temp = temp->link;
for (termptr ptr = temp; ptr != NULL; ptr = ptr->link)
{
temp1->link = new term;
temp1 = temp1->link;
temp1->co = ptr->co;
temp1->exp = ptr->exp;
}
}
istream& operator >> (istream& ins, Polynomial& p1)
{
char x;
string line;
termptr new_term, current;
new_term = new term;
new_term->link = NULL;
p1.set_head(new_term);
current = new term;
getline(ins,line);
if (ins)
{
line.erase(remove(line.begin(), line.end(),' '), line.end());
stringstream ss(line);
while (ss>>current->exp >> x >> current->exp)
{
new_term = new term;
new_term->link = NULL;
current->link = new_term;
current = current->link;
}
}
current->link = NULL;
return ins;
}
void Polynomial::sort()
{
Polynomial p1;
termptr cursor, new_term;
int largest = 0;
new_term = new term;
p1.set_head(new_term);
cursor = new_term;
for (termptr ptr = head; ptr != NULL; ptr = ptr->link)
{
for (termptr temp = ptr; temp != NULL; temp = temp->link)
{
if (temp->exp > largest)
{
largest = temp->exp;
cursor->co = temp->co;
cursor->exp = temp->exp;
cursor->link = new term;
}
}
}
head = p1.get_head();
}
ostream& operator << (ostream& outs, Polynomial& p1)
{
for (termptr ptr = p1.get_head(); ptr != NULL; ptr = ptr->link)
{
if (ptr == p1.get_head())
{
if (ptr->exp == 0)
{
outs << ptr->co;
}else if (ptr->exp > 0)
{
outs << ptr->co << "x" << ptr->exp;
}
}else
{
if (ptr->exp == 0)
{
outs.setf(ios::showpos);
outs << ptr->co;
outs.unsetf(ios::showpos);
}else if (ptr->exp > 0)
{
outs.setf(ios::showpos);
outs << ptr->co;
outs.unsetf(ios::showpos);
outs << "x" << ptr->exp;
}
}
}
return outs;
}
MAIN.cc
-------
//application file
#include <iostream>
#include "polynomial.h"
using namespace std;
int main()
{
Polynomial poly1, poly2, result;
cout << "Please enter a polynomial: ";
cin >> poly1;
cout << endl;
//poly1.sort(); --sort doesn't work at all so I commented it out
cout << "Please enter another one: ";
cin >> poly1;
cout << endl;
//poly2.sort();
cout << poly1 << endl << poly2 << endl;
return 0;
}
what I really want to know is how to sort the incoming list while it's still being input
I'm trying to sort an already full list. it seems like it would be a lot easier to sort a list as it's being filled, but I can't figure that algorithm out either. here's some of my code so you can see what I mean:
POLYNOMIAL.H
------------
//this is the header file for class Polynomial and struct term
using namespace std;
struct term
{
int co; //coefficient of the term
int exp; //exponent of the term
term* link;
};
typedef term* termptr;
class Polynomial
{
public:
Polynomial (const Polynomial& p1);
~Polynomial();
void operator = (Polynomial p1);
Polynomial() {head = NULL;}
void sort();
const termptr get_head() const {return head;}
void set_head(termptr t1) {head = t1;}
Polynomial operator + (Polynomial& p1);
friend istream& operator >> (istream& ins, Polynomial& p1);
friend ostream& operator << (ostream& outs, Polynomial& p1);
private:
termptr head;
};
POLYNOMIAL.cc
-------------
//definitions for class Polynomial
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cctype>
#include "polynomial.h"
using namespace std;
Polynomial::Polynomial(const Polynomial& p1)
{
termptr temp, temp1;
temp = p1.get_head();
if (!p1.get_head())
return;
temp1 = new term;
temp1->co = temp->co;
temp1->exp = temp->exp;
temp1->link = NULL;
head = temp1;
temp = temp->link;
for (termptr ptr = temp; ptr != NULL; ptr = ptr->link)
{
temp1->link = new term;
temp1 = temp1->link;
temp1->co = ptr->co;
temp1->exp = ptr->exp;
}
}
istream& operator >> (istream& ins, Polynomial& p1)
{
char x;
string line;
termptr new_term, current;
new_term = new term;
new_term->link = NULL;
p1.set_head(new_term);
current = new term;
getline(ins,line);
if (ins)
{
line.erase(remove(line.begin(), line.end(),' '), line.end());
stringstream ss(line);
while (ss>>current->exp >> x >> current->exp)
{
new_term = new term;
new_term->link = NULL;
current->link = new_term;
current = current->link;
}
}
current->link = NULL;
return ins;
}
void Polynomial::sort()
{
Polynomial p1;
termptr cursor, new_term;
int largest = 0;
new_term = new term;
p1.set_head(new_term);
cursor = new_term;
for (termptr ptr = head; ptr != NULL; ptr = ptr->link)
{
for (termptr temp = ptr; temp != NULL; temp = temp->link)
{
if (temp->exp > largest)
{
largest = temp->exp;
cursor->co = temp->co;
cursor->exp = temp->exp;
cursor->link = new term;
}
}
}
head = p1.get_head();
}
ostream& operator << (ostream& outs, Polynomial& p1)
{
for (termptr ptr = p1.get_head(); ptr != NULL; ptr = ptr->link)
{
if (ptr == p1.get_head())
{
if (ptr->exp == 0)
{
outs << ptr->co;
}else if (ptr->exp > 0)
{
outs << ptr->co << "x" << ptr->exp;
}
}else
{
if (ptr->exp == 0)
{
outs.setf(ios::showpos);
outs << ptr->co;
outs.unsetf(ios::showpos);
}else if (ptr->exp > 0)
{
outs.setf(ios::showpos);
outs << ptr->co;
outs.unsetf(ios::showpos);
outs << "x" << ptr->exp;
}
}
}
return outs;
}
MAIN.cc
-------
//application file
#include <iostream>
#include "polynomial.h"
using namespace std;
int main()
{
Polynomial poly1, poly2, result;
cout << "Please enter a polynomial: ";
cin >> poly1;
cout << endl;
//poly1.sort(); --sort doesn't work at all so I commented it out
cout << "Please enter another one: ";
cin >> poly1;
cout << endl;
//poly2.sort();
cout << poly1 << endl << poly2 << endl;
return 0;
}
what I really want to know is how to sort the incoming list while it's still being input