Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct nodo{
- char c;
- struct list *next;
- struct nodo *left, *right;
- };
- struct list{
- char nome[50];
- char numero[15];
- struct list *next;
- };
- void iniRubrica(struct nodo **p){
- *p = NULL;
- }
- void stampaARB(struct nodo *p){
- if(p == NULL){
- printf("()");
- return;
- }else{
- printf("(%c",p->c);
- stampaARB(p->left);
- stampaARB(p->right);
- printf(")");
- }
- }
- struct nodo* InserisciLettera(struct nodo *p, char k){
- struct nodo *nuovo;
- if(p == NULL){
- nuovo = malloc(sizeof(struct nodo));
- nuovo -> c = k;
- nuovo -> left = NULL;
- nuovo -> right = NULL;
- nuovo ->next = NULL;
- return nuovo;
- }else{
- if(p -> c < k){
- p->left = InserisciLettera(p->left,k);
- return p;
- }
- else
- {
- p->right = InserisciLettera(p->right,k);
- return p;
- }
- }
- }
- struct nodo* ricercaBinaria(struct nodo *p,char k){
- int trovato;
- if(p -> c == k){
- return p;
- }
- else{
- if(p -> c < k)
- return ricercaBinaria(p->left,k);
- else
- return ricercaBinaria(p->right,k);
- }
- };
- struct list* Inserisci_Elemento(struct list* lista,char nome[],char numero[])
- {
- struct list* prec,*curr,*new_node;
- prec=NULL;
- curr=lista;
- while(curr!=NULL && (strcmp(nome,curr->nome))>0) ///trova la posizione di inserimento in modo da insere l'elemento in ordine
- {
- prec=curr;
- curr=curr->next;
- }
- new_node=(struct nodo*)malloc(sizeof(struct nodo)); ///allocazione dinamica
- if(new_node==NULL){printf("Errore di allocazione");exit(1);}
- strcpy(new_node->nome,nome);
- strcpy(new_node->numero,numero);
- if(prec==NULL)///inserimento in testa
- {
- printf("\n\nins testa\n");
- new_node->next=lista;
- lista=new_node;
- return lista;
- }
- else ///inserimento in mezzo o fondo
- {
- printf("\n\nins mezzo\n");
- prec->next=new_node;
- new_node->next=curr;
- return lista;
- }
- }
- struct nodo* inserisciNomeNumero(struct nodo *p, char nome[50], char numero[15]){
- struct nodo *pos;
- struct list *j;
- pos = ricercaBinaria(p,nome[0]);
- //printf("\n\nLettera lista: %c\n",pos->c);
- pos->next=Inserisci_Elemento(pos->next,nome,numero);
- printf("\n\nLISTA TOTALE %c\n",pos->c);
- while(pos->next!=NULL)
- {
- printf("\n\nLista lettera %c\nNome: ",nome[0]); puts(pos->next->nome); printf("Numero: "); puts(pos->next->numero);
- pos->next=pos->next->next;
- }
- return pos;
- }
- int main()
- {
- int i;
- char let[26] = "LFTBJPWADHKNQUYCEGIMORSVXZ";
- struct nodo *r;
- iniRubrica(&r);
- for(i=0;i<26;i++){
- r = InserisciLettera(r,let[i]);
- }
- stampaARB(r);
- r=inserisciNomeNumero(r,"Sergio","3338154382");
- r=inserisciNomeNumero(r,"Salvatore","3338154381");
- r=inserisciNomeNumero(r,"Sandro","3338154382");
- printf("\n\nLISTA TOTALE %c\n",r->c);
- while(r->next!=NULL)
- {
- printf("\n\nLista lettera %c\nNome: ",r->c); puts(r->next->nome); printf("Numero: "); puts(r->next->numero);
- r->next=r->next->next;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement