Hi this is the HW question, i can't understand the avail_list part can anyone explain??


(1) Write a program to implement indexing with avail_list support for deleted records as follows:
a. The program maintains a data file called students.txt of students’ records which contains studentID and studentName. These records of fixed length and not sorted.
b. The program uses an index file called index.txt to speed up the search for students’ records. The index contains the studentID and the RRN number. Access to a record is via a binary search technique.
c. The program uses an avail_list to keep track of the slots of deleted records. Newly inserted records replace the deleted records, which are pointed to by the avail_list. If no items in the avail_list, the new record is added to the end of a students.txt file and the index is updated and resorted.

Let's say you have an array {a, b, c, d, e} . Now c is deleted resulting in {a, b, (empty), d, e} . The avail_list logic would store index 2 such that adding a new item (f) would result in {a, b, f, d, e} rather than {a, b, (empty), d, e, f} . So f is added to index 2 (the first empty slot) instead of to a new index 5.

Edited 5 Years Ago by Narue: n/a

ok, if i deleted two items and added three items..
how do i perform that i mean how can i khow which index is empty and when the empty indexs are finish and when to start adding data at the append of index file
note that I'm using a studentData file and i have to create an index file..

>how can i khow which index is empty and when the empty indexs are
>finish and when to start adding data at the append of index file

That's what the avail_list is. It's a separate array/list of available indices. When a record is deleted, its index is added to the avail_list. When a new record is added, first search the avail_list for the next available index. If there's an available index, remove it from the avail_list and insert the record. Otherwise append the record.

Hi im still working on it..

how can i delete a record from the index file which i created in the program
it's a fixed length record..

i seek to the record which the user want to delete then what should i do??

How about storing the record index in your avail_list? If it's in the avail_list, clearly it doesn't have a legit record and whatever data is there can be overwritten.

I'm using student ID as a key in the index ex.(U00011221)
should i sort it??

and i want to mark the record as deleted what should i use??

>should i sort it??
It doesn't matter. I'd recommend keeping things simple until you have a working draft of the availability list.

>and i want to mark the record as deleted what should i use??
You don't need to mark the record as deleted. The presence of the search key/index in your avail_list serves that purpose.

Now for some advice. You're not going to learn jack until you take some chances, screw shit up, and get out of holes you dig for yourself. Your questions reek of fear. You're afraid of doing things the "wrong" way, or making mistakes, or having to start over from scratch because you made a poor decision in the beginning. But all of those are necessary to grow. The only way to get really good at this stuff is experience, and experience is nothing more than a mountain of mistakes. Don't expect people to hold your hand, because it's not going to happen, and it wouldn't help you in the long run anyway.

hhhhhhhhhhhhh...
yes u are right I'm new in this field and I realy NEED advice like that.. thanks

return to our subject..

this is the programe i come up with:

#include<iostream>
#include<fstream>

using namespace std;

int main()
{

	char ch;
	int i,delIndex[50];

	fstream infile;
	fstream outfile;

	infile.open("studentID.txt",ios::in);
	outfile.open("index.txt",ios::out);

	infile.unsetf( ios::skipws );

	for( i=0; i<10; i++)
	{

		infile>>ch;

		while( ch != ' ' )
		{
			outfile<<ch;
			infile>>ch;

		}

		outfile<<"  "<<i;
		infile.seekg( 11,ios::cur );

	}

	outfile<<endl;

	int num,j,d=0;

	bool found = false;

	do
	{
		
		cout<<" Choose one of these options:\n\n";

		cout<<"1) To delete a record.\n2) To add a record."
			<<"\n3) To end the loop.\n";

		cin>>num;

		if( num == 1)//delete the record
		{
			cout<<"\nEnter the index you want to delete from(0-9):";
			cin>>j;
			
			d++;
			delIndex[d] = j;

			//delete the record
			

		}
		else if( num == 2)//add record
		{
			
			cout<<"Enter the ID & name of the student:\n";
			
			if( d > 0 )
			{
				outfile.seekp( delIndex[d]*12, ios::beg );
			}
			else
				outfile.seekp( ios::app );
			

			for(int k=0; k<21; k++ )
			{
				cin>>ch;
				outfile<<ch;
			}
			outfile<<"  "<<i+1;
		
		}
		else if( num == 3)//end the loop
		{
			found = true;

		}


	}while( !found );



	return 0;


}

this is the "studentID.txt" file:

U00011211 Ahmed Adnan
U00011212 Fatima Ahme
U00011213 Yasir Moham
U00011214 Mohamed Nas
U00011215 Mona Kalid 
U00011216 Maha Abdull
U00011217 Nora Khalfa
U00011218 Shahad Yusi
U00011219 Adnan Sami 
U00011220 Ali Mohamed

the problem is with the 1st & 2nd choices..

I'm feeling generous today, so here's an example. Note that it doesn't use files, and the standard library is used for brevity, but all of the requisite logic is in place for the avail_list. You should have no trouble figuring out what I did and why. If not, feel free to ask and I'll be happy to explain any details:

#include <algorithm>
#include <deque>
#include <iostream>
#include <string>
#include <vector>

enum menu_opt {
    ADD_RECORD,
    DELETE_RECORD,
    PRINT_RECORDS,
    EXIT
};

menu_opt menu()
{
    std::cout<<"1) Add Record\n2) Delete Record\n3) Print Records\n4) Exit\n> ";

    std::string opt;

    while (getline(std::cin, opt)) {
        if (opt.empty())
            continue;

        switch (opt[0]) {
        case '1': return ADD_RECORD;
        case '2': return DELETE_RECORD;
        case '3': return PRINT_RECORDS;
        case '4': return EXIT;
        default:
            std::cout<<"Invalid option. Please try again> ";
        }
    }

    return EXIT;
}

int main()
{
    // Records are a simple string, for simplicity
    typedef std::vector<std::string> record_list_t;

    // The avail_list acts as a queue, so the first deleted index
    // is the first index used when adding new records
    typedef std::deque<record_list_t::size_type> avail_list_t;

    record_list_t records;
    avail_list_t avail;
    std::string record;
    menu_opt opt;

    while ((opt = menu()) != EXIT) {
        switch (opt) {
        case ADD_RECORD:
            std::cout<<"Search Key> ";

            if (getline(std::cin, record)) {
                // Check for deleted indices
                if (avail.empty())
                    records.push_back(record);
                else {
                    // Use the first available index
                    records[avail.back()] = record;

                    // Mark the index as used by removing it
                    avail.pop_back();
                }
            }

            break;
        case DELETE_RECORD:
            std::cout<<"Search Key> ";

            if (getline(std::cin, record)) {
                for (record_list_t::size_type i = 0; i < records.size(); i++) {
                    if (records[i] == record) {
                        // Be sure it's not already deleted before adding the index
                        if (find(avail.begin(), avail.end(), i) == avail.end())
                            avail.push_front(i);

                        break;
                    }
                }
            }

            break;
        case PRINT_RECORDS:
            for (record_list_t::size_type i = 0; i < records.size(); i++) {
                if (find(avail.begin(), avail.end(), i) == avail.end())
                    std::cout<< records[i] <<'\n';
            }

            break;
        }
    }
}

Edited 5 Years Ago by Narue: n/a

I can't read this program because I'm using C++ and it's on C maybe..

ok, for the program i wrote it's the same idea, I'm prompt the user to choose an option then if the user choose delete it should go to the index and assign it to the avil_list, i used an array here but i will change it to a linked_list and keep adding..

if the user choose to add, it should use the index of the last element in the avail_list and assign the record to it, and pop() one element from the avail_list..

Is this write??

if it is I'll try that again and post it..

>I can't read this program because I'm using C++
The code I posted is C++.

>Is this write [sic] ??
Yes.

This question has already been answered. Start a new discussion instead.