You are on page 1of 7

1.

UNOS JEDNOSTRUKE LISTE

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci;};

int main() {
// Definisanje promenjivih u main-u i poruka za korisnika za unos
prvog clana
struct clan *pocetak, *kraj;
int x; double y;
printf("Unesite prvi clan liste:\n");
scanf("%d%lf", &x, &y);

//Postavljanje pokazivaca pocetak na odredjena mesta i raspored posle


unosenja prvog elementa //

kraj = pocetak = (struct clan *)malloc(sizeof(struct clan));


pocetak->x = x;
pocetak->y = y;
pocetak->sledeci = NULL;

//Unos sledeceg elementa liste i njegovo smestanje


printf("Unesite sledeci clan liste:\n");
scanf("%d%lf", &x, &y);

//While petlja sa uslovom,rezervisanje memorije za kraj-


>sledeci,postavka pokazivaca kraj na odredjena mesta,novi unos + poruka //

while (x != -1) {

kraj->sledeci = (struct clan *)malloc(sizeof(struct clan));


(kraj->sledeci)->x = x;
(kraj->sledeci)->y = y;
(kraj->sledeci)->sledeci = NULL;
kraj = kraj->sledeci;

printf("Unesite sledeci clan liste:\n");


scanf("%d%lf", &x, &y);}

system("PAUSE");
return 0;
}

2. JEDNOSTRUKO POVEZANA I TAMPANJE

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };

int main()
{
stampanje(pocetak);

system("PAUSE");
return 0;
}

// Stampanje jednostruko povezane liste //

void stampanje(struct clan *pok) // funkcija void stampanje za promenjivu


uzima nesto tipa struct clan *
{
while (pok){
printf("%d\t%lf", pok->x, pok->y); // Stampa clan na koji
naidje,x i y komponentu koju smo uneli
pok = pok->sledeci; // S obzirom da je gore odstampao,moramo
pokazivac pomeriti na sledeci clan liste
}
}

3. JEDNOSTRUKA LISTA PREBROJAVANJE

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };

int main(){
int prebroj=br_clanova(pocetak);
printf("%d", prebroj);

system("PAUSE");
return 0;
}

int br_clanova(struct clan *zed){


int i = 0; //brojac postavljamo na 0
while (zed) {

i++; //uvecavamo brojac svaki put kad naidjemo na clan koji


zadovoljava uslov
zed = zed->sledeci; //pomeramo pokazivac na sledeci clan u listi
}
return i;
}

4. JEDNOSTRUKA PARNI UVEAJ ZA 1

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };


void uvecaj(struct clan *pok);
void stampanje(struct clan *kek);

int main(){
uvecaj(pocetak);
stampanje(pocetak);

system("PAUSE");
return 0;
}
void uvecaj(struct clan *pok) {

while (pok != NULL){


if ((pok->x&&pok->y) % 2 == 0){
(pok->x)++;
(pok->y)++;
}
pok = pok->sledeci;
}
}

void stampanje(struct clan *kek) {


while(kek){
printf("%d\t%.2lf\t", kek->x, kek->y);
kek = kek->sledeci;
}
}

5. JEDNOSTRUKA SEKVENCIJALNO PRETRAIVANJE

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };

//funkcija
struct clan *pronadji (struct clan *pok,int x);

int main(){
//deklaracija struct clana r i pozivanje funkcije pronadji
struct clan r=*pronadji(pocetak, 9);
printf("%d%lf", r);

system("PAUSE");
return 0;
}

struct clan *pronadji (struct clan *pok,int x){


while(pok)
if((pok->x==x)||(pok->y==x))
{return pok;
pok = pok->sledeci;
}
return NULL;
}

6. JEDNOSTRUKA MAKSIMUM

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci;};


struct clan *max(struct clan *pok);

int main(){
struct clan *djoka; //definisanje strukture potrebne za poziv
funkcije
djoka = max(pocetak); // funkcija uzima promenjivu tipa struct tako
da promenjivu moramo smestiti u isti tip kad vracamo iz funkcije
printf("%d\n", djoka->x); //stampanje maksimuma,samo x-ta komponenta

system("PAUSE");
return 0;
}

struct clan *max(struct clan *pok){


struct clan *q; // pomocni pokazivac,pokazuje na trenutni maksimum
q = pok; //pretpostavka da je prvi broj u list max
while (pok)
{if (pok->x > q->x)

q = pok; //max
pok = pok->sledeci;} //pomeranje pokazivaca na sledeci clan
return q;
}

7. JEDNOSTRUKA DODAVANJE ELEMENATA NA KRAJ LISTE

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };


struct clan *dodaj_na_kraj(struct clan *pok, int novi);

int main(){
kraj->sledeci = dodaj_na_kraj(pocetak, x); //poziv funkcije
kraj = kraj->sledeci; //moramo pomeriti kraj

system("PAUSE");
return 0;
}
struct clan *dodaj_na_kraj(struct clan *pok, int novi) {

if (pok == NULL) {

pok= (struct clan *)malloc(sizeof(struct clan)); //lista je


prazna na pocetku
pok->x = novi; //novi clan postaje pocetak,odnosno pok koji na
njega pokazuje
pok->sledeci = NULL; //s obzirom da je to poslednji znamo da
posle njega ne postoji sledeci i zato je NULL
return pok;}

while(pok->sledeci) //kad ispadnemo iz while petlje pok pokazuje na


poslednji element
{pok = pok->sledeci;}

pok->sledeci= (struct clan *)malloc(sizeof(struct clan));


(pok->sledeci)->x = novi;
(pok->sledeci)->sledeci = NULL;

return pok->sledeci;}
8. JEDNOSTRUKA DODAVANJE NA POETAK

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };


struct clan *dodaj_na_pocetak(struct clan *pok, int np);

int main(){

pocetak = dodaj_na_pocetak(pocetak, x);

system("PAUSE");
return 0;
}

struct clan *dodaj_na_pocetak(struct clan *pok, int np) {

struct clan *ispred;


ispred = (struct clan *)malloc(sizeof(struct clan));;
ispred->x = np;
ispred->sledeci = pok;
return ispred;
}

9. JEDNOSTRUKA DODAVANJE NA SREDINU

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };


struct clan *dodaj_na_sred(struct clan *pok, int el);

int main(){

struct clan *q1;


while(q1->x!=6) // predpostavljamo da je 6 broj u sredini
{
q1 = q1->sledeci; //po izlasku iz while petlje q1 pokazuje na
strukturu koja sadrzi 6
}

q1->sledeci = dodaj_na_sred(q1, 17);

system("PAUSE");
return 0;
}

struct clan *dodaj_na_sred(struct clan *pok, int el){

struct clan *pok1;


pok1= (struct clan *)malloc(sizeof(struct clan));
pok1->x = el;
pok1->sledeci = pok->sledeci;
pok->sledeci = pok1;
return pok1;
}
10. JEDNOSTRUKA UKLANJANJE ELEMENATA SA KRAJA

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };

int main(){

pocetak = ukloni_sa_kraja(pocetak);

system("PAUSE");
return 0;
}

struct clan *ukloni_sa_kraja(struct clan *pok) {

struct clan *r; //pomocni pokazivac


if (pok == NULL) return pok; // prazna lista
if (pok->sledeci = NULL) {
free(pok); return NULL; //ako imamo jedan clan u listi
}

r = pok; // r je sada na pocetku


while((r->sledeci)->sledeci){
r = r->sledeci; //kada izadjemo iz while pokazuje na
pretposlednji element liste
}

free(r->sledeci);//poslednji pokazivac u listi


return pok;
}

11. JEDNOSTRUKA UKLANJANJE SA POETKA

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };


struct clan *ukloni(struct clan *pok);

int main(){
pocetak = ukloni(pocetak);
system("PAUSE");
return 0;
}

struct clan *ukloni(struct clan *pok){

struct clan *r1;


r1 = pok; //r1 postaje novi pocetak
pok = pok->sledeci; //novi pocetak je sledeci posle prethodnog
pocetka
free(r1); // oslobadjamo novi pocetak
return pok;
}
12. JEDNOSTRUKA UKLANJANJE SA SREDINE

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct clan { int x; double y; struct clan *sledeci; };


struct clan *ukloni_iz_sredine(struct clan *pok, struct clan *r1);

int main(){
struct clan *q1;
q1 = pocetak;
while(q1->x!=-1){
q1 = q1->sledeci;
}

pocetak = ukloni_iz_sredine(pocetak, q1);

system("PAUSE");
return 0;
}

struct clan *ukloni_iz_sredine(struct clan *pok, struct clan *r1) {

struct clan *r2; //pomocni pokazivac


r2 = pok;
while(r2->sledeci!=r1){
r2 = r2->sledeci;
}

r2->sledeci = r1->sledeci;
free(r1);
return pok;}

You might also like