Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct nodo{
  6.     char *inf;
  7.     struct nodo *next;
  8.     struct nodo *back;
  9. };
  10. typedef struct nodo nodo;
  11.  
  12. void stampa(nodo* start)
  13. {
  14. int i=1;
  15. printf("\nSTAMPA DELLA LISTA FULL");
  16.     while(start!=NULL)
  17.     {
  18.         printf("\nelemento: %2d, m: %7p, s->back: %8p, s->inf: %s  m->next: %8p",i,start,start->back,start->inf,start->next);
  19.         start=start->next;
  20.         i++;
  21.     }
  22. }
  23. void stampa_small(nodo *start)
  24. {
  25.     int i=1;
  26.     printf("\nSTAMPA DELLA LISTA small");
  27.     while (start!=NULL)
  28.     {
  29.         printf("%s ",start->inf);
  30.         start=start->next;
  31.         i++;
  32.     }
  33. }
  34. int contaNode(nodo *start)
  35. {
  36.     int conta=0;
  37.     while(start!=NULL)
  38.     {
  39.         start=start->next;
  40.         conta++;
  41.     }
  42. return conta;
  43. }
  44.  
  45. nodo* ricercaPosizione(nodo *start, int pos )
  46. {
  47.     int i=1;
  48.     while ( i < pos && start != NULL){
  49.         start = start->next;
  50.         i = i+1;
  51. }
  52. return start;
  53. }
  54.  
  55. nodo* inserimento(nodo *start, char *v, int j)
  56. {
  57.  
  58.     int i=1;
  59.     int s_word;
  60.     s_word=strlen(v);
  61.     printf("stampo v: %s\n", v);
  62.     nodo *app;
  63.     nodo *end;
  64.     app=start;
  65.     end=start;
  66.      if(j==1)//CIOÈ SE IL NODO È UNO
  67.      {
  68.         printf("entro nell'if\n");
  69.         app=start;
  70.         app->back=NULL;
  71.         app->next=NULL;
  72.         app->inf=(char*)malloc(sizeof(char)*s_word);
  73.         app->inf=v;
  74.       return start;
  75.      }
  76.     else{
  77.         app=start;
  78.         printf("entro nell'else\n");
  79.         app=ricercaPosizione(start,contaNode(start));
  80.         end=app;
  81.  
  82.         app->next=(nodo*)malloc(sizeof(nodo));
  83.         app=app->next;
  84.         app->back=end;
  85.  
  86.         app->inf=(char*)malloc(sizeof(char)*strlen(v));
  87.         app->inf=v;
  88.         app->next=NULL;
  89.         return start;
  90.     }
  91. return start;
  92. }
  93.  
  94. int main()
  95. {
  96.     system("clear");
  97.     int sum,i,s_word=0;
  98.     int conta_p;
  99.     char word[26];
  100.     nodo *master1;
  101.  
  102.     master1=(nodo*)malloc(sizeof(nodo));
  103.  
  104. printf("Quante parole vuoi inserire: ");
  105. scanf("%d",&conta_p);
  106.  for (i=1;i<=conta_p;i++) //i indica quante parole
  107.  {
  108.     printf("\ninserisci la %da parola: ",i);
  109.     scanf("%s",word);
  110.     master1=inserimento(master1,word,i);
  111.     stampa(master1);
  112.  }
  113.  
  114.     free(master1);printf("\n\n");
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement