Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Programmatismos2
- ---project2.c : mia pio polyploki basi dedomenwn me dynamiki desmeusi mnimis.
- ---Onomatepwnymo : Sarafoglou Dimitrios
- ---AEM : 02993
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <signal.h>
- struct COURSE_CODE
- {
- short unsigned int code;
- struct COURSE_CODE *next;
- };
- typedef struct COURSE_CODE course_code;
- struct ENTRY
- {
- long unsigned int AEM;
- short unsigned int courses;
- char name[64];
- struct ENTRY *next_student;
- struct ENTRY *previous_student;
- course_code *root;
- };
- typedef struct ENTRY entry;
- typedef struct
- {
- entry **entries;
- entry **hash_table;
- int size;
- int hashTable_size;
- int capacity;
- int increaser;
- int decreaser;
- int isSorted;
- } entry_list;
- /* Ylopoihsh synarthsewn. */
- /* Initalization functions. */
- void init_entry_list(entry_list *el)
- {
- el->entries = NULL;
- el->capacity = 0;
- el->size = 0;
- el->increaser = 0;
- el->decreaser = 0;
- el->isSorted = 0;
- }
- void init_course_list(entry_list *el)
- {
- int i;
- for (i=0; i<el->size; i++)
- el->entries[i]->root = NULL;
- }
- void hash_table_init(entry_list *el)
- {
- int i;
- for (i=0; i<el->hashTable_size; i++)
- {
- el->hash_table[i] = (entry *) malloc(sizeof(entry));
- if (el->hash_table[i] == NULL)
- exit(0); /* No memory for allocation. */
- el->hash_table[i]->previous_student = el->hash_table[i];
- el->hash_table[i]->next_student = el->hash_table[i];
- }
- }
- void deleteHashTable(entry_list *el)
- {
- int i;
- for (i=0; i<el->hashTable_size; i++)
- free(el->hash_table[i]);
- free(el->hash_table);
- }
- void clear_hash_table(entry_list *el)
- {
- free(el->hash_table);
- }
- long unsigned int hash(char *str)
- {
- long unsigned int hash = 5381;
- int c;
- while ((c = *str++))
- hash = ((hash << 5) + hash) + c;
- return hash;
- }
- /* Binary search or linear search (in one). */
- int student_search(entry_list *el, long unsigned int AEM)
- {
- int i, middle, from = 0, to = el->size-1;
- if (el->isSorted == 0)
- {
- /* Edw ta AEM den einai taksinomimena, ara efarmozoume linear search. */
- for (i=0; i<el->size; i++)
- {
- if (AEM == el->entries[i]->AEM)
- {
- return 1;
- }
- }
- return 0;
- }
- else
- {
- /* Edw o pinakas me ta AEM einai taksinomimenos, opote efarmozoume binary search. */
- while(from <= to)
- {
- middle = (to + from) / 2;
- if (el->entries[middle]->AEM == AEM)
- {
- return 1;
- }
- else if (el->entries[middle]->AEM > AEM)
- {
- to = middle - 1;
- }
- else
- {
- from = middle + 1;
- }
- }
- return 0;
- }
- }
- int find_cmps(entry_list *el, long unsigned int AEM)
- {
- int i, middle, from = 0, to = el->size-1, comparisons = 0;
- if (el->isSorted == 0)
- {
- for (i=0; i<el->size; i++)
- {
- if (AEM == el->entries[i]->AEM)
- {
- comparisons++;
- break;
- }
- else
- {
- comparisons++;
- continue;
- }
- }
- return comparisons;
- }
- else
- {
- while (from <= to)
- {
- middle = (to + from) / 2;
- if (el->entries[middle]->AEM == AEM)
- {
- comparisons++;
- return comparisons;
- }
- comparisons++;
- if (el->entries[middle]->AEM > AEM)
- {
- comparisons++;
- to = middle - 1;
- }
- else
- {
- comparisons++;
- from = middle + 1;
- }
- }
- return comparisons;
- }
- }
- /* Find comparisons while sorting function.
- H synartisi epistrefei to plithos twn sygkrisewn. */
- int insertion_sort(entry_list *el, long unsigned int AEM)
- {
- int i, step, key_aem, key_course, comparisons = 0;
- char key_name[64] = {'\0'};
- for (step = 1; step < el->size; step++) /* step = 1: afinoume to prwto stoixeio, giati theoroume oti einai sorted. */
- {
- i = step - 1;
- key_aem = el->entries[step]->AEM;
- key_course = el->entries[step]->courses;
- strcpy(key_name, el->entries[step]->name);
- comparisons++;
- while(i>=0 && key_aem < el->entries[i]->AEM)
- {
- if (i == 0)
- {
- comparisons--;
- }
- comparisons++;
- el->entries[i+1]->AEM = el->entries[i]->AEM;
- el->entries[i+1]->courses = el->entries[i]->courses;
- strcpy(el->entries[i+1]->name, el->entries[i]->name);
- i--;
- if (i < 0)
- {
- break;
- }
- }
- el->entries[i+1]->AEM = key_aem;
- el->entries[i+1]->courses = key_course;
- strcpy(el->entries[i+1]->name, key_name);
- }
- el->isSorted = 1; /* Sorted. */
- return comparisons;
- }
- /* Swap. */
- void swap(course_code *a, course_code *b)
- {
- int temp;
- temp = a->code;
- a->code = b->code;
- b->code = temp;
- }
- /* Sorting of nodes(codes of courses). */
- void course_sorting(course_code *root)
- {
- int cont;
- course_code *ptr1, *ptr2 = NULL;
- /* Tsekaroume an i lista mas einai keni. */
- if (root == NULL)
- return;
- do
- {
- cont = 0;
- ptr1 = root;
- while (ptr1->next != ptr2)
- {
- if (ptr1->code > ptr1->next->code)
- {
- swap(ptr1,ptr1->next);
- cont = 1;
- }
- ptr1 = ptr1->next;
- }
- ptr2 = ptr1;
- } while(cont);
- }
- /* Find position of student function
- H synartisi epistrefei tin thesi tou foititi mesa stin agenta.
- */
- int find_pos_of_student(entry_list *el, long unsigned int AEM)
- {
- int position;
- for (position = 0; (position < el->capacity) && (el->entries[position] != NULL); position++)
- {
- if (el->entries[position]->AEM == AEM)
- break;
- }
- return position;
- }
- /* Hash Table's content is printed. */
- void print_hash_table(entry_list *el)
- {
- entry *current;
- course_code *current_course;
- int i;
- for (i=0; i<el->hashTable_size; i++)
- {
- if (el->hash_table[i]->next_student == el->hash_table[i]) /* Periptwsi opou exoume keno bucket. */
- continue;
- for (current=el->hash_table[i]->next_student; current!=el->hash_table[i]; current=current->next_student)
- {
- printf("----------------------------\n");
- printf("[Position in the Hash Table]: %d\n", i);
- printf("[Name]: %s\n", current->name);
- printf("[AEM]: %lu\n", current->AEM);
- printf("[Courses]: %hu\n", current->courses);
- printf("%s is registered in these courses:\n", current->name);
- for (current_course=current->root; current_course!=NULL; current_course=current_course->next)
- {
- printf("%hu\n", current_course->code);
- }
- }
- }
- }
- /* Find student in a circular list function.
- 1: If YES
- 0: If NO
- */
- int student_search_circular_list(entry_list *el, long unsigned int AEM, char name[])
- {
- entry *current;
- int position_in_HT = hash(name) % (el->hashTable_size);
- el->hash_table[position_in_HT]->AEM = AEM;
- for (current=el->hash_table[position_in_HT]->next_student; current!=el->hash_table[position_in_HT]; current=current->next_student)
- {
- if (current->AEM == AEM)
- return 1; /* O foititis yparxei idi sto hash table. */
- }
- return 0;
- }
- /* Rehashing functions. */
- void rehashing(entry_list *el)
- {
- /* Diagrafoume to hash table. */
- clear_hash_table(el);
- /* Desmeuoume ena neo me allagmeno megethos.
- To megethos eksartatai apo to load factor opote den allazei edw. */
- el->hash_table = (entry **) malloc(sizeof(entry *) * el->hashTable_size);
- if (el->hash_table == NULL)
- exit(0); /* No memory for allocation. */
- /* Kanoume arxikopoiisi tou hash table.
- Prepei na exoume kai ta sentinels. */
- hash_table_init(el);
- /* Edw ginetai to rehashing. */
- entry *current, *temp;
- int i, position_in_HT;
- for (i=0; i<el->size; i++)
- {
- /* Edw vriskoume ti nea thesi tou entry sto hash table. */
- position_in_HT = hash(el->entries[i]->name) % el->hashTable_size;
- /* Case1: Periptwsi opou to bucket me to sentinel einai keno. */
- if (el->hash_table[position_in_HT]->next_student == el->hash_table[position_in_HT])
- {
- /* Apla eisagoume to entry xwris kapoion periorismo. */
- el->hash_table[position_in_HT]->next_student = el->entries[i];
- el->entries[i]->previous_student = el->hash_table[position_in_HT];
- el->entries[i]->next_student = el->hash_table[position_in_HT];
- }
- else
- {
- /* Case2: Periptwsi opou prepei o foititis na mpei sth swsth thesi (sortarismeni lista diladi). */
- for (current=el->hash_table[position_in_HT]->next_student; current!=el->hash_table[position_in_HT]; current=current->next_student)
- {
- if (strcmp(el->entries[i]->name, current->name) < 0)
- break;
- if (strcmp(el->entries[i]->name, current->name) == 0)
- {
- if (el->entries[i]->AEM < current->AEM)
- break;
- else
- {
- while(strcmp(el->entries[i]->name, current->name) == 0)
- {
- current = current->next_student;
- if (el->entries[i]->AEM < current->AEM)
- break;
- }
- break;
- }
- }
- }
- /* Se auto to simeio ousiastika exoume enan deikti current pou deixnei se mia sygkekrimeni thesi sti dipli lista mas. */
- /* Case1: Periptwsi opou den eftasa sto telos tis listas.
- O foititis mpainei kapou endiamesa (H sthn arxh h sti mesi.) */
- if (current != el->hash_table[position_in_HT])
- {
- el->entries[i]->next_student = current;
- el->entries[i]->previous_student = current->previous_student;
- current->previous_student = el->entries[i];
- current->previous_student->previous_student->next_student = current->previous_student;
- }
- else
- {
- /* Case2: Periptwsi opou h eggrafi mpainei sto telos tis diplis listas.
- Emeis xreiazomaste enan deikti sto teleutaio stoixeio tis listas. */
- for (temp=el->hash_table[position_in_HT]->next_student; temp->next_student!=el->hash_table[position_in_HT]; temp=temp->next_student);
- el->entries[i]->previous_student = temp;
- el->entries[i]->next_student = temp->next_student;
- temp->next_student = el->entries[i];
- }
- }
- }
- }
- void rehashing2(entry_list *el, long unsigned int AEM)
- {
- /* Diagrafoume to hash table. */
- clear_hash_table(el);
- /* Desmeuoume ena neo me allagmeno megethos.
- To megethos eksartatai apo to load factor opote den allazei edw. */
- el->hash_table = (entry **) malloc(sizeof(entry *) * el->hashTable_size);
- if (el->hash_table == NULL)
- exit(0); /* No memory for allocation. */
- /* Kanoume arxikopoiisi tou hash table.
- Prepei na exoume kai ta sentinels. */
- hash_table_init(el);
- /* Edw ginetai to rehashing. */
- entry *current, *temp;
- int i, position_in_HT, position = find_pos_of_student(el, AEM);
- for (i=0; i<el->size; i++)
- {
- if (i == position)
- continue; /* Auto pou svisame den theloume na to ksanavaloume. */
- /* Edw vriskoume ti nea thesi tou entry sto hash table. */
- position_in_HT = hash(el->entries[i]->name) % el->hashTable_size;
- /* Case1: Periptwsi opou to bucket me to sentinel einai keno. */
- if (el->hash_table[position_in_HT]->next_student == el->hash_table[position_in_HT])
- {
- /* Apla eisagoume to entry xwris kapoion periorismo. */
- el->hash_table[position_in_HT]->next_student = el->entries[i];
- el->entries[i]->previous_student = el->hash_table[position_in_HT];
- el->entries[i]->next_student = el->hash_table[position_in_HT];
- }
- else
- {
- /* Case2: Periptwsi opou prepei o foititis na mpei sth swsth thesi (sortarismeni lista diladi). */
- for (current=el->hash_table[position_in_HT]->next_student; current!=el->hash_table[position_in_HT]; current=current->next_student)
- {
- if (strcmp(el->entries[i]->name, current->name) < 0)
- break;
- if (strcmp(el->entries[i]->name, current->name) == 0)
- {
- if (el->entries[i]->AEM < current->AEM)
- break;
- else
- {
- while(strcmp(el->entries[i]->name, current->name) == 0)
- {
- current = current->next_student;
- if (el->entries[i]->AEM < current->AEM)
- break;
- }
- break;
- }
- }
- }
- /* Se auto to simeio ousiastika exoume enan deikti current pou deixnei se mia sygkekrimeni thesi sti dipli lista mas. */
- /* Case1: Periptwsi opou den eftasa sto telos tis listas.
- O foititis mpainei kapou endiamesa (H sthn arxh h sti mesi.) */
- if (current != el->hash_table[position_in_HT])
- {
- el->entries[i]->next_student = current;
- el->entries[i]->previous_student = current->previous_student;
- current->previous_student = el->entries[i];
- current->previous_student->previous_student->next_student = current->previous_student;
- }
- else
- {
- /* Case2: Periptwsi opou h eggrafi mpainei sto telos tis diplis listas.
- Emeis xreiazomaste enan deikti sto teleutaio stoixeio tis listas. */
- for (temp=el->hash_table[position_in_HT]->next_student; temp->next_student!=el->hash_table[position_in_HT]; temp=temp->next_student);
- el->entries[i]->previous_student = temp;
- el->entries[i]->next_student = temp->next_student;
- temp->next_student = el->entries[i];
- }
- }
- }
- }
- /* Insert name to hash table function.
- Return 0: An o foititis iparxei idi sti lista.
- Return -1: An o foititis den yparxei sto database.
- Return 1: Success.
- */
- void entry_insert_to_HTable2(entry_list *el, long unsigned int AEM, char name[])
- {
- entry *current;
- float load_factor = el->size / el->hashTable_size;
- int position_in_HT = hash(name) % (el->hashTable_size); /* Thesi ston pinaka hash. */
- int position = find_pos_of_student(el, AEM); /* Thesi ston pinaka twn entries. */
- /* Periptwsi opou i lista mas einai keni. */
- if (el->hash_table[position_in_HT]->next_student == el->hash_table[position_in_HT])
- {
- el->hash_table[position_in_HT]->next_student = el->entries[position];
- el->entries[position]->previous_student = el->hash_table[position_in_HT];
- el->entries[position]->next_student = el->hash_table[position_in_HT];
- }
- else /* Periptwsi opou i lista den einai keni. */
- {
- /* Mesa edw kanw to sorting.
- Den pairnw periptwsi otan exw keni lista , giati den ginetai kapoiou eidous sortarisma. */
- for (current=el->hash_table[position_in_HT]->next_student; current!=el->hash_table[position_in_HT]; current=current->next_student)
- {
- if (strcmp(name, current->name) < 0)
- break;
- if (strcmp(name, current->name) == 0)
- {
- if (AEM < current->AEM)
- break;
- else /* Prepei na ginei enas elegxos gia to an meta exw entries me idia onomata. */
- {
- while(strcmp(name, current->name) == 0)
- {
- /* Ousiastika prepei na ftasw sto akrivws epomeno entry pou exei diaforetiko onoma. */
- current = current->next_student;
- if (AEM < current->AEM)
- break;
- }
- break;
- }
- }
- }
- /* Periptwsi opou i eggrafi mpainei stin arxi h se endiameso simeio tis listas tou bucket se sygkekrimeni thesi tou hash table. */
- if (current != el->hash_table[position_in_HT])
- {
- el->entries[position]->next_student = current;
- el->entries[position]->previous_student = current->previous_student;
- current->previous_student = el->entries[position];
- el->entries[position]->previous_student->next_student = el->entries[position];
- }
- else
- {
- /* Periptwsi opou prepei i eggrafi na mpei sto telos tis listas. */
- for (current=el->hash_table[position_in_HT]->next_student; current->next_student!=el->hash_table[position_in_HT]; current=current->next_student);
- el->entries[position]->previous_student = current;
- el->entries[position]->next_student = current->next_student;
- current->next_student = el->entries[position];
- }
- }
- if (load_factor >= 4)
- {
- el->hashTable_size = el->hashTable_size * 2;
- rehashing(el);
- }
- }
- void entry_remove_from_HT2(entry_list *el, long unsigned int AEM, char name[], int StartingHT_size)
- {
- float load_factor = el->size / el->hashTable_size;
- entry *current;
- int position_in_HT = hash(name) % el->hashTable_size;
- for (current=el->hash_table[position_in_HT]->next_student; current->AEM!=AEM; current=current->next_student);
- if (current != el->hash_table[position_in_HT])
- {
- /* Parakampsi komvou. */
- /* Periptwsi opou eksetazoume an to stoixeio pou theloume na diagrapsoume einai sto telos tis listas. */
- if (current->next_student == el->hash_table[position_in_HT])
- {
- current->previous_student->next_student = el->hash_table[position_in_HT];
- current->next_student = NULL;
- current->previous_student = NULL;
- }
- else
- {
- current->previous_student->next_student = current->next_student;
- current->next_student->previous_student = current->previous_student;
- current->previous_student = NULL;
- current->next_student = NULL;
- }
- }
- if (load_factor == 1)
- {
- if (el->hashTable_size / 2 < StartingHT_size)
- el->hashTable_size = StartingHT_size; /* To megethos tou epanaferetai sto arxiko. */
- else
- el->hashTable_size = el->hashTable_size / 2;
- rehashing2(el, AEM);
- }
- }
- void sort_circ_list(entry_list *el, char name[])
- {
- entry *current, *index;
- long unsigned int temp_AEM;
- short unsigned int temp_courses;
- char temp_name[64];
- course_code *temp_root;
- /* Prepei na vroume ti thesi sto hash table tis listas tin opoia theloume na sortaroume. */
- int position_in_HT = hash(name) % el->hashTable_size;
- current = el->hash_table[position_in_HT]->next_student;
- do {
- index = current->next_student;
- while(index != el->hash_table[position_in_HT]->next_student)
- {
- if (strcmp(current->name, index->name) > 0)
- {
- /* Edw tha ginei to swapping metaksy twn entries. */
- temp_AEM = current->AEM;
- current->AEM = index->AEM;
- index->AEM = temp_AEM;
- temp_courses = current->courses;
- current->courses = index->courses;
- index->courses = temp_courses;
- strcpy(temp_name, current->name);
- strcpy(current->name, index->name);
- strcpy(index->name, temp_name);
- temp_root = current->root;
- current->root = index->root;
- index->root = temp_root;
- }
- else if (strcmp(current->name, index->name) == 0)
- {
- /* Edw sortaroume me basi to AEM. */
- if (current->AEM > index->AEM)
- {
- temp_AEM = current->AEM;
- current->AEM = index->AEM;
- index->AEM = temp_AEM;
- temp_courses = current->courses;
- current->courses = index->courses;
- index->courses = temp_courses;
- temp_root = current->root;
- current->root = index->root;
- index->root = temp_root;
- /* Ta onomata den xreiazontai swapping, giati einia idia. */
- }
- }
- index = index->next_student;
- }
- current = current->next_student;
- } while(current->next_student != el->hash_table[position_in_HT]->next_student);
- }
- /* Add student function.
- H synartisi auti epistrefei 1: An o foititis prostethike epityxws.
- H synartisi auti epistrefei 0: An o foititis yparxei idi.
- */
- int add(entry_list *el, long unsigned int AEM, short unsigned int courses, char name[])
- {
- entry **temp;
- int i, length;
- int finder;
- finder = student_search(el, AEM);
- if (finder == 1)
- {
- /* Simainei oti o foititis yparxei idi. */
- return 0;
- }
- else
- {
- /* Prwta eksetazoume tin periptwsi opou den yparxei xwros ston pinaka deiktwn. */
- if (el->capacity == el->size)
- {
- temp = (entry **) realloc(el->entries, (el->capacity + el->increaser) * sizeof(entry *));
- if (temp == NULL)
- {
- /* No memory for allocation!\n */
- exit(0);
- }
- /* Desmeuoume xwro gia tin yparksi neas eggrafis. */
- entry *student;
- student = (entry *) malloc(sizeof(entry));
- if (student == NULL)
- {
- /* No memory for allocation... */
- exit(0);
- }
- el->entries = temp;
- /* for (i=el->capacity; i<(el->capacity+el->increaser); i++)
- {
- el->entries[i]->root = NULL;
- }*/
- el->capacity += el->increaser;
- el->entries[el->size] = student;
- el->entries[el->size]->AEM = AEM;
- el->entries[el->size]->courses = courses;
- /* Metatropi tou onomatos se kefalaia. */
- length = strlen(name);
- for (i=0; i<length; i++)
- {
- name[i] = toupper(name[i]);
- }
- strcpy(el->entries[el->size]->name, name);
- el->entries[el->size]->root = NULL;
- //el->entries[el->size]->next_student = NULL;
- el->size++;
- el->isSorted = 0;
- entry_insert_to_HTable2(el, AEM, name);
- //sort_circ_list(el, name);
- return 1;
- }
- /* Twra eksetazoume tin periptwsi opou yparxei eleutheros xwros ston pinaka deiktwn. */
- /* Desmeuw xwro gia mia nea eggrafi. */
- entry *student;
- student = (entry *) malloc(sizeof(entry));
- if (student == NULL)
- {
- /* No memory for allocation... */
- exit(0);
- }
- el->entries[el->size] = student;
- el->entries[el->size]->AEM = AEM;
- el->entries[el->size]->courses = courses;
- el->entries[el->size]->root = NULL;
- el->entries[el->size]->next_student = NULL;
- /* Metatropi tou onomatos se kefalaia. */
- length = strlen(name);
- for (i=0; i<length; i++)
- {
- name[i] = toupper(name[i]);
- }
- strcpy(el->entries[el->size]->name, name);
- el->size++;
- el->isSorted = 0;
- entry_insert_to_HTable2(el, AEM, name);
- //sort_circ_list(el, name);
- return 1;
- }
- }
- /* Find course function.
- H synartisi epistrefei 1: an to mathima vrethike.
- H synartisi epistrefei 0: an to mathima den vrethike. */
- int find_course(entry_list *el, short unsigned int code, long unsigned int AEM)
- {
- course_code *current;
- if (student_search(el, AEM) == 0) /* Pou Simainei oti o foititis den vrethike. */
- return 0; /* Oute kai to mathima tha yparxei epomenws. */
- /* An o foititis yparxei, prepei na vroume ti thesi tou. */
- int position = find_pos_of_student(el, AEM);
- for (current=el->entries[position]->root; current!=NULL; current=current->next)
- {
- if (current->code == code)
- return 1;
- }
- return 0;
- }
- /* isreg function.
- Epistrefetai: 1 an einai eggegrammenos.
- 0 an den yparxei sto database.
- -1 an den einai eggegrammenos.*/
- int isRegistered(entry_list *el, short unsigned int code, long unsigned int AEM)
- {
- int finder = student_search(el, AEM);
- if (finder == 0)
- return -1;
- int position = find_pos_of_student(el, AEM);
- course_code *current;
- for (current=el->entries[position]->root; current!=NULL; current=current->next)
- {
- if (current->code == code)
- return 1;
- }
- return 0;
- }
- /* Insert course node to the list function (Eggrafi foititi se mathima).
- H synartisi auti epistrefei 1 an i eisagwgi tou node stin lista egine me epityxia,
- alliws 0 se periptwsi pou o mathitis den vrisketai sto database kai -1 se periptwsi
- pou einai idi eggegrammenos. */
- int course_insert(entry_list *el, short unsigned int code, long unsigned int AEM)
- {
- int finder = student_search(el, AEM);
- if (finder == 0) /* Den yparxei o mathitis. */
- return 0; /* Den mporw na prosthesw mathima. */
- int find_crs = find_course(el, code, AEM);
- if (find_crs == 1) /* O mathitis einai eggegrammenos. */
- return -1;
- int position = find_pos_of_student(el, AEM);
- course_code *current;
- current = (course_code *) malloc(sizeof(course_code));
- if (current == NULL)
- exit(0); /* No memory for allocation. */
- current->code = code;
- current->next = el->entries[position]->root;
- el->entries[position]->root = current;
- /* Twra sortaroume. */
- course_sorting(el->entries[position]->root);
- return 1; /* inserted. */
- }
- /* Remove course function.
- H synartisi auti epistrefei 1: an petyxe h apomakrynsi komvou.
- H synartisi auti epistrefei 0: an o foititis den yparxe sto database.
- H synartisi auti epistrefei -1: an to mathima pou theloume na afairesoume den yparxei.*/
- int remove_course(entry_list *el, short unsigned int code, long unsigned int AEM)
- {
- int finder = student_search(el, AEM);
- if (finder == 0) /* O foititis den yparxei sto database. */
- return 0;
- course_code *current, *previous;
- int position = find_pos_of_student(el, AEM);
- for (current=el->entries[position]->root; current!=NULL; previous=current,current=current->next)
- {
- if (current->code == code)
- break;
- }
- if (current != NULL)
- {
- if (current == el->entries[position]->root)
- el->entries[position]->root = current->next;
- else
- previous->next = current->next;
- }
- else
- {
- return -1;
- }
- free(current);
- return 1; /* removed. */
- }
- /* Remove student function
- H synartisi auti epistrefei 1: an o foititis afairethike me epityxia.
- H synartisi auti epistrefei 0: an to AEM to foititi den yparxei sti lista h an exw kenh lista me entries h an h apodeseusi tou pinaka apetyxe.
- */
- int remove_student(entry_list *el, long unsigned int AEM, int StartingHT_size)
- {
- int finder, position, i, empty_positions = 0;
- entry **temp;
- if (el->size == 0)
- {
- /* Periptwsi opou den exw kanena entry, opote den mporw na afairesw tpt. */
- return 0;
- }
- /* Periptwsi opou den yparxei o mathitis, ara den mporw na ton afairesw. */
- finder = student_search(el, AEM);
- if (finder == 0)
- {
- return 0; /* O foititis den vrethike. */
- }
- /* Periptwsi opou o mathitis yparxei, kai thelw na ton afairesw. */
- position = find_pos_of_student(el, AEM);
- entry_remove_from_HT2(el, AEM, el->entries[position]->name, StartingHT_size);
- if (position == el->size-1)
- {
- /* Simainei oti h eggrafi vrisketai stin teleutaia thesi tis listas eggrafwn. */
- /* Prwta prepei na kanoume apodesmeusi tous komvous. */
- /* Kai meta apodesmeusi tou struct tou foititi. */
- course_code *current;
- while (el->entries[position]->root != NULL)
- {
- current = el->entries[position]->root;
- el->entries[position]->root = el->entries[position]->root->next;
- free(current);
- }
- // Me to pou vgw apo tin loopa: el->entries[position]->root = NULL.
- free(el->entries[position]);
- el->entries[position] = NULL;
- el->size--;
- /* Vriskoume poses kenes (NULL) theseis exei o pinakas deiktwn. */
- for (i=0; i<el->capacity; i++)
- {
- if (el->entries[i] != NULL)
- continue;
- else
- empty_positions++;
- }
- if (empty_positions > el->decreaser)
- {
- temp = (entry **) realloc(el->entries, (el->capacity - el->decreaser) * sizeof(entry));
- if (temp == NULL)
- {
- /* realloc() failed... */
- exit(0);
- }
- el->entries = temp;
- el->capacity -= el->decreaser;
- }
- return 1; /* removed */
- }
- else
- {
- /* Periptwsi opou o foititis den vrisketai stin teleutaia thesi. */
- /* Prepei na apodesmeusoume tous komvous gia ta mathimata. */
- course_code *current;
- while (el->entries[position]->root != NULL)
- {
- current = el->entries[position]->root;
- el->entries[position]->root = el->entries[position]->root->next;
- free(current);
- }
- free(el->entries[position]);
- el->entries[position] = el->entries[el->size-1];
- el->entries[el->size-1] = NULL;
- el->size--;
- /* Vriskoume poses kenes (NULL) theseis exei o pinakas deiktwn. */
- for (i=0; i<el->capacity; i++)
- {
- if (el->entries[i] != NULL)
- continue;
- else
- empty_positions++;
- }
- if (empty_positions > el->decreaser)
- {
- temp = (entry **) realloc(el->entries, (el->capacity - el->decreaser) * sizeof(entry));
- if (temp == NULL)
- {
- /* realloc() failed... */
- exit(0);
- }
- el->entries = temp;
- el->capacity -= el->decreaser;
- }
- el->isSorted = 0;
- return 1; /* removed */
- }
- }
- /* Find by name function. */
- int find_by_name(entry_list *el, char name[])
- {
- entry *current;
- int i, found = 0;
- int length = strlen(name);
- for (i=0; i<length; i++) /* Meatrepoume to onoma se kefalaia giati etsi tha to sinantisoume mesa sto hash table. */
- name[i] = toupper(name[i]);
- for (i=0; i<el->hashTable_size; i++)
- {
- for (current=el->hash_table[i]->next_student; current!=el->hash_table[i]; current=current->next_student)
- {
- if (strcmp(current->name, name) != 0) /* To onoma pou sinantisame den einai auto pou psaxnoume. */
- continue;
- else
- {
- found++;
- printf("\nN-OK %s\n", current->name);
- printf("%lu %hu\n", current->AEM, current->courses);
- while (strcmp(current->name, name) == 0)
- {
- current = current->next_student;
- if (strcmp(current->name, name) == 0)
- {
- printf("%lu %hu\n", current->AEM, current->courses);
- }
- }
- break;
- }
- }
- }
- if (found == 0)
- return 0;
- else
- return 1;
- }
- /* Modify courses based on AEM function.
- H synartisi epistrefei 1: an epiteuxthei h tropopoiisi.
- H synartisi epistrefei 0: an h tropoiisi den epiteuxthei h an o foititis gia ton opoio theloume na kanoume tis allages den yparxei sti lista mas. */
- int course_modify(entry_list *el, const long unsigned int AEM, short unsigned int courses)
- {
- int finder, position;
- finder = student_search(el, AEM);
- if (finder == 0)
- {
- /* Simainei oti o foititis den yparxei sti lista. */
- return 0;
- }
- /* O foititis yparxei sti lista. Prepei na vrethei i thesi tou. */
- position = find_pos_of_student(el, AEM);
- el->entries[position]->courses = courses;
- return 1; /* changed. */
- }
- /* List courses function. */
- void list_courses(entry_list *el, short unsigned int code, long unsigned int AEM)
- {
- int position = find_pos_of_student(el, AEM);
- course_code *current;
- printf("\nL-OK %s\n", el->entries[position]->name);
- for (current=el->entries[position]->root; current!=NULL; current=current->next)
- printf("%d\n", current->code);
- }
- /* Print entries function.
- H synartisi epistrefei void. */
- void print_entries(entry_list *el)
- {
- int i;
- printf("\n#\n");
- for (i=0; i<el->size; i++)
- printf("%lu %s %hu\n", el->entries[i]->AEM, el->entries[i]->name, el->entries[i]->courses);
- }
- /* Clear function. */
- void clear_entries(entry_list *el)
- {
- int i;
- course_code *current;
- /* Arxika prepei na diagrapsoume oles tis eggrafes.
- Meta ton pinaka deiktwn. */
- for (i=0; i<el->capacity; i++)
- {
- if (el->entries[i] == NULL)
- {
- free(el->entries[i]);
- continue;
- }
- /* Arxika prepei na diagrapsoume ti lista tou kathe foititi. */
- while (el->entries[i]->root != NULL)
- {
- current = el->entries[i]->root;
- el->entries[i]->root = el->entries[i]->root->next;
- free(current);
- }
- free(el->entries[i]);
- }
- free(el->entries);
- el->size = 0;
- el->capacity = 0;
- el->entries = NULL;
- el->isSorted = 0;
- }
- /* Main. */
- int main(int argc, char **argv)
- {
- long unsigned int AEM = 0;
- short unsigned int courses = 0, code;
- char name[64] = {'\0'}, menu_choice;
- int add_tester, remove_tester, mod_tester, find_tester, position, comparisons = 0, sorting_comparisons = 0;
- int reg_tester, unreg_tester, isReg_tester;
- if (argc != 4)
- {
- printf("Invalid command-line arguments!\n");
- return 0;
- }
- entry_list el;
- el.capacity = atoi(argv[1]);
- el.entries = (entry **) malloc(sizeof(entry *) * el.capacity); /* Desmeusame xwro gia ton pinaka deiktwn dynamika. */
- if (el.entries == NULL)
- exit(0); /* No memory for allocation. */
- init_entry_list(&el);
- init_course_list(&el);
- el.decreaser = atoi(argv[2]);
- el.increaser = atoi(argv[2]);
- el.hashTable_size = atoi(argv[3]);
- el.hash_table = (entry **) malloc(sizeof(entry *) * el.hashTable_size); /* Desmeuoume xwro gia to hash table. */
- if (el.hash_table == NULL)
- exit(0); /* No memory for allocation. */
- hash_table_init(&el);
- while(1)
- {
- scanf(" %c", &menu_choice);
- switch(menu_choice)
- {
- case 'a':
- {
- scanf("%lu %63s %hu", &AEM, name, &courses);
- add_tester = add(&el, AEM, courses, name);
- if (add_tester == 0)
- {
- /* Simainei oti den petyxe h eggrafi. */
- printf("\nA-NOK %lu, %d %d\n", AEM, el.size, el.capacity);
- }
- else
- {
- printf("\nA-OK %lu, %d %d\n", AEM, el.size, el.capacity);
- }
- break;
- }
- case 'g':
- {
- scanf("%lu %hu", &AEM, &code);
- reg_tester = course_insert(&el, code, AEM);
- if (reg_tester == 1)
- printf("\nG-OK %lu %hu\n", AEM, code);
- else if (reg_tester == 0)
- printf("\nG-NOK %lu\n", AEM);
- else
- printf("\nG-NOK %hu\n", code);
- break;
- }
- case 'r':
- {
- scanf("%lu", &AEM);
- remove_tester = remove_student(&el, AEM, el.hashTable_size);
- if (remove_tester == 1)
- {
- printf("\nR-OK %lu, %d %d\n", AEM, el.size, el.capacity);
- }
- else
- {
- printf("\nR-NOK %lu, %d %d\n", AEM, el.size, el.capacity);
- }
- break;
- }
- case 'u':
- {
- scanf("%lu %hu", &AEM, &code);
- unreg_tester = remove_course(&el, code, AEM);
- if (unreg_tester == 1)
- printf("\nU-OK %lu %hu\n", AEM, code);
- else if (unreg_tester == 0)
- printf("\nU-NOK %lu\n", AEM);
- else
- printf("\nU-NOK %hu\n", code);
- break;
- }
- case 's':
- {
- sorting_comparisons = insertion_sort(&el, AEM);
- printf("\nS-OK\n");
- fprintf(stderr, "\n$%d\n", sorting_comparisons);
- break;
- }
- case 'i':
- {
- scanf("%lu %hu", &AEM, &code);
- isReg_tester = isRegistered(&el, code, AEM);
- if (isReg_tester == 1)
- {
- printf("\nYES\n");
- }
- else if (isReg_tester == 0)
- {
- printf("\nNO\n");
- }
- else
- {
- printf("\nI-NOK %lu\n", AEM);
- }
- break;
- }
- case 'f':
- {
- scanf("%lu", &AEM);
- find_tester = student_search(&el, AEM);
- if (find_tester == 1)
- { position = find_pos_of_student(&el, AEM);
- printf("\nF-OK %s %hu\n", el.entries[position]->name, el.entries[position]->courses);
- comparisons = find_cmps(&el, AEM);
- fprintf(stderr, "\n$%d\n", comparisons);
- }
- else
- {
- printf("\nF-NOK %lu\n", AEM);
- comparisons = find_cmps(&el, AEM);
- fprintf(stderr, "\n$%d\n", comparisons);
- }
- break;
- }
- case 'n':
- {
- scanf("%63s", name);
- int found = find_by_name(&el, name);
- if (found == 1)
- break;
- else
- printf("\nN-NOK %s\n", name);
- break;
- }
- case 'm':
- {
- scanf("%lu %hu", &AEM, &courses);
- mod_tester = course_modify(&el, AEM, courses);
- if (mod_tester == 1)
- {
- printf("\nM-OK %lu\n", AEM);
- }
- else
- {
- printf("\nM-NOK %lu\n", AEM);
- }
- break;
- }
- case 'p':
- {
- print_entries(&el);
- break;
- }
- case 'h':
- {
- print_hash_table(&el);
- break;
- }
- case 'l':
- {
- scanf("%lu", &AEM);
- find_tester = student_search(&el, AEM);
- if (find_tester == 0) /* O foititis den yparxei sto database. */
- printf("\nL-NOK %lu\n", AEM);
- else
- {
- list_courses(&el, code, AEM);
- }
- break;
- }
- case 'c':
- {
- deleteHashTable(&el); /* Ousiastika edw digrafoume ton pinaka katakermatismou. */
- el.hash_table = (entry **) malloc(sizeof(entry *) * atoi(argv[3]));
- if (el.hash_table == NULL)
- exit(0);
- hash_table_init(&el);
- clear_entries(&el);
- printf("\nC-OK\n");
- break;
- }
- case 'q':
- {
- clear_entries(&el);
- clear_hash_table(&el);
- //deleteHashTable(&el);
- return 0;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement