Advertisement
Guest User

trim

a guest
Jan 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4.  
  5. struct lista {
  6. int i;
  7. struct lista *next;
  8. };
  9. struct lista *trim(struct lista *,int);
  10. void print_lista(struct lista *);
  11.  
  12. void main(){
  13. struct lista *p,*q,*t,*l;
  14. int n;
  15. p=(struct lista *)malloc(sizeof(struct lista));
  16. p->next=NULL;
  17. scanf("%d",&n);
  18. p->i=n;
  19. t=p;
  20. while(n<30){
  21.     q=(struct lista *)malloc(sizeof(struct lista));
  22.     scanf("%d",&n);
  23.     q->next=NULL;
  24.     q->i=n;
  25.     t->next=q;
  26.     t=q;
  27.     }
  28.     print_lista(p);
  29.     l=trim(p,3);
  30.     printf("Nova lista je:");
  31.     print_lista(l);
  32.  
  33.  
  34. }
  35.  
  36. void print_lista(struct lista *p){
  37. struct lista *q=p;
  38. while(q!=NULL){
  39.     printf("%d ",q->i);
  40.     q=q->next;
  41.     }
  42.  
  43. }
  44. struct lista *trim(struct lista *gl,int n){
  45. struct lista *p,*d,*a,*b,*c;
  46. int i,br=0;
  47. p=gl;
  48. while(p!=NULL){
  49.     br++;
  50.     p=p->next;
  51. }
  52. if(n>0 && n<(br/2)){
  53.    p=gl;
  54.    i=1;
  55.    while(p!=NULL){
  56.     if(i==br-n){
  57.      break;
  58.     }
  59.     p=p->next;
  60.     i++;
  61.    }
  62. d=p;
  63. d->next=NULL;
  64. }
  65. while(p!=NULL){
  66.     p=gl;
  67.     i=1;
  68.     if(i==n-1){
  69.         break;
  70.     }
  71.     a=i;
  72.     a->next=b;
  73.     b->next=c;
  74. }
  75. b=gl;
  76. b->next=c;
  77. return gl;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement