Ultima modifica:
Eccomi qui con la mia versione. Ho utilizzato una lista ordinata bidirezionale.
Alcuni screenshot dell'esecuzione:
C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MODE_INSERT 0
#define MODE_FIND 1
#define MODE_REMOVE 2
struct Person
{
char* name;
char* last_name;
int age;
};
struct LLNode
{
struct Person data;
struct LLNode *next;
struct LLNode *prev;
};
struct LLNode *head = NULL;
int LLSize = 0;
void clean_input_buffer()
{
char c;
while ((c = getchar()) != '\n' && c != EOF);
}
void pause()
{
printf("\n\nPremere un tasto per continuare.. ");
getchar();
}
int get_int(char* message)
{
int input = 0;
printf("\n%s:\t", message);
scanf("%d", &input);
clean_input_buffer();
return input;
}
char* get_string(char* message)
{
char* input = (char*)malloc(1024);
printf("\n%s:\t", message);
scanf("%[^\n]%*c", input);
int len = strlen(input);
input[len] = 0;
return (char*)realloc(input, len + 1);
}
void print_list()
{
struct LLNode *node = head;
printf("\n\nLISTA PERSONE (%d):", LLSize);
while(node != NULL)
{
printf("\n%s\t%s\t%d", node->data.name, node->data.last_name, node->data.age);
node = node->next;
}
}
struct Person insert_person(char* name, char* last_name, int age)
{
struct Person person;
person.name = name;
person.last_name = last_name;
person.age = age;
struct LLNode *new_node = (struct LLNode*)malloc(sizeof(struct LLNode));
new_node->data = person;
if(head == NULL)
{
head = new_node;
}
else
{
struct LLNode *node = head;
int index = 0;
while(node != NULL)
{
if(node->data.age < new_node->data.age)
{
new_node->next = node;
node->prev = new_node;
if(index == 0)
{
head = new_node;
}
break;
}
if(node->next == NULL)
{
node->next = new_node;
break;
}
node = node->next;
index++;
}
}
LLSize++;
return person;
}
void find_people(char* name, char* last_name)
{
struct LLNode *node = head;
printf("\n\nRISULTATI:");
while(node != NULL)
{
if(strstr(node->data.name, name) ||
strstr(node->data.last_name, last_name))
{
printf("\n%s\t%s\t%d", node->data.name, node->data.last_name, node->data.age);
}
node = node->next;
}
}
struct Person* remove_person(char* name, char* last_name)
{
struct Person *person = NULL;
int index = 0;
struct LLNode *node = head;
while(node != NULL)
{
if(strstr(node->data.name, name) &&
strstr(node->data.last_name, last_name))
{
person = &node->data;
if(index == 0)
{
if(LLSize > 1)
{
head = node->next;
}
else {
head = NULL;
}
}
else
{
if(index < LLSize - 1)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
else
{
node->prev->next = NULL;
}
}
LLSize--;
break;
}
node = node->next;
index++;
}
return person;
}
void ask_person(int mode)
{
char* name;
char* last_name;
int age;
switch(mode)
{
case MODE_INSERT:
printf("\n\nINSERIMENTO NUOVA PERSONA\n");
name = get_string("DIGITARE IL NOME");
last_name = get_string("DIGITARE IL COGNOME");
age = get_int("DIGITARE L'ETA'");
struct Person inserted = insert_person(name, last_name, age);
printf("\n%s %s INSERITO!", inserted.name, inserted.last_name);
print_list();
break;
case MODE_FIND:
printf("\n\nRICERCA PERSONA\n");
name = get_string("DIGITARE IL NOME");
last_name = get_string("DIGITARE IL COGNOME");
find_people(name, last_name);
break;
case MODE_REMOVE:
printf("\n\nRIMOZIONE PERSONA\n");
name = get_string("DIGITARE IL NOME");
last_name = get_string("DIGITARE IL COGNOME");
struct Person *removed = remove_person(name, last_name);
if(removed == NULL)
{
printf("\nNESSUNA PERSONA TROVATA!");
break;
}
printf("\n%s %s RIMOSSO!", removed->name, removed->last_name);
print_list();
break;
}
}
int main() {
int action;
do
{
action = -1;
system("clear");
printf("__________ MENU __________\n\n");
printf("1. Inserisci una persona\n");
printf("2. Cerca delle persone\n");
printf("3. Rimuovi una persona\n");
printf("0. Esci\n");
printf("__________________________\n");
action = get_int("SELEZIONARE UN OPERAZIONE");
switch(action)
{
case 1:
ask_person(MODE_INSERT);
break;
case 2:
ask_person(MODE_FIND);
break;
case 3:
ask_person(MODE_REMOVE);
break;
}
if(action < 0 || action > 3)
{
printf("\nOPERAZIONE NON VALIDA!");
action = 1;
}
if(action != 0)
{
pause();
}
} while (action > 0);
printf("\nADDIO!");
return action;
}
Alcuni screenshot dell'esecuzione: