#include #include #include struct myNode { int val; struct myNode *next; }; struct myNode *head = NULL; main(){ struct myNode *pNode = (struct myNode*)malloc(sizeof(struct myNode)); pNode->val=42; pNode->next=NULL; head = pNode; pNode = (struct myNode*)malloc(sizeof(struct myNode)); pNode->val=71; pNode->next=head; head = pNode; //ciclo sulla lista e visualizzazione struct myNode* pn; pn = head; while (pn){ printf("%d ",pn->val); pn=pn->next; } } /* struct myNode *curr = NULL; struct myNode* create_list(int val) { printf("\n creating list with headnode as [%d]\n",val); struct myNode *ptr = (struct myNode*)malloc(sizeof(struct myNode)); if(NULL == ptr) { printf("\n Node creation failed \n"); return NULL; } ptr->val = val; ptr->next = NULL; head = curr = ptr; return ptr; } struct myNode* add_to_list(int val, bool add_to_end) { if(NULL == head) { return (create_list(val)); } if(add_to_end) printf("\n Adding node to end of list with value [%d]\n",val); else printf("\n Adding node to beginning of list with value [%d]\n",val); struct myNode *ptr = (struct myNode*)malloc(sizeof(struct myNode)); if(NULL == ptr) { printf("\n Node creation failed \n"); return NULL; } ptr->val = val; ptr->next = NULL; if(add_to_end) { curr->next = ptr; curr = ptr; } else { ptr->next = head; head = ptr; } return ptr; } struct myNode* search_in_list(int val, struct myNode **prev) { struct myNode *ptr = head; struct myNode *tmp = NULL; bool found = false; printf("\n Searching the list for value [%d] \n",val); while(ptr != NULL) { if(ptr->val == val) { found = true; break; } else { tmp = ptr; ptr = ptr->next; } } if(true == found) { if(prev) *prev = tmp; return ptr; } else { return NULL; } } int delete_from_list(int val) { struct myNode *prev = NULL; struct myNode *del = NULL; printf("\n Deleting value [%d] from list\n",val); del = search_in_list(val,&prev); if(del == NULL) { return -1; } else { if(prev != NULL) prev->next = del->next; if(del == curr) { curr = prev; } else if(del == head) { head = del->next; } } free(del); del = NULL; return 0; } void print_list(void) { struct myNode *ptr = head; printf("\n -------Printing list Start------- \n"); while(ptr != NULL) { printf("\n [%d] \n",ptr->val); ptr = ptr->next; } printf("\n -------Printing list End------- \n"); return; } int main(void) { int i = 0, ret = 0; struct myNode *ptr = NULL; print_list(); for(i = 5; i<10; i++) add_to_list(i,true); print_list(); for(i = 4; i>0; i--) add_to_list(i,false); print_list(); for(i = 1; i<10; i += 4) { ptr = search_in_list(i, NULL); if(NULL == ptr) { printf("\n Search [val = %d] failed, no such element found\n",i); } else { printf("\n Search passed [val = %d]\n",ptr->val); } print_list(); ret = delete_from_list(i); if(ret != 0) { printf("\n delete [val = %d] failed, no such element found\n",i); } else { printf("\n delete [val = %d] passed \n",i); } print_list(); } return 0; } */