Advertisement
Guest User

eliminazioneParole

a guest
Jun 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Stringa {
  6.     int val;
  7.     char *stringa;
  8.     struct Stringa *next;
  9. };
  10.  
  11. struct Stringa *head = NULL;
  12.  
  13. void InsertC (char *buff);
  14. void EliminaToken (char *token);
  15. void Stampa ();
  16.  
  17. int main () {
  18.    
  19.     char stringa[300];
  20.     char del[50];
  21.     struct Stringa *var;
  22.    
  23.     printf("Inserire testo libero\n > ");
  24.    
  25.     fgets(stringa, 300, stdin);
  26.     char * word = strtok(stringa, "\r\n\t ");
  27.    
  28.     while (word) {
  29.         InsertC(word);
  30.         word = strtok(NULL, "\r\n\t ");
  31.     }
  32.      
  33.  
  34.     printf("Inserire stringa da eliminare > ");
  35.     fgets(del, 50, stdin);
  36.    
  37.     // elimino il newline finale: altrimenti il confronto tra questa parola
  38.     // e quelle nei nodi fallirebbe sicuramente
  39.     *(strchr(del, '\n')) = 0;
  40.    
  41.    
  42.    
  43.     EliminaToken(del);
  44.    
  45.    
  46.     Stampa();
  47.    
  48.      return 0;
  49. }
  50.  
  51. void InsertC (char *buff) {
  52.     if(!(*buff)) return;
  53.    
  54.     struct Stringa *pnodo, *ptr;
  55.    
  56.    
  57.     pnodo = (struct Stringa *) malloc (sizeof(struct Stringa));
  58.     pnodo->stringa = (char *) malloc (sizeof(char *) * strlen(buff)+1);
  59.     strcpy(pnodo->stringa, buff);
  60.     pnodo->next = NULL;
  61.    
  62.    
  63.     if(!head) {
  64.         head = pnodo;
  65.         // printf("%s\n", head->stringa);
  66.     }  else {
  67.         ptr = head;
  68.        
  69.         while(ptr->next) {
  70.             ptr = ptr->next;
  71.         }
  72.        
  73.         ptr->next = pnodo;
  74.        
  75.         // printf("%s\n", ptr->next->stringa);
  76.     }
  77.    
  78.    
  79.    
  80. }
  81.  
  82.  
  83.  
  84. void EliminaToken (char *token) {
  85.     if(!(*token)) return;
  86.    
  87.        
  88.     struct Stringa *prev, *curr;
  89.    
  90.     while(!strcmp(token, head->stringa)) {
  91.         curr = head;
  92.         head = head->next;
  93.         free(curr);
  94.     }
  95.    
  96.     prev = head;
  97.     curr = head->next;
  98.     while(curr) {
  99.        
  100.         if(!strcmp(token, curr->stringa)) {
  101.             curr = curr->next;
  102.             free(prev->next);
  103.             prev->next = curr;
  104.             continue;
  105.         }
  106.        
  107.         prev = curr;
  108.         curr = curr->next;
  109.     }
  110.    
  111.     return;
  112.    
  113. }
  114.  
  115.  
  116. void Stampa () {
  117.     if(!head) return;
  118.    
  119.     struct Stringa *curr;
  120.    
  121.     curr = head;
  122.    
  123.     while (curr) {
  124.         printf("%s ", curr->stringa);
  125.         curr = curr->next;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement