You are on page 1of 3

#include <stdio.

h>
#include <stdlib.h>
//https://codeshare.io/RbPY9o
typedef struct {
float x;
float y;
float z;
struct point_3d_t *next; //per collegare...
} point_3d_t;

void print_list(point_3d_t *);


point_3d_t* append(point_3d_t *, point_3d_t);
point_3d_t* insert_at_head(point_3d_t *, point_3d_t);
point_3d_t* insert_at_position(point_3d_t *, point_3d_t, unsigned int);

int main(int argc, char **argv){


//creare 3 strutture di tipo point_3d_t che siano collegate...
point_3d_t *p1 = NULL, *p2 = NULL, *p3 = NULL, *head = NULL;
point_3d_t p_data = {1.2F, 4.5F, 5.6F, NULL };
p1 = (point_3d_t *)malloc( sizeof(point_3d_t));
p2 = (point_3d_t *)malloc( sizeof(point_3d_t));
p3 = (point_3d_t *)malloc( sizeof(point_3d_t));
if( p1 == NULL || p2 == NULL || p3 == NULL){
printf("ahi ahi malloc err\n");
exit(1);
}
p1->x = 0.0F; p1->y = 1.0F; p1->z = 2.0F;
p2->x = 1.0F; p2->y = 2.0F; p2->z = 3.0F;
p3->x = 2.0F; p3->y = 3.0F; p3->z = 4.0F;
//collegare head -> p1 -> p2 -> p3 -> NULL
head = p1;
p1->next = p2;
p2->next = p3;
p3->next = NULL;
print_list(head);
append(head, p_data);
print_list(head);
head = NULL;
print_list(head);
printf("%p\n", head);
head = append(head, p_data);
printf("%p\n", head);
head = append(head, p_data);
printf("%p\n", head);
head = append(head, p_data);
printf("%p\n", head);
print_list(head);
head = NULL;
printf("**** Inserimento in testa ****\n");
print_list(head);
printf("%p\n", head);
//inserisco il primo elemento in testa...
head = insert_at_head(head, p_data);
print_list(head);
printf("%p\n", head);
//inserire un altro elemento in testa....
p_data.x = -9999.0F; p_data.y = -9999.0F; p_data.z = -9999.0F;
head = insert_at_head(head, p_data);
print_list(head);
printf("%p\n", head);
p_data.x = 9999.0F; p_data.y = 9999.0F; p_data.z = 9999.0F;
head = append(head, p_data);
print_list(head);
printf("%p\n", head);
head = NULL;
print_list(head);
printf("%p\n", head);
p_data.x =0.0F; p_data.y = 0.0F; p_data.z = 0.0F;
head = append(head, p_data);
print_list(head);
printf("%p\n",head);
p_data.x =9999.0F; p_data.y = 9999.0F; p_data.z = 9999.0F;
head = append(head, p_data);
p_data.x =229999.0F; p_data.y = 229999.0F; p_data.z = 229999.0F;
head = append(head, p_data);
p_data.x =339999.0F; p_data.y = 9999.0F; p_data.z = 9999.0F;
head = append(head, p_data);
p_data.x =12999.0F; p_data.y = 12.0F; p_data.z = 15.0F;
head = append(head, p_data);
print_list(head);
printf("%p\n",head);
head = insert_at_position(head, p_data,0);
p_data.x =-1.0F; p_data.y = -3.0F; p_data.z = -5.0F;
head = insert_at_position(head, p_data,2);
return 0;
}

void print_list(point_3d_t *item_p){

while(item_p != NULL){
printf("%f %f %f\n", item_p->x, item_p->y, item_p->z);
item_p = item_p->next;
}
return;
}

point_3d_t* append(point_3d_t *head, point_3d_t value){


point_3d_t *tmp = NULL, *p = head;

//creo il nuovo elemento....


tmp = (point_3d_t *)malloc(sizeof(point_3d_t));
if(tmp == NULL){
printf("err malloc\n");
exit(1);
} //dumpinggg
tmp->x = value.x;
tmp->y = value.y;
tmp->z = value.z;
tmp->next = NULL; //nessun altro collegamento dopo di lui...
if(head == NULL){
return tmp;
}
while(p->next != NULL){ //scorro fino a trovare riferimento all'ultimo blocco..
p = p -> next;
}
//qui so che head->next è pari a null;
//creo un nuovo elemento, copio i valori e poi collego il tutto...
p->next = tmp; //collegamento
return head; //ritorna la testa originaria....
}

point_3d_t* insert_at_head(point_3d_t *head, point_3d_t value){


point_3d_t *tmp = NULL;
//creo il nuovo elemento....
tmp = (point_3d_t *)malloc(sizeof(point_3d_t));
if(tmp == NULL){
printf("err malloc\n");
exit(1);
} //dumping
tmp->x = value.x;
tmp->y = value.y;
tmp->z = value.z;
tmp->next = NULL; //nessun altro collegamento dopo di lui..
if(head == NULL){
return tmp; //caso della lista vuota...
}
//aggiorno la testa...
tmp->next = head; //collega l'elemento creato con la lista esistente...
return tmp;
/*head = tmp;
return head;
*/
}

point_3d_t* insert_at_position(point_3d_t *head, point_3d_t value, unsigned int


position){
point_3d_t *tmp = NULL, *p = head, *prev = head;
unsigned int curr_pos = 0U;
//creo il nuovo elemento....
tmp = (point_3d_t *)malloc(sizeof(point_3d_t));
if(tmp == NULL){
printf("err malloc\n");
exit(1);
} //dumping
tmp->x = value.x;
tmp->y = value.y;
tmp->z = value.z;
tmp->next = NULL; //nessun altro collegamento dopo di lui..
if(head == NULL){
return tmp; //caso della lista vuota...
}
//gestione dei vari casi....
if(position == 0U){
//inserisco in testa e ritorno direttamente la nuova testa
return insert_at_head(head, value);
}
//caso intermedio...testa da qui in poi non cambia
//per questo motivo usiamo p per avanzare...
while(curr_pos < position){
//position -1 ?
//attenzione se mi trovo come ultimo elemento...
//tengo traccia dell'elemento precedente
prev = p;
p = p->next;
curr_pos++;
}
}

You might also like