Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Progetto realizzato da: Fabrizio Siciliano
- Algoritmi per l'informatica
- Matricola 843770
- Codice Persona 10522031*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct albero{
- struct albero *right, *left, *padre;
- char *path;
- }tree;
- typedef struct node{
- struct node *testa, *padre, *left, *right;
- char *file; int figli; int check;
- char nome[255];
- }nodo;
- void create_dir(nodo *);
- void create(nodo *, int);
- int write(char *,char *, nodo *);
- void insert(nodo*, nodo*);
- void read(char *, nodo *);
- void delete(nodo *);
- void delete_r(nodo *);
- void find(nodo*, char*, char*, tree*);
- nodo *move(nodo*, char *);
- nodo* cancella(nodo *, nodo *);
- void cancella_ricorsiva(nodo *);
- void inorder_tree_walk(tree *);
- void tree_insert(tree *, char*);
- nodo* tree_minimum(nodo*);
- nodo* tree_successor(nodo*);
- int main (){
- char *comando, *par1, *par2;
- int conta_caratteri = 0, i=0, j, k, stampati;
- nodo *head;
- head = (nodo*) malloc(sizeof(nodo));
- head->padre = NULL; head->testa = NULL; head->left = NULL; head->right = NULL; head->file = NULL; head->figli = 0; head->check = 0;
- while(1){
- comando = (char*)calloc(30, sizeof(char));
- scanf("%s", comando);
- conta_caratteri += strlen(comando);
- if (strcmp(comando,"create_dir")==0){
- create(head, 0);
- }
- else if (strcmp(comando,"create")==0){
- create(head, 1);
- }
- else if (strcmp(comando,"write")==0){
- par1 = (char*)calloc(255, sizeof(char));
- par2 = (char*)calloc(255, sizeof(char));
- scanf("%s", par1);
- scanf("%[^\n]", par2);
- conta_caratteri += strlen(par1);
- conta_caratteri += strlen(par2);
- stampati = write(par1, par2,head);
- if(stampati!=0)
- printf(" %d", stampati);
- free(par1);
- free(par2);
- }
- else if (strcmp(comando,"find")==0){
- par1 = (char*)calloc(255, sizeof(char));
- scanf("%s", par1);
- conta_caratteri += strlen(par1);
- find(head, par1, NULL, NULL);
- free(par1);
- }
- else if (strcmp(comando,"read")==0){
- par1 = (char*)calloc(255, sizeof(char));
- scanf("%s", par1);
- conta_caratteri += strlen(par1);
- read(par1, head);
- free(par1);
- }
- else if (strcmp(comando,"delete")==0){
- delete(head);
- }
- else if (strcmp(comando,"delete_r")==0){
- delete_r(head);
- }
- else if (strcmp(comando, "exit")==0){
- free(head);
- free(comando);
- break;
- }
- free(comando);
- //printf("\n");
- }
- return 0;
- }
- nodo *move(nodo*x, char *stringa){
- if(x==NULL)
- return x;
- if(strcmp(x->nome, stringa)==0)
- return x;
- if(strcmp(x->nome, stringa)>0)
- return move(x->left, stringa);
- else
- return move(x->right, stringa);
- }
- void create(nodo *head, int venafro){
- nodo *ptr=head,*newnodo;
- int i=0;
- char *percorso[255], *p, path[255*255];
- scanf("%s", path);
- for (p = strtok(path, "/"); p; p = strtok(NULL, "/")){
- percorso[i]=(char*)malloc(strlen(p)*sizeof(char));
- strcpy(percorso[i], p);
- i++;
- }
- percorso[i]=NULL;
- for(i=0; percorso[i]!=NULL && ptr!=NULL; i++){
- if(ptr->check==1){
- printf("no\n");
- return;
- }
- head=ptr;
- ptr=move(ptr->testa, percorso[i]);
- }
- if(percorso[i]==NULL && ptr==NULL && head->figli<1024){
- newnodo=(nodo*)malloc(sizeof(nodo));
- strcpy(newnodo->nome, percorso[i-1]);
- newnodo->check=venafro;
- newnodo->file=NULL;
- newnodo->figli=0;
- head->figli+=1;
- insert(head, newnodo);
- newnodo->testa=NULL;
- printf("ok\n");
- return;
- }
- else{
- printf("no\n");
- return;
- }
- }
- void insert(nodo*T, nodo*z){
- nodo *y = NULL, *x= T->testa;
- while(x!=NULL){
- y=x;
- if(strcmp(z->nome, x->nome)<0)
- x = x->left;
- else
- x = x->right;
- }
- z->padre=y;
- if(y==NULL)
- T->testa = z;
- else{
- if(strcmp(z->nome, y->nome)<0)
- y->left = z;
- else
- y->right = z;
- }
- }
- int write(char *path,char *string, nodo * head){
- nodo *ptr; int i=0, len; char *percorso[255], *p;
- ptr = head;
- for (p = strtok(path, "/"); p; p = strtok(NULL, "/")){
- percorso[i]=(char*)malloc(strlen(p)*sizeof(char));
- strcpy(percorso[i], p);
- i++;
- }
- percorso[i]=NULL;
- string = strtok(string, "\"");
- string = strtok(NULL, "\"");
- len = strlen(string);
- for(i=0; percorso[i]!=NULL; i++){
- if(percorso[i+1]==NULL){
- if(ptr->testa)
- ptr = ptr->testa;
- else{printf("no");return 0;}
- while(ptr!=NULL){
- if(strcmp(ptr->nome, percorso[i])<0){
- ptr = ptr->left;
- }
- else if(strcmp(ptr->nome, percorso[i])>0){
- ptr = ptr->right;
- }
- else if(strcmp(ptr->nome, percorso[i])==0){
- if(ptr->file!=NULL){
- ptr->file = realloc(ptr->file, len);
- strcpy(ptr->file, string);
- printf("ok");
- return len;
- }
- else{
- printf("no");
- return 0;
- }
- }
- }
- printf("no");
- return 0;
- }
- else if(percorso[i+1]!=NULL){
- if(ptr->testa)
- ptr = move(ptr->testa, percorso[i]);
- else{
- printf("no");
- return 0;
- }
- if(ptr==NULL){
- printf("no");
- return 0;
- }
- }
- }
- }
- void read(char *path, nodo *head){
- nodo *ptr; char *percorso[255], *p; int check=0, i = 0;
- ptr = head;
- for (p = strtok(path, "/"); p; p = strtok(NULL, "/")){
- percorso[i]=(char*)malloc(strlen(p)*sizeof(char));
- strcpy(percorso[i], p);
- i++;
- }
- percorso[i]=NULL;
- for(i=0; percorso[i]!=NULL; i++){
- if(percorso[i+1]==NULL){
- ptr = ptr->testa;
- while(ptr!=NULL){
- if(strcmp(ptr->nome, percorso[i])<0)
- ptr = ptr->left;
- else if(strcmp(ptr->nome, percorso[i])>0)
- ptr = ptr->right;
- else if(strcmp(ptr->nome, percorso[i])==0){
- if(ptr->file!=NULL){
- printf("contenuto %s", ptr->file);
- return;
- }
- else{
- printf("no");
- return;
- }
- }
- }
- printf("no");
- return;
- }
- else if(percorso[i+1]!=NULL){
- if(ptr->testa)
- ptr = move(ptr->testa, percorso[i]);
- else{
- printf("no");
- return;
- }
- if(ptr==NULL){
- printf("no");
- return;
- }
- }
- }
- }
- void delete(nodo *head){
- nodo *ptr=head;
- int i=0;
- char *percorso[255], *p, path[255*255];
- scanf("%s", path);
- for (p = strtok(path, "/"); p; p = strtok(NULL, "/")){
- percorso[i]=(char*)malloc(strlen(p)*sizeof(char));
- strcpy(percorso[i], p);
- i++;
- }
- percorso[i]=NULL;
- for(i=0; percorso[i]!=NULL && ptr!=NULL; i++){
- if(ptr->check==1){
- printf("no\n");
- return;
- }
- head=ptr;
- ptr=move(ptr->testa, percorso[i]);
- }
- if(percorso[i]==NULL && ptr!=NULL && ptr->testa==NULL){
- ptr=cancella(head, ptr);
- head->figli-=1;
- if(ptr->file!=NULL)
- free(ptr->file);
- free(ptr);
- printf("ok\n");
- return;
- }
- else{
- printf("no\n");
- return;
- }
- }
- nodo* cancella(nodo *T, nodo *z){
- nodo *x, *y, *ptr = T;
- if(z->left==NULL || z->right == NULL)
- y = z;
- else
- y = tree_successor(z);
- if(y->left != NULL)
- x = y->left;
- else x = y->right;
- if(x!=NULL)
- x->padre = y->padre;
- if(y->padre == NULL)
- T->testa = x;
- else if(y == y->padre->left)
- y->padre->left = x;
- else
- y->padre->right = x;
- if(y!=z){
- strcpy(z->nome, y->nome);
- z->testa=y->testa;
- z->figli=y->figli;
- z->check=y->check;
- z->file=y->file;
- }
- return y;
- }
- nodo *tree_successor(nodo*x){
- nodo *y;
- if(x->right != NULL)
- return tree_minimum(x->right);
- y = x->padre;
- while(y!=NULL && strcmp(x->nome, y->right->nome)==0){
- x = y;
- y = y->padre;
- }
- return y;
- }
- nodo* tree_minimum(nodo*x){
- while(x->left!=NULL)
- x = x->left;
- return x;
- }
- void delete_r(nodo *head){
- nodo *ptr=head;
- int i=0;
- char *percorso[255], *p, path[255*255];
- scanf("%s", path);
- for (p = strtok(path, "/"); p; p = strtok(NULL, "/")){
- percorso[i]=(char*)malloc(strlen(p)*sizeof(char));
- strcpy(percorso[i], p);
- i++;
- }
- percorso[i]=NULL;
- for(i=0; percorso[i]!=NULL && ptr!=NULL; i++){
- if(ptr->check==1){
- printf("no\n");
- return;
- }
- head=ptr;
- ptr=move(ptr->testa, percorso[i]);
- }
- if(percorso[i]==NULL && ptr!=NULL){
- cancella_ricorsiva(ptr->testa);
- ptr->testa=NULL;
- ptr=cancella(head, ptr);
- head->figli-=1;
- if(ptr->file!=NULL)
- free(ptr->file);
- free(ptr);
- printf("ok\n");
- return;
- }
- else{
- printf("no\n");
- return;
- }
- }
- void cancella_ricorsiva(nodo *x){
- if(x==NULL)
- return;
- cancella_ricorsiva(x->right);
- cancella_ricorsiva(x->left);
- cancella_ricorsiva(x->testa);
- if(x->file!=NULL)
- free(x->file);
- free(x);
- return;
- }
- void find(nodo *root, char* risorsa, char*percorso, tree *R){
- nodo *ptr; int i;
- ptr = root->testa;
- if(ptr!=NULL){
- if(strcmp(ptr->nome, risorsa)==0){
- if(percorso==NULL)
- percorso = (char*)malloc(sizeof(char)*strlen(ptr->nome)+1);
- else
- percorso = realloc(percorso, strlen(percorso)+strlen(ptr->nome)+1);
- strcat(percorso,"/");
- strcat(percorso, ptr->nome);
- if(R == NULL)
- R = (tree*)malloc(sizeof(tree));
- tree_insert(R, percorso);
- if(ptr->file == NULL && ptr->testa!=NULL){
- if(percorso==NULL) percorso = (char*)malloc(sizeof(char)*strlen(ptr->nome)+1);
- else percorso = realloc(percorso, strlen(percorso)+strlen(ptr->nome)+1);
- strcat(percorso, "/");
- strcat(percorso, ptr->nome);
- find(ptr->testa, risorsa, percorso, R);
- }
- }
- else if(ptr->file == NULL && ptr->testa!=NULL){
- if(percorso==NULL) percorso = (char*)malloc(sizeof(char)*strlen(ptr->nome)+1);
- else percorso = realloc(percorso, strlen(percorso)+strlen(ptr->nome)+1);
- strcat(percorso, "/");
- strcat(percorso, ptr->nome);
- find(ptr->testa, risorsa, percorso, R);
- }
- if(ptr->left)
- find(ptr->left, risorsa, percorso, R);
- if(ptr->right)
- find(ptr->right, risorsa, percorso, R);
- }
- else return;
- if(R==NULL){
- printf("1no");
- return;
- }
- else{
- printf("ok");
- inorder_tree_walk(R);
- return;
- }
- }
- void tree_insert(tree *T, char *string){
- tree *z, *y = NULL, *x = T;
- z = (tree*)malloc(sizeof(tree));
- z->left = NULL; z->right = NULL; z->path = (char*)malloc(sizeof(char)*strlen(string)); strcpy(z->path, string); z->padre = NULL;
- while(x!=NULL){
- y=x;
- if(strcmp(z->path, x->path)<0)
- x = x->left;
- else x = x->right;
- }
- if(y==NULL)
- T = z;
- else if(strcmp(z->path, y->path)<0){
- z->padre = y;
- y->left = z;
- }
- else{
- z->padre = y;
- y->right = z;
- }
- return;
- }
- void inorder_tree_walk(tree *x){
- inorder_tree_walk(x->left);
- printf("\n%s",x->path);
- inorder_tree_walk(x->right);
- free(x->path);
- free(x);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement