Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include <ctype.h>
- //this shit is shared between functions
- struct Node{
- int value;
- struct Node* next;
- };
- int count; //number of unique addresses
- int size; //size of the hashtable
- int idx;
- static struct Node* hashTable1;
- struct Node* hT1; //this pointer points to the front of a hash table, allowing the hashtable to be passed and returned
- //initialize load factor
- struct Node* ptr; //used in rebalance() to keep track of which node we look at
- struct Node* prv;
- struct Node* del;
- int data;
- int insert(int data){
- //invalid data
- if(data == -1){
- return 0;
- }
- hT1 = hashTable1;
- for(int i = idx; i!=0; i--){
- hT1++;
- }
- if(hT1 == NULL){
- hT1 = malloc(sizeof(struct Node));
- hT1-> value = data;
- hT1-> next = NULL;
- return 1;
- }
- while(ptr != NULL){
- if(data == ptr->value){
- return 0;
- }
- prv = ptr;
- ptr = ptr-> next;
- }
- prv->next = malloc(sizeof(struct Node));
- ptr = prv->next;
- ptr->value = data;
- ptr->next = NULL;
- return 1;
- }
- //rebalance -accepts hashtable
- //main -accepts file -declares hashtable -declarations
- int main (int argc, char** argv){
- if(argc !=2){
- printf("error\n");
- return -1;
- }
- size = 10000;
- count = 0;
- hashTable1 = malloc(size*sizeof(struct Node*));
- int data;
- FILE *fp;
- fp = fopen(argv[1], "r");
- if(fp == NULL){
- printf("error\n");
- return -1;
- }
- //insert values into the hashtable
- while(fscanf(fp, "%d", &data)!= EOF){
- idx = data%size;
- for(int i = idx; i!=0; i--){
- hT1++;
- }
- count += insert(data);
- }
- fclose(fp);
- ptr = hashTable1;
- //ptr stays on the main array, ptr++ after every iteration of the main for looop
- //
- for(int f = size; f!=0; f--){
- }//free's the buckets
- printf("%d",count);
- return 0;
- };//end of main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement