All pastes #96581 Raw Edit

Miscellany

public cpp v1 · immutable
#96581 ·published 2006-07-23 00:18 UTC
rendered paste body
//Includes#include <stdio.h>#include <fcntl.h>#include <sys/stat.h>#include <string.h>#include <iostream>#include <fstream>using std::cout;using std::endl;using std::ios;using namespace std;int NUMBUCKETS;int OVERFLOW_BUCKET;class Record{public:	int key;	int data;	Record()	{		key = -1;		data = -1;	}};class Bucket{public:	int bucket_id;	int next_bucket;	Record *bucket_record;	Bucket()	{		bucket_record = new Record[4];		bucket_id = -1;		next_bucket = -1;	}};class Hash_Table {public:	int *index;	Bucket *hash_bucket;	Hash_Table()	{		index = new int[NUMBUCKETS];		hash_bucket = new Bucket[NUMBUCKETS];	}	int hash_function(int);};int Hash_Table::hash_function (int key){	int hash_index;	hash_index = key % NUMBUCKETS;	return hash_index;}void overflow_function(int new_bucket_id, int index_num, int key, int key_data, int full){	int j = 0;	Bucket new_bucket;	new_bucket.bucket_id = new_bucket_id;	for(int i = 0; i<4; i++)			{				j = -1;				if(new_bucket.bucket_record[i].data == -1)				{					j = i;					break;				}			}			if(j == -1)			{			 if (new_bucket.next_bucket == -1)			 {			 	 OVERFLOW_BUCKET++;			 	 cout << "Second overflow bucket, recurrsion" << endl;			 	  new_bucket.next_bucket = OVERFLOW_BUCKET;			 	  overflow_function(new_bucket.next_bucket, index_num, key, key_data, 1);		 	 }			 else			 	overflow_function(new_bucket.next_bucket,index_num, key, key_data, 0);			}			else			{				cout<< "new bucket id: " << new_bucket.bucket_id;				new_bucket.bucket_record[j].key= key;				cout << "  Key from Overflow: " << new_bucket.bucket_record[j].key;				new_bucket.bucket_record[j].data= key_data;				cout << "  Data from Overflow: " << new_bucket.bucket_record[j].data;		}}void write_head(){	//fstream file;	fstream file("file2.txt", ios_base::in);	file.open("file2.txt");	file << "its bad file" << endl;	if(!file)	{		cout << "File does not exist" << endl;		file.open("file2.txt");		file.write("Hello Motto\n", 12);		//file << "its bad file" << endl;		//file << NUMBUCKETS;	}	else	{		//file.close();		//file.open("file2.txt");		//cout << "its a good file" <<endl;		file.write("I dont think so Motto\n", 22);		//file << "writing to the file" << endl;		//cout << "file does exist" << endl;	}	file.close();}int main(){	int bucket_num = 0;	int insert_key;	int insert_data;	int key_index;	write_head();	cout << "\nEnter Bucket Number: ";	cin >> bucket_num;	NUMBUCKETS = bucket_num;	OVERFLOW_BUCKET = bucket_num;	Hash_Table hashing;		//Bucket creating_buckets;	for(int i = 0; i< NUMBUCKETS; i++)	{		hashing.index[i] = i;		hashing.hash_bucket[i].bucket_id = i + 1;	}	while (insert_key != 0 && insert_data != 0)	{		int j = -1;		cout << "\n Insert Key and its Data: ";		cin >> insert_key;		cin >> insert_data;		if (insert_key == 0 && insert_data == 0)			break;		key_index = hashing.hash_function(insert_key);		for(int i = 0; i<4; i++)		{			j = -1;			if(hashing.hash_bucket[key_index].bucket_record[i].data == -1)			{				j = i;				break;			}		}		if(j == -1)		{		 if (hashing.hash_bucket[key_index].next_bucket == -1)		 {		 	 OVERFLOW_BUCKET++;		 	  hashing.hash_bucket[key_index].next_bucket = OVERFLOW_BUCKET;		 	  overflow_function(hashing.hash_bucket[key_index].next_bucket, key_index, insert_key, insert_data, 1);	 	 }		 else		 	overflow_function(hashing.hash_bucket[key_index].next_bucket,key_index, insert_key, insert_data, 0);		}		else		{			//inserts the initial first values in the bucket			hashing.hash_bucket[key_index].bucket_record[j].key= insert_key;			hashing.hash_bucket[key_index].bucket_record[j].data= insert_data;		}	}	for(int i = 0; i< NUMBUCKETS; i++)	{		for(int g=0;g<4;g++)		{			cout << "Index: " << hashing.index[i];			cout << " Bucket: " << hashing.hash_bucket[i].bucket_id;			cout << " Record: " << g << " Key: " << hashing.hash_bucket[i].bucket_record[g].key;			cout << " Data: " << hashing.hash_bucket[i].bucket_record[g].data << endl;		}	}return 0;}