0% found this document useful (0 votes)
101 views5 pages

Ulancane Liste - Podsetnik

The document describes various operations that can be performed on single and doubly linked lists in C including traversing the lists, adding/removing elements, sorting elements, and more. Single linked lists are defined using a struct containing an integer value and a pointer to the next element. Doubly linked lists additionally contain a pointer to the previous element. Functions are provided to count elements, print elements, add elements to the beginning/end of the list, insert elements in order, sort the list, remove all elements, and remove a specific element.

Uploaded by

Prle Tihi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views5 pages

Ulancane Liste - Podsetnik

The document describes various operations that can be performed on single and doubly linked lists in C including traversing the lists, adding/removing elements, sorting elements, and more. Single linked lists are defined using a struct containing an integer value and a pointer to the next element. Doubly linked lists additionally contain a pointer to the previous element. Functions are provided to count elements, print elements, add elements to the beginning/end of the list, insert elements in order, sort the list, remove all elements, and remove a specific element.

Uploaded by

Prle Tihi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ulananeliste

JEDNOSTRUKOULANANELISTE

typedef struct elem {int broj; struct elem *sled;} Elem;


Obilazakliste(prebrojavanjeelemenata)

int duz (Elem *lst) {


Elem *tek;
int n=0;
for (tek=lst;tek;tek=tek->sled) n++;
return n;
}
Ispisivanjeelemenataliste

void pisi (Elem *lst) {


for (tek=lst;tek;tek=tek->sled)
printf(%d\n,tek->broj);
}
Dodavanjeelementanapoetakliste

Elem* na_pocetak (Elem *lst, int b) {


Elem *novi=malloc(sizeof(Elem));
novi->broj=b;
novi->sled=lst;
lst=novi;
return lst;
}
Dodavanjeelementanakrajliste

Elem* na_kraj (Elem *lst, int b) {


Elem *tek, *novi=malloc(sizeof(Elem));
novi->broj=b;
novi->sled=NULL;
if (!lst)
lst=novi;
else {
for(tek=lst;tek->sled;tek=tek->sled);
tek->sled=novi;
}
return lst;
}

Umetanjeelementauneopadajuulistu

Elem* umetni (Elem *lst, int b) {


Elem *tek=lst, *pret=NULL, *novi;
while (tek && tek->broj<b) {
pret=tek; tek=tek->sled;
}
novi=malloc(sizeof(Elem));
novi->broj=b;
novi->sled=tek;
if (!pret)
lst=novi;
else
pret->sled=novi;
return lst;
}
Sortiranjelisteuneopadajuemporetku(metodomizbora)

void sortiraj (Elem *lst) {


Elem *i, *j;
int p;
for (i=lst;i;i=i->sled)
for (j=lst;j;j=j->sled)
if (j->broj < i->broj) {
p=i->broj;
i->broj=j->broj;
j->broj=p;
}
}
Brisanjesvihelemenataliste

void brisi (Elem *lst) {


Elem *stari;
while (lst) {
stari=lst;
lst=lst->sled;
free(stari);
}
}
Izostavljanjeelementasazadatomvrednou

Elem* izostavi (Elem *lst, int b) {


Elem *tek=lst, *pret=NULL, *stari;
while (tek) {
if (tek->broj!=b) {
pret=tek; tek=tek->sled;
}

else { /* element se izbacuje */


stari=tek; tek=tek->sled;
if (!pret) lst=tek;
else pret->sled=tek;
free(stari);
}
return lst;
}
DVOSTRUKOULANANELISTE

typedef struct elem {


int broj;
struct elem *sled, *pret;
} Elem;
typedef struct {Elem *prvi, *posl} Lista;
Praznalista

Lista lst={NULL,NULL};
Obilazaklisteunapred

for (tek=lst.prvi;tek;tek=tek->sled);
Obilazaklisteunazad

for (tek=lst.posl;tek;tek=tek->pret);
Ispisivanjelisteunapred

void pisi_unapred (Lista lst) {


Elem *tek;
for (tek=lst.prvi;tek;tek=tek->sled) {
printf(%d\n,tek->broj);
}
Ispisivanjelisteunazad

void pisi_unazad (Lista lst) {


Elem *tek;
for (tek=lst.posl;tek;tek=tek->pret) {
printf(%d\n,tek->broj);
}

Nalaenjeprvepojavebroja

Elem* nadji_prvi (Lista lst, int b) {


Elem *tek;
for (tek=lst.prvi;tek && tek->broj!=b;tek=tek->sled);
return tek;
}
Nalaenjeposlednjepojavebroja

Elem* nadji_posl (Lista lst, int b) {


Elem *tek;
for (tek=lst.posl;tek && tek->broj!=b;tek=tek->pret);
return tek;
}
Dodavanjenovogelementanapoetakliste

Lista na_pocetak (Lista lst, int b) {


Elem *novi=malloc(sizeof(Elem));
novi->broj=b;
novi->sled=lst.prvi;
novi->pret=NULL;
if (!lst.posl) lst.posl=novi;
else lst.prvi->pret=novi;
lst.prvi=novi;
return lst;
}
Dodavanjenovogelementanakrajliste

Lista na_kraj (Lista lst, int b) {


Elem *novi=malloc(sizeof(Elem));
novi->broj=b;
novi->pret=lst.posl;
novi->sled=NULL;
if (!lst.prvi) lst.prvi=novi;
else lst.posl->sled=novi;
lst.posl=novi;
return lst;
}
Izostavljanjeprvepojavezadatogbroja

void izostavi_prvi (Lista *plst, int b) {


Elem *tek=plst->prvi;
while (tek && tek->broj!=b)
tek=tek->sled;
if (tek) {
if (!tek->pret) /* izbacuje se prvi */
plst->prvi=tek->sled;

else tek->pret->sled=tek->sled;
if (!tek->sled) /* izbacuje se poslednji */
plst->posl=tek->pret;
else tek->sled->pret=tek->pret;
free(tek);
}
}
Brisanjesvihelemenataliste

void brisi (Lista *plst) {


Elem *tek=plst->prvi, *stari;
while (tek) {
stari=tek;
tek=tek->sled;
free(stari);
}
plst->prvi=plst->posl=NULL;
}

You might also like