Taking the Smallest from a LL

This is a discussion on Taking the Smallest from a LL within the C++ Programming forums, part of the General Programming Boards category; Below is my Homemade Linked List. I need to implement a method in the class that will return an applicant ...

  1. #1
    Unregistered
    Guest

    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

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    381
    You need to put some form of testing in the loop.
    eg:
    Code:
    	applicant hire(void){
    		
    		cur=head;
    		applicant smallest = cur->data;
    
    		do{
                            if ((cur->data.date != 0) && (cur->data.date < smallest.date))
    				smallest = cur->data;
    
    			goNext();
    
    		}while(!cur->data.date);
    
    
    		return smallest;
    
    	}
    If you own a piece of land and there is an volcano on it and it ruins a
    nearby town, do you have to pay for the property damage?

  3. #3
    Unregistered
    Guest

    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;
    }

  4. #4
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187

    Presto ...

    Open the file like this -

    Code:
                fstream fs("2111112.aaa", ios::in | ios::out | ios::binary);

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    please post your whole code or/and errors

  6. #6
    Unregistered
    Guest
    It's alright pode, ginoitalo solved my problem,

    I though an fstream's default would be ios:ut and ios::in

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 06:44 PM
  2. Pointer Mayhem
    By thomas41546 in forum C Programming
    Replies: 3
    Last Post: 05-11-2008, 11:25 AM
  3. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 04:25 PM
  4. Finding the smallest value......HARD!!!
    By AssistMe in forum C Programming
    Replies: 21
    Last Post: 03-10-2005, 06:23 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-07-2002, 12:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21