Prethodna tema :: Sljedeća tema |
Autor/ica |
Poruka |
stiv Gost
|
Postano: 8:11 sub, 21. 4. 2007 Naslov: Dvostruko vezana lista |
|
|
Jel bi netko možda zna napisati funkcijicu za uzlazno sortiranje dvostruko povezane liste po ovom red elementu.
[code:1]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 20
struct cell {
int red;
float clan;
struct cell *next;
struct cell *prev;
};
struct cell *head, *tail;
void stvori(struct cell *cvor)
{
cvor->clan=100* (float)rand()/(RAND_MAX+1);
if(head == NULL) {
head = cvor;
cvor->prev = NULL;
} else {
tail->next = cvor;
cvor->prev = tail;
}
tail = cvor;
cvor->next = NULL;
}
void obrisi(struct cell *cvor) {
if(cvor->prev == NULL)
head = cvor->next;
else
cvor->prev->next = cvor->next;
if(cvor->next == NULL)
tail = cvor->prev;
else
cvor->next->prev = cvor->prev;
}
void ispis(struct cell *cvor)
{
for(cvor=head;cvor!=NULL;cvor=cvor->next)
{
printf("%d,%f\n", cvor->red,cvor->clan);
}
}
int cla(int q[N],int i)
{
int x,y;
y=N* (float)rand()/(RAND_MAX+1);
for(x=0;x<=i;x++)
{
if(y==q[x])
{
y=N* (float)rand()/(RAND_MAX+1);
exit;
}
}
return y;
}
int main(void) {
struct cell *cvor,*novi;
novi=NULL;
int i;
int p[N+1];
p[0]=0;
/* add some numbers to the double linked list */
for(i = 0; i <N; i++) {
cvor = (struct cell *)malloc(sizeof(struct cell));
cvor->red = cla(p,i);
stvori(cvor);
p[i]=cvor->red;
}
ispis(cvor);
/* destroy the dll list */
while(head != NULL)
obrisi(head);obrisi(head);
} ]
THX.[/code:1][/code]
Jel bi netko možda zna napisati funkcijicu za uzlazno sortiranje dvostruko povezane liste po ovom red elementu.
Kod: |
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 20
struct cell {
int red;
float clan;
struct cell *next;
struct cell *prev;
};
struct cell *head, *tail;
void stvori(struct cell *cvor)
{
cvor->clan=100* (float)rand()/(RAND_MAX+1);
if(head == NULL) {
head = cvor;
cvor->prev = NULL;
} else {
tail->next = cvor;
cvor->prev = tail;
}
tail = cvor;
cvor->next = NULL;
}
void obrisi(struct cell *cvor) {
if(cvor->prev == NULL)
head = cvor->next;
else
cvor->prev->next = cvor->next;
if(cvor->next == NULL)
tail = cvor->prev;
else
cvor->next->prev = cvor->prev;
}
void ispis(struct cell *cvor)
{
for(cvor=head;cvor!=NULL;cvor=cvor->next)
{
printf("%d,%f\n", cvor->red,cvor->clan);
}
}
int cla(int q[N],int i)
{
int x,y;
y=N* (float)rand()/(RAND_MAX+1);
for(x=0;x<=i;x++)
{
if(y==q[x])
{
y=N* (float)rand()/(RAND_MAX+1);
exit;
}
}
return y;
}
int main(void) {
struct cell *cvor,*novi;
novi=NULL;
int i;
int p[N+1];
p[0]=0;
/* add some numbers to the double linked list */
for(i = 0; i <N; i++) {
cvor = (struct cell *)malloc(sizeof(struct cell));
cvor->red = cla(p,i);
stvori(cvor);
p[i]=cvor->red;
}
ispis(cvor);
/* destroy the dll list */
while(head != NULL)
obrisi(head);obrisi(head);
} ]
THX. | [/code]
|
|
[Vrh] |
|
vsego Site Admin


Pridružen/a: 06. 10. 2002. (22:07:09) Postovi: (3561)16
Spol: 
Lokacija: /sbin/init
|
|
[Vrh] |
|
stiv Gost
|
|
[Vrh] |
|
vinko Forumaš(ica)

Pridružen/a: 26. 08. 2006. (23:08:00) Postovi: (1A8)16
Spol: 
Lokacija: PMF-MO 214
|
Postano: 19:56 sub, 21. 4. 2007 Naslov: |
|
|
[quote="stiv"]Pgledao sam vec gro primjera,sam ni jedan nece radit kak spada...[/quote]
Ja mislim da bi ovo trebalo raditi (sort mijenja vrijednosti)
[code:1]void sort(cell *b, cell *e) {
for (cell *c1=b; c1!=e; c1=c1->next) {
cell* m = c1, *c2=c1->next;
while (true) {
if (c2->red < m->red) m=c2;
if (c2==e) break;
c2=c2->next;
}
if (m!=c1) {
int tr = m->red; m->red=c1->red; c1->red = tr;
float tc = m->clan; m->clan=c1->clan; c1->clan = tc;
}
}
}[/code:1]
a treba se pozvati sa sort(head, tail).
Medjutim, vjerojatno se zeli da sort samo mijenja medjuveze. Sori, al za to iskodirat bas nemam volje. U kôdu sa pocetka ove teme vidim gomile nekih nejasnoca (a vjerojatno ima i gresaka).
stiv (napisa): | Pgledao sam vec gro primjera,sam ni jedan nece radit kak spada... |
Ja mislim da bi ovo trebalo raditi (sort mijenja vrijednosti)
Kod: | void sort(cell *b, cell *e) {
for (cell *c1=b; c1!=e; c1=c1->next) {
cell* m = c1, *c2=c1->next;
while (true) {
if (c2->red < m->red) m=c2;
if (c2==e) break;
c2=c2->next;
}
if (m!=c1) {
int tr = m->red; m->red=c1->red; c1->red = tr;
float tc = m->clan; m->clan=c1->clan; c1->clan = tc;
}
}
} |
a treba se pozvati sa sort(head, tail).
Medjutim, vjerojatno se zeli da sort samo mijenja medjuveze. Sori, al za to iskodirat bas nemam volje. U kôdu sa pocetka ove teme vidim gomile nekih nejasnoca (a vjerojatno ima i gresaka).
|
|
[Vrh] |
|
stiv Gost
|
|
[Vrh] |
|
vinko Forumaš(ica)

Pridružen/a: 26. 08. 2006. (23:08:00) Postovi: (1A8)16
Spol: 
Lokacija: PMF-MO 214
|
|
[Vrh] |
|
|