#include <stdio.h> #include <stdlib.h> typedef struct _cvor { int vrednost; struct _cvor *sledeci; }cvor; cvor* napraviCvor(int x, cvor* naredni) { cvor *a=(cvor*)malloc(sizeof(cvor)); a->vrednost=x; a->sledeci=naredni; return a; } void dodajCvor(int x, cvor** lista) { if((*lista)==NULL) { (*lista)=napraviCvor(x,NULL); return; } cvor* b=napraviCvor(x,NULL); cvor* pocetak; pocetak=(*lista); while(pocetak!=NULL && pocetak->sledeci!=NULL) pocetak=pocetak->sledeci; pocetak->sledeci=b; return; } int brojEl(cvor *lista) { int brojac=0; if(lista==NULL) return brojac; while(lista->sledeci!=NULL) { brojac++; lista=lista->sledeci; } return (brojac+1); } void ispis(cvor *lista) { if(lista==NULL) return; printf("%d ",lista->vrednost); ispis(lista->sledeci); } cvor* pronadjiPocetak(cvor *lista) { if(lista==NULL) return NULL; return lista; } cvor* pronadjiKraj(cvor *lista) { if(lista==NULL) return NULL; while(lista->sledeci!=NULL) lista=lista->sledeci; return lista; } void izmeniListu(cvor* lista1,cvor* lista2) { cvor *pocetak; cvor *kraj; pocetak=pronadjiPocetak(lista1); kraj=pronadjiKraj(lista1); printf("\n Prvi element: %d",pocetak->vrednost); printf("\n Poslednji element: %d",kraj->vrednost); while(brojEl(lista1)){ if(brojEl(lista1)==1) { dodajCvor(pocetak->vrednost,&lista2); return; } printf("\n%d",lista2->vrednost); dodajCvor(pocetak->vrednost,&lista2); dodajCvor(kraj->vrednost,&lista2); cvor* tekuci; tekuci=pocetak; while(tekuci->sledeci->sledeci!=NULL) tekuci=tekuci->sledeci; kraj=tekuci; free(tekuci->sledeci); cvor *temp; temp=pocetak; pocetak->sledeci=pocetak->sledeci->sledeci; pocetak=pocetak->sledeci; free(temp); } } int main() { int broj; cvor *lista=NULL; cvor* lista2=NULL; int n; int brojac=0; int k; printf("\n Unesite broj elemenata liste:"); scanf("%d",&n); printf("\n Unos clanova liste:"); while(brojac<n) { scanf("%d",&broj); dodajCvor(broj,&lista); brojac++; } printf("\n Izgled liste: "); ispis(lista); printf("\n Broj elemenata liste: %d",brojEl(lista)); izmeniListu(lista,lista2); printf("\n Izgled izmenjene liste je:"); ispis(lista2); return 0; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)