Taking the Smallest from a LL
Below is my Homemade Linked List.
I need to implement a method in the class that will return an
applicant with the smallest number in their date attrib.
Applicants with a date of zero don't count and once an applicant is returned it's date should be set to zero indicating that i've hired them ( hopefully as a programmer :) )
The Method (doesn't work):
Code:
applicant hire(void){
applicant tmp;
cur=head;
do{
tmp=cur->data;
if(cur->data.date)
cur->data.date=0;
goNext();
}while(!cur->data.date);
return tmp;
}
The Structure:
Code:
struct applicant {
char name[61]; // applicant's name
char phone[21]; // phone number
long date; // date of application in yyyymmdd
operator=(const applicant rhs){
strcpy(name, rhs.name);
strcpy(phone, rhs.phone);
date = rhs.date;
}
};
struct Node{
applicant data;
Node *next, *prev;
Node(){next=prev=NULL;};
Node(applicant data){
this->data = data;
next = prev = NULL;
}
};
class jobfile:public fstream{
protected:
int success, records;
char FileName[30];
Node *cur, *tail, *head;
public:
//Misc. Methods
This Simple Write Doesn't "Get it back"
Code:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
struct applicant {
char name[61]; // applicant's name
char phone[21]; // phone number
long date; // date of application in yyyymmdd format
applicant(){}
applicant &operator=(applicant x){
date=x.date;
strcpy(name, x.name);
strcpy(phone, x.phone);
return *this;
}
};
int main(void){
fstream fs("2111112.aaa", ios::binary);
applicant x;
x.date=9;
strcpy(x.name, "Chief");
strcpy(x.phone, "13281");
cout<<endl<<"--------------"<<endl<<"Name="<<x.name<<endl<<"Date="<<x.date<<endl
<<"Phone="<<x.phone<<endl<<"--------------"<<endl;
fs.seekp(ios::beg);
fs.write( (char*)&x, sizeof(applicant) );
fs.seekg(ios::end);
int recs = ( fs.tellg() / sizeof(applicant) );
cout<<"recs="<<recs<<endl;
applicant in;
fs.seekg(ios::beg);
fs.read((char*)&in, sizeof(applicant) );
cout<<endl<<"--------------"<<endl<<"Name="<<in.name<<endl<<"Date="<<in.date<<en
dl<<"Phone="<<in.phone<<endl<<"--------------"<<endl;
return 0;
}