Library Class definition file
With bin sort function
//Link to Main Program"
#include <string>
#include <fstream.h>
struct node {
char data[500];
node *next;
};
class Library {
public:
Library() { head=tail=cur=NULL; };
Library(char *filename);
void ToFirst() {cur=head;};
void ToLast() {cur=tail;};
void ToNext();
void ToPrev();
void Insert(char *BookInfo);
void Remove();
void Print();
void BinSort(int startpos, int endpos);
private:
void ReadFile(char *filename);
node *head;
node *tail;
node *cur;
};
void Library::ToNext()
{
if (head==NULL) return;
if (cur->next==NULL) return;
cur = cur->next;
};
void Library::ToPrev()
{
node *p=head;
if (p==NULL) return;
if (p==cur) return;
while (p->next!=cur && p->next!=NULL) p = p->next;
cur = p;
};
Library::Library(char *filename)
{
head=tail=cur=NULL;
ReadFile(filename);
ToFirst();
};
void Library::ReadFile(char *filename)
{
ifstream Bookfile;
Bookfile.open(filename);
while (!Bookfile.eof())
{
char BookInfo[500];
Bookfile.getline(BookInfo,499,'\n');
Insert(BookInfo);
};
Bookfile.close();
};
void Library::Insert(char *BookInfo)
{
node *p;
p = new node;
p->next = NULL;
strcpy(p->data, BookInfo);
if (head==NULL)
head = cur = p;
else
tail->next = p;
tail = p;
};
void Library::Remove()
{
};
void Library::Print()
{
cout << cur->data << endl;
};
void Library::BinSort(int startpos, int endpos)
{
node *Binhead[256],*Bintail[256],*p;
int i,j,k;
for (i=endpos; i>=startpos; i--)
{
for (j=0; j<256; j++) Binhead[j]=Bintail[j]=NULL;
// DISTRIBUTE NODES TO BINS
while (head != NULL)
{
p=head;
head = head->next;
k=p->data[i];
if (Binhead[k] == NULL)
Binhead[k] = p;
else
Bintail[k]->next = p;
Bintail[k]=p;
p->next=NULL;
};
tail = NULL;
// APPEND BINS BACK TO MAIN LIST
for (j=0; j<256; j++)
{
if (Binhead[j]==NULL) continue; // Skip this bin if empty
if (head==NULL)
head=Binhead[j];
else
tail->next=Binhead[j];
tail=Bintail[j];
Binhead[j]=Bintail[j]=NULL;
};
}
cur=head;
};