Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Stringa {
- int val;
- char *stringa;
- struct Stringa *next;
- };
- struct Stringa *head = NULL;
- void InsertC (char *buff);
- void EliminaToken (char *token);
- void Stampa ();
- int main () {
- char stringa[300];
- char del[50];
- struct Stringa *var;
- printf("Inserire testo libero\n > ");
- fgets(stringa, 300, stdin);
- char * word = strtok(stringa, "\r\n\t ");
- while (word) {
- InsertC(word);
- word = strtok(NULL, "\r\n\t ");
- }
- printf("Inserire stringa da eliminare > ");
- fgets(del, 50, stdin);
- // elimino il newline finale: altrimenti il confronto tra questa parola
- // e quelle nei nodi fallirebbe sicuramente
- *(strchr(del, '\n')) = 0;
- EliminaToken(del);
- Stampa();
- return 0;
- }
- void InsertC (char *buff) {
- if(!(*buff)) return;
- struct Stringa *pnodo, *ptr;
- pnodo = (struct Stringa *) malloc (sizeof(struct Stringa));
- pnodo->stringa = (char *) malloc (sizeof(char *) * strlen(buff)+1);
- strcpy(pnodo->stringa, buff);
- pnodo->next = NULL;
- if(!head) {
- head = pnodo;
- // printf("%s\n", head->stringa);
- } else {
- ptr = head;
- while(ptr->next) {
- ptr = ptr->next;
- }
- ptr->next = pnodo;
- // printf("%s\n", ptr->next->stringa);
- }
- }
- void EliminaToken (char *token) {
- if(!(*token)) return;
- struct Stringa *prev, *curr;
- while(!strcmp(token, head->stringa)) {
- curr = head;
- head = head->next;
- free(curr);
- }
- prev = head;
- curr = head->next;
- while(curr) {
- if(!strcmp(token, curr->stringa)) {
- curr = curr->next;
- free(prev->next);
- prev->next = curr;
- continue;
- }
- prev = curr;
- curr = curr->next;
- }
- return;
- }
- void Stampa () {
- if(!head) return;
- struct Stringa *curr;
- curr = head;
- while (curr) {
- printf("%s ", curr->stringa);
- curr = curr->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement