Hirdetés

2024. május 21., kedd

Gyorskeresés

Hozzászólások

(#1622) Dirty_Pio


Dirty_Pio
csendes tag

Sziasztok, van egy kis problemam a listakkal c -ben. Nem a hagyomanyos pointeres modon szeretnem megoldani a feladatot, azaz letrehozni egy listat es kivenni, beletenni elemeket es kiiratni, hanem a kurzoros megoldassal, amikor egy listat vektor segitsegevel szimulalsz. Itt jon a kod:

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

#define maxlen 100


typedef char tipcursor;

typedef int tipnod;

typedef struct tipelem{
tipnod element;
tipcursor next;
}tipelement;

typedef tipcursor tiplista;


void init(tipelement z[maxlen])
{
int i;
for (i=0;i<maxlen-1;i++)
z[i].next=i+1;
z[maxlen].next=-1;
}

tiplista add(tiplista *d,tiplista p,tipelement z[maxlen],tipnod e)
{
if (p==-1) {
p=*d;
z[*d].element=e;
*d=z[*d].next;
z[p].next=-1;
}
else{
tipcursor k=p;
while ((z[k].element<e)&&(k!=-1)&&(z[k].next!=-1))
{
k=z[k].next;
}
tipnod temp;
while (k!=-1)
{
temp=z[k].element;
z[k].element=e;
e=temp;
if(-1==(k=z[k].next)) z[k].next=*d;
}

z[*d].element=e;

temp=*d;
*d=z[*d].next;

z[temp].next=-1;
}
return p;
}

void list(tiplista s,tipelement z[maxlen])
{
while (s!=-1)
{
printf("%i ",z[s].element);
printf("%i ",z[s].next);
s=z[s].next;
}
}

int main(void)
{
tipelement zona[maxlen];

tiplista l=-1,m=-1,*disp;
disp=(tiplista*)malloc(sizeof(tiplista));
*disp=0;

init(zona);

l=add(disp,l,zona,16);
l=add(disp,l,zona,5);
list(l,zona);
l=add(disp,l,zona,41);
list(l,zona);
return 0;
}

Nos a problemam az, hogy a program nem helyezi jol be az elemeket a helyukre, azaz miutan betettem a 16-ost utana az o helyere teszi az 5-t, de nem tolja arrebb a 16-t, pedig az add fuggvenybe minden jol megy, csak kiirataskor mar nem oks a lista.

Elore is koszonom.

[ Szerkesztve ]

Copyright © 2000-2024 PROHARDVER Informatikai Kft.