Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- int val;
- int ind;
- struct node *next;
- };
- // stampa lista
- void print_list(struct node *list_head){
- // lista vuota
- if (list_head == NULL){
- printf("lista vuota!\n");
- return;
- }
- struct node *iteratore = list_head;
- int indice = 0;
- int counter = 0;
- int max = iteratore->val;
- int min = iteratore->val;
- while (iteratore != NULL){
- printf("(%d, %d)->", iteratore->val, iteratore->ind);
- if (iteratore->val > max){
- max = iteratore->val;
- }
- if (iteratore->val < min){
- min = iteratore->val;
- }
- counter+= iteratore->val;
- iteratore = iteratore->next;
- indice += 1;
- }
- printf("\b\b \n");
- printf("Max: %d, Min: %d, Media: %d\n\n", max, min, counter/indice);
- return;
- }
- struct node *createnode(int x, int i){
- struct node *node = (struct node*)malloc(sizeof(struct node));
- if (node == NULL){
- printf("malloc() failed!\n");
- exit(1);
- }
- node->val = x;
- node->ind = i;
- return node;
- }
- void insertnode(struct node **list_head, int x, int i){
- // primo elemento da inserire in lista
- if (*list_head == NULL){
- struct node *node = createnode(x,i);
- node->next = NULL;
- *list_head = node;
- return;
- }
- // scorrere la lista fino al penultimo nodo
- struct node *iteratore = *list_head;
- while (iteratore->next != NULL){
- iteratore = iteratore->next;
- }
- struct node *node = createnode(x,i);
- iteratore->next = node;
- node->next = NULL;
- return;
- }
- struct node *arrayList(int V[], int len){
- struct node *list_head = NULL;
- int i = 0;
- for (i = 0; i < len; i++){
- if (V[i] != 0){
- insertnode(&list_head, V[i], i);
- }
- }
- return list_head;
- }
- void updateList(struct node **list_head, int value, int index){
- struct node *iteratore = *list_head;
- while(iteratore->next != NULL){
- // indice presente in lista
- if (iteratore->ind == index){
- iteratore->val = value;
- return;
- }
- // indice non presente in lista
- if (iteratore->ind < index && index < iteratore->next->ind){
- struct node *node = createnode(value, index);
- node->next = iteratore->next;
- iteratore->next = node;
- return;
- }
- iteratore = iteratore->next;
- }
- // indice non presente in lista fino al penultimo nodo
- if (iteratore->ind == index){
- iteratore->val = value;
- return;
- }
- else{
- struct node *node = createnode(value, index);
- iteratore->next = node;
- node->next = NULL;
- return;
- }
- return;
- }
- int main(void){
- int len = 6;
- int V[6] = { 1, 0, -1, 6, 0, 4 };
- struct node *list_head;
- /* Invocare la funzione "arrayList" con l'array V come argomento */
- list_head= arrayList(V, len);
- print_list(list_head);
- /* Invocare la funzione "updateList" con la lista creata da arrayList,
- value=7 e index=2 come parametri */
- updateList(&list_head, 7, 2);
- print_list(list_head);
- /* Invocare la funzione "updateList" con la lista, value=8 e index=1 come
- parametri */
- updateList(&list_head, 8, 1);
- print_list(list_head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement