Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2008
    Messaggi
    760

    Lista in c (invariante e complessità)

    Su questa funzione,che copia gli elementi compresi tra 2 e 8 l' invariante è appunto che stampa tutti i valori tra 2 e 8?
    La complessità in tempo è O (n) perchè si scorre una sola volta la lista?
    Com' è invece in spazio?
    Grazie in anticipo


    codice:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct nod {
            int data;
            struct nod *next;
            struct nod *prev;
    } node;
    
    node *newnode(void)
    {
       return (node *)malloc(sizeof(node));
    }
    
    
    node *buildlis()
    {
    int x;int z=0;
    node *lis, *p, *last;
    printf("nuovo numero da inserire in lista:\n");
    scanf("%d", &x);
    if (x==00000)
    lis= NULL; /* caso di lista vuota */
    else
    {
    /* inserzione del primo elemento in una lista */
    last=newnode();
    lis = last;
    last->data = x;
    last->next = NULL;
    printf("nuovo numero da inserire in lista (00000 per terminare):\n");
    scanf("%d", &x);
    z=z+1;
    while (x!=00000)
    
    {
    p=newnode();
    p->data = x;
    p->next = NULL;
    last->next = p;
    last = p;
    printf("nuovo numero da inserire in lista (00000 per terminare):\n");
    scanf("%d", &x);
    z=z+1;
    
    }
    }
    if (z>0) printf("Il valore e' %d\n",z);
    if(z==0) printf("|||||-----lista vuota-----|||||\n");
    return(lis);
    }
    
    
    
    void printlis(node *lis)
    {
         node* tmp;
    
         tmp = lis;
         int somma=0;float media=0;int conteggio=0;
    
         while (tmp != NULL) {
               printf(">>>> %d\n", tmp->data)  ;
               tmp = tmp->next;
    
         }
    
    }
    
    void myFree ( node* top )
    {
        node* tmp;
        tmp = top;
    
        while ( top->next != NULL ) {
              tmp = top->next;
              free(top);
              top = tmp;
        }
    }
    
    int conteggio(node *lis)
    {
    int acc=0;
    while (lis != NULL)
    {
    acc=acc+1;
    lis=lis->next;
    }
    return(acc);
    }
    
    
    node* moltiplica(node *l1)
    {
    node *p,*head,*tail;
    head=NULL;int n=0;int pos=1;
    if(l1==NULL)  return NULL;
    while (l1!=NULL)  {
    
     if((pos>2) && (pos<8)) {
        p=newnode();
        p->data=l1->data;
        p->next=NULL;
        if(head==NULL){head=p,tail=p;}
        else{tail->next=p;tail=tail->next;}}
        pos++;
        l1=l1->next;
        }
        return head;}
    
    
    
    
    
    int main()
    {
        node* head;node* head2;node *dup;
    
        printf ("-----LISTA A-----\n");
        head = buildlis (  );
        
    
    
        printf ("\n-----STAMPA LISTA A-----\n");
        printlis(head);
       
    
        printf ("\n-----CONTROLLO-----\n");
    
        dup=moltiplica(head);
        printlis(dup);
        if(head==NULL) printf ("\nNESSUN RISULTATO ESPRIME I VALORI VOLUTI\n");
        free ( head );
    
    
        return 0;
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2008
    Messaggi
    760
    ragazzi,nessuno mi vuol dare consigli.....

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.