Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Sposta il primo elemento maggiore di X in fondo alla lista*/
- #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!=NULL)
- {
- printf("%d\n", l->info);
- l=l->next;
- }
- }
- void sposta(ListaDiElementi *l, int x){
- ListaDiElementi corr=*l, new=NULL, prec=NULL, save=NULL;
- int trovato=0;
- while(corr->next!=NULL){
- if (trovato==0){
- if (corr->info>x ){
- trovato=1;
- new=malloc(sizeof(ElementoDiLista));
- new->info=corr->info;
- new->next=NULL;
- save=prec;
- }
- }
- prec=corr;
- corr=corr->next;
- }
- if (trovato==1){
- if (save==NULL){
- *l=(*l)->next;
- prec->next=new;
- }
- else {
- prec->next=new;
- save->next=save->next->next;
- }
- }
- }
- int main(){
- ListaDiElementi l=malloc(sizeof(ElementoDiLista));
- ListaDiElementi corr=l;
- int x=4,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;
- sposta(&l, x);
- stampa(l);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement