Sunteți pe pagina 1din 6

Liste multiplu

typedef struct statiune

{
char *locatia, *tara;
int distanta;
struct statiune *urm1;
struct statiune *urm2;
}nod;

nod *prim1 = NULL, *prim2 = NULL;


nod *auxp;
nod*adauga1(nod*p, nod*q)
{
nod *q1, *q2;
for (q1 = q2 = p; q1 != NULL && strcmp(q1->locatia, q-
>locatia) < 0; q2 = q1, q1 = q1->urm1);
q->urm1 = q1;
if (q1 == q2)
return q;
else
{
q2->urm1 = q;
return p;
}
}

nod*adauga2(nod*p, nod*q)
{
nod *q1, *q2;
for (q1 = q2 = p; q1 != NULL && q1->distanta > q-
>distanta; q2 = q1, q1 = q1->urm2);
q->urm2 = q1;
if (q1 == q2)
return q;
else
{
q2->urm2 = q;
return p;
}
}

void adaugare(char *locatie, char *tara, int distanta)


{
nod *p;
p = (nod*)malloc(sizeof(nod));
p->locatia = (char*)malloc(strlen(locatie) + 1);
p->tara = (char*)malloc(strlen(tara) + 1);
strcpy(p->locatia, locatie);
strcpy(p->tara, tara);
p->distanta = distanta;
prim1 = adauga1(prim1, p);
prim2 = adauga2(prim2, p);
}

void afisare(nod *q){ printf("\nLocatia:%s\nTara:


%s\nDistanta:%d\n", q->locatia, q->tara, q->distanta); }

void afis1(void)
{
nod *p;
for (p = prim1; p != NULL; p = p->urm1)
afisare(p);
}

void afis2(void)
{
nod *p;
for (p = prim2; p != NULL; p = p->urm2)
afisare(p);
}

void afisfctara(char *tara)


{
nod *q;
int sw = 0;
for (q = prim1; q != NULL;q=q->urm1)
if (strcmp(q->tara, tara) == 0)
{
afisare(q);
sw = 1;
}
if (!sw)
printf("Nu a fost gasita nicio locatie aceasta tara!");
}
nod *stergere1(nod *p, char *locatia)
{
nod *q1, *q2, *aux;

for (q1 = q2 = p; q1 != NULL && strcmp(q1->locatia,


locatia) < 0; q2 = q1, q1 = q1->urm1);

if (q1 != NULL && strcmp(q1->locatia, locatia) == 0){


auxp = q1;
if (q1 == q2)
return p->urm1;
else
q2->urm1 = q1->urm1;
return p;
}
else{
printf("Locatia nu a fost introdusa!");
return p;
}
}

nod*stergere2(nod*p, char *l)


{
nod *q1, *q2;
for (q1 = q2 = p; q1 != NULL && strcmp(q1->locatia, l) < 0;
q2 = q1, q1 = q1->urm1);

if (q1 != NULL && strcmp(q1->locatia, l) == 0)


{
auxp = q1;

if (q1 == q2)
return p->urm2;
else{
q2->urm2 = q1->urm2;
return p;
}
}
else
{
printf("Eroare nu apare in ev!");
return p;
}
}

nod *cautare(char *locatia)


{
nod *p;
for (p = prim1; p != NULL && strcmp(p->locatia, locatia) <
0; p = p->urm1);
if (p != NULL && strcmp(p->locatia, locatia) == 0)
return p;
return NULL;
}

void elim(char *locatia)


{
if (cautare(locatia))
{
prim1 = stergere1(prim1, locatia);
prim2 = stergere2(prim2, locatia);
free(auxp->locatia);
free(auxp->tara);
free(auxp);
}
else
printf("locatia nu apare in ev!\n");
}
void salvare_fis()
{
FILE *f;
nod *p;
f = fopen("vacanta.txt", "wt");
for (p = prim1; p != NULL; p = p->urm1){
fprintf(f, "\n %30s |%30s |%10d |", p->locatia, p->tara,
p->distanta);
}
fclose(f);
}

void citire_fisier()
{
FILE *f;
nod *p;
char text[30]="";
char locatia[30] = "", tara[30] = "";
int distanta;
int c = 0,v;

f =fopen("vacanta.txt", "rt");
if (f == NULL)
printf("Fisierul nu a putut fi deschis!");
else{
while (fscanf(f,"%s",text)!=EOF)
if (strcmp(text, "|") == 0)
c++;
else
{
if (c % 3 == 0)
{
strcat(locatia, text);
strcat(locatia, " ");
}
if (c % 3 == 1)
{
strcat(tara, text);
strcat(tara, " ");
}
if (c % 3 == 2)
{
locatia[strlen(locatia) - 1] = '\0';
tara[strlen(tara) - 1] = '\0';
adaugare(locatia, tara, atoi(text));
strcpy(locatia, "");
strcpy(tara, "");
}
}
printf("\nDatele au fost Preluate\n");
fclose(f);
}
}