Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct lista {
- int i;
- struct lista *next;
- };
- void print_lista(struct lista *);
- struct lista* NadoveziKPuta(struct lista *,int, int);
- int main(){
- struct lista *p,*q,*t,*temp;
- int x, P, K;
- puts("Unesi P\n");
- scanf("%d",&P);
- puts("Unesi K\n");
- scanf("%d",&K);
- printf("Unijeti elemente liste (lista se zavrsava unosom prvog broja veceg od 50): ");
- p=(struct lista *)malloc(sizeof(struct lista));
- p->next=NULL;
- scanf("%d",&x);
- p->i=x;
- t=p;
- while(x<=50){
- q=(struct lista *)malloc(sizeof(struct lista));
- scanf("%d",&x);
- q->next=NULL;
- q->i=x;
- t->next=q;
- t=q;
- }
- print_lista(p);
- puts("\n");
- temp=NadoveziKPuta(p,P, K);
- print_lista(temp);
- }
- void print_lista(struct lista *p){
- struct lista *q=p;
- while(q!=NULL){
- printf("%d ",q->i);
- q=q->next;
- }
- }
- struct lista* NadoveziKPuta(struct lista * glava,int P, int K){
- struct lista *pom,*pom2,*pom3, *novi, *novaLista, *nov2;
- int i,brojac=0,tempP,tempQ;
- pom=glava;
- pom2=glava;
- pom3 = glava;
- while(pom!=NULL){
- brojac++;
- pom=pom->next;
- }
- if(P<1 || P>brojac || K<0){
- return glava;
- }
- else
- {
- for(i=1;i<=brojac;i++){
- if(i==P){
- tempP=pom2->i;
- }
- pom2=pom2->next;
- }
- printf("tempP je: %d", tempP);
- novi = (struct lista *)malloc(sizeof(struct lista));
- novi->i = tempP;
- novaLista = novi;
- for(i=1;i<K;i++){
- nov2 = (struct lista *)malloc(sizeof(struct lista));
- nov2->i = tempP;
- novaLista->next = nov2;
- novaLista=novaLista->next;
- }
- printf("Novi");
- print_lista(novi);
- printf("Proslo");
- printf("\n");
- while(pom3->next!=NULL) {
- pom3 = pom3->next;
- }
- pom3->next = novi;
- return glava;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement