Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct lista {
  5. int i;
  6. struct lista *next;
  7. };
  8.  
  9. void print_lista(struct lista *);
  10. struct lista* NadoveziKPuta(struct lista *,int, int);
  11.  
  12. int main(){
  13.  
  14. struct lista *p,*q,*t,*temp;
  15. int x, P, K;
  16. puts("Unesi P\n");
  17. scanf("%d",&P);
  18. puts("Unesi K\n");
  19. scanf("%d",&K);
  20. printf("Unijeti elemente liste (lista se zavrsava unosom prvog broja veceg od 50): ");
  21. p=(struct lista *)malloc(sizeof(struct lista));
  22. p->next=NULL;
  23. scanf("%d",&x);
  24. p->i=x;
  25. t=p;
  26. while(x<=50){
  27. q=(struct lista *)malloc(sizeof(struct lista));
  28. scanf("%d",&x);
  29. q->next=NULL;
  30. q->i=x;
  31. t->next=q;
  32. t=q;
  33. }
  34.  
  35. print_lista(p);
  36. puts("\n");
  37. temp=NadoveziKPuta(p,P, K);
  38. print_lista(temp);
  39.  
  40. }
  41.  
  42.  
  43. void print_lista(struct lista *p){
  44. struct lista *q=p;
  45. while(q!=NULL){
  46. printf("%d ",q->i);
  47. q=q->next;
  48. }
  49.  
  50. }
  51. struct lista* NadoveziKPuta(struct lista * glava,int P, int K){
  52. struct lista *pom,*pom2,*pom3, *novi, *novaLista, *nov2;
  53. int i,brojac=0,tempP,tempQ;
  54. pom=glava;
  55. pom2=glava;
  56. pom3 = glava;
  57. while(pom!=NULL){
  58. brojac++;
  59. pom=pom->next;
  60. }
  61. if(P<1 || P>brojac || K<0){
  62. return glava;
  63.  
  64. }
  65. else
  66. {
  67. for(i=1;i<=brojac;i++){
  68. if(i==P){
  69. tempP=pom2->i;
  70.  
  71. }
  72. pom2=pom2->next;
  73.  
  74. }
  75.  
  76. printf("tempP je: %d", tempP);
  77. novi = (struct lista *)malloc(sizeof(struct lista));
  78. novi->i = tempP;
  79. novaLista = novi;
  80. for(i=1;i<K;i++){
  81. nov2 = (struct lista *)malloc(sizeof(struct lista));
  82. nov2->i = tempP;
  83. novaLista->next = nov2;
  84. novaLista=novaLista->next;
  85. }
  86. printf("Novi");
  87. print_lista(novi);
  88.  
  89. printf("Proslo");
  90.  
  91. printf("\n");
  92.  
  93.  
  94. while(pom3->next!=NULL) {
  95. pom3 = pom3->next;
  96. }
  97.  
  98. pom3->next = novi;
  99.  
  100. return glava;
  101.  
  102. }
  103.  
  104. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement