Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Inserisce un elemento x prima dell'ultima occorrenza di y*/
- #include <stdio.h>
- #include <stdlib.h>
- struct el{
- int info;
- struct el* next;
- };
- typedef struct el ElementoDiLista;
- typedef struct el * ListaDiElementi;
- void stampa(ListaDiElementi l){
- while(l->next!=NULL)
- {
- printf("%d\n", l->info);
- l=l->next;
- }
- }
- void inserisci (ListaDiElementi *l, int x,int y){
- ListaDiElementi corr=*l, prec=NULL, new=malloc(sizeof(ElementoDiLista)), save=NULL;
- new->info=x;
- new->next=NULL;
- int trovato=0;
- if (*l!=NULL){
- while(corr->next!=NULL){
- if (corr->info==y){
- save=prec;
- trovato=1;
- }
- prec=corr;
- corr=corr->next;
- }
- if (trovato==1){
- if (save==NULL){
- new->next=*l;
- *l=new;
- }
- else {
- new->next=save->next;
- save->next=new;
- }
- }
- }
- else {
- prec->next=new;
- }
- }
- int main(){
- ListaDiElementi l=malloc(sizeof(ElementoDiLista));
- ListaDiElementi corr=l;
- int x=4,y=7,n=0;
- int i=0;
- while(i<10)
- {
- scanf("%d",&n);
- corr->info=n;
- corr->next=malloc(sizeof(ElementoDiLista));
- corr=corr->next;
- i++;
- }
- corr->next=NULL;
- inserisci (&l,x,y);
- stampa(l);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement