Sunteți pe pagina 1din 8

UNIVERSITATEA TEHNIC A MOLDOVEI

CATEDRA: Tehnologii

Informaionale

Lucrarea de laborator nr: 2 si 3


La Matematica Discret
TEMA: Algoritmul parcurgerii n adncime i lrgime

ELABORAT:
Studentul gr TI-122

Suiu Ion

VERIFICAT:
Profesorul

Sirbu Tamara

Chiinu 2013

SCOPUL LUCRRII:
Studierea algoritmilor de cutare n graf i a diferitor forme de pstrare i prelucrare a datelor.
Elaborarea procedurii de cutare n adncime si largime
SARCINA DE BAZ
1.

Elaborai procedura cutrii n adncime si largime ntr-un graf arbitrar;

2. Elaborai un program cu urmtoarele posibiliti:

introducerea grafului n calculator;

parcurgerea grafului n adncime si largime;

vizualizarea rezultatelor la display sau imprimant.


Coninutul programului:
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <dos.h>
#include <alloc.h>
struct lista
{int inf;
lista *next; }
*ls;
typedef struct list
{ int ai;
struct list *next;}
*stiva;
stiva ps(stiva s,int ai);
stiva pop(stiva s, int *pai);
void pop_end(stiva &s, int *pai);
void Drumul_Init();
void InitTL();
void IntrTL();
void AfisTL();
void Parc_adn();
void Parc_lat();
void AfisDrum();
void ai();
int menu();
//Declarare de variabile globale
int tab[15],n,i,j=0;
struct lista *tl[15];
stiva s=NULL,s2=NULL;
void main(void)
{
for (;;)
switch(menu())
{
case 1 : clrscr();InitTL();IntrTL();break;
case 2 : clrscr();AfisTL();getch();break;
case 3 : clrscr();Drumul_Init();s=NULL;j=0;
Parc_lat();AfisDrum();getch();break;
case 4 : clrscr();Drumul_Init();s=NULL;j=0;
Parc_adn();AfisDrum();getch();break;
case 5 : ;
case 27 : ai(); exit(1);break;
}
}

lista *inss(lista *cap, int k)


{
//Insereaza la sfirsitul listei
lista *t=(lista *)malloc(sizeof(lista)),*temp;
t->inf=k;
t->next=NULL;
if (!cap) return t;
temp=cap; //Atribuirea automata a val curente variabilei temp
while (temp->next)
temp=temp->next;
temp->next=t;
return cap;
}
void afisl(lista *cap)
{
if (cap)
{
printf("%5d",cap->inf);
afisl(cap->next);
}
else return;
}
stiva ps(stiva s,int ai)
{
stiva t;
t=(stiva)malloc(sizeof(list));
t->ai=ai;
t->next=s;
return t;
}
stiva pop(stiva s, int *pai)
{
stiva t;
if (!s) return NULL;
else
{
*pai=s->ai;
t=s->next;
free(s);
return(t);
}
}
int este(int v, int n, int t[])
{
//valoare
int val=0,i;
for (i=0;i<=n;i++)
if (v==t[i])
{
val=1;
break;
}
return val;
}

int getst(stiva s)
{
if (!s) return -1;
else return(s->ai);
}
int mdfcontine(lista *cap,int n,int t[])
{
while (cap)
if (este(cap->inf,n,t))
cap=cap->next;
else return(cap->inf);
return (-1);
}
void Drumul_Init()
{
int i;
for (i=0;i<15;i++)
tab[i]=-999;
}
void InitTL()
{
int i;
for (i=0;i<15;i++)
tl[i]=NULL;
}
void IntrTL()
{
int v;
printf("n=");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
printf("%d->",i);
do
{
scanf("%d",&v);
tl[i]=inss(tl[i],v);
} while(v!=0);
}
}
void AfisTL()
{
for (i=1;i<=n;i++)
{
printf("%d-> ",i);
afisl(tl[i]);
puts("");
}
}
void Parc_adn()
{

int v1,vc,k;
m1:printf("Introdu primul virf ");
scanf("%d",&v1);
if(v1<1 || v1>n)
{
puts("Virf inexistent!");
getch();
goto m1;
}
else
{
tab[j++]=v1;
s=ps(s,v1);
}
int b=1;
while (b)
{
while (s)
{
vc=getst(s);
if (tl[vc] && tl[vc]->inf!=0)
{
int c=mdfcontine(tl[vc],j-1,tab);
if (c!=-1 && c)
{
tab[j++]=c;
s=ps(s,c);
}
else s=pop(s,&k);
}
else s=pop(s,&k);
}
b=0;s=NULL;
for (i=1;i<=n;i++)
if (!este(i,j-1,tab))
{
b=1;tab[j++]=i;
s=ps(s,i);
break;
}
}
}
void AfisDrum()
{
for (i=0;i<j;i++)
printf("%d ",tab[i]);
}
int menu()
{
clrscr();
int func=1;
while (1)
{
gotoxy(23,12);

cputs("-------------------------------------------------------------------------------");
gotoxy(23,13);
cputs("");
cputs("

MENIU

");

cputs("");
gotoxy(23,14);
cputs("-------------------------------------------------------------------------------");
gotoxy(23,15);
cputs("
");
gotoxy(23,16);
cputs("");
textbackground(4*(func==1));
textcolor(3+12*(func==1));
cputs(" Introducerea datelor arborelui ");
textbackground(BLACK);
textcolor(GREEN);
cputs("");
gotoxy(23,17);
cputs("----------------------------------------------------------------------------");
gotoxy(23,18);
cputs("");
textbackground(4*(func==2));
textcolor(3+12*(func==2));
cprintf(" Afisarea listei de incidenta
");
textbackground(BLACK);
textcolor(GREEN);
cputs("");
gotoxy(23,19);
cputs("----------------------------------------------------------------------------");
gotoxy(23,20);
cputs("");
textbackground(4*(func==3));
textcolor(3+12*(func==3));
cprintf(" Parcurgerea arborelui in largime ");
textbackground(BLACK);
textcolor(GREEN);
cputs("");
gotoxy(23,21);
cputs("--------------------------------------------------------------------------");
gotoxy(23,22);
cputs("");
textbackground(4*(func==4));
textcolor(3+12*(func==4));
cprintf(" Parcurgerea arborelui in adincime ");
textbackground(BLACK);
textcolor(GREEN);
cputs("");
gotoxy(23,23);
cputs("-----------------------------------------------------------------------");
gotoxy(23,24);
cputs("");
textbackground(4*(func==5));
textcolor(1+12*(func==5));

cprintf("
IESIRE!!!
");
textbackground(BLACK);
textcolor(GREEN);
cputs("");
gotoxy(23,25);
cputs("-----------------------------------------------------------------------");
switch(getch())
{
case 80:if(func<5)func++;else if (func==5) func=1; break;
case 72:if(func>1)func--;else if (func==1) func=5; break;
case 13:return func;
case 27:
clrscr();
ai();
exit(1);
}
}
}
void Parc_lat()
{
int v1,vc,b;
struct lista *ctl[15];
for (i=1;i<=n;i++)
ctl[i]=tl[i];
m1:clrscr();
printf("Introdu primul virf ");
scanf("%d",&v1);
if(v1<1 || v1>n)
{
puts("Virf inexistent!");
getch();
goto m1;
}
else s=ps(s,v1);
b=1;
while (b)
{
while (s || s2)
{
if (s)
{
pop_end(s,&vc);
if (!este(vc,j-1,tab) && vc!=0)
tab[j++]=vc;
if (ctl[vc]->inf!=0)
while (ctl[vc]->next)
{
if (!este(ctl[vc]->inf,j-1,tab))
s2=ps(s2,ctl[vc]->inf);
ctl[vc]=ctl[vc]->next;
}
}
else
{
stiva temp=s;
s=s2;
s2=temp;

}
}
b=0;s=NULL;
s2=NULL;
for (i=1;i<=n;i++)
if (!este(i,j-1,tab))
{
b=1;
s=ps(s,i);
break;
}
}
}
void pop_end(stiva &s, int *pai)
{
if (!s) return ;
if (s->next) pop_end(s->next,pai);
else
{
*pai=s->ai;
s=NULL;
}
}
void ai()
{
char s[]="
\0";
char c,p[60];
int i,j;
clrscr();
i=0;
while(s[i]!='\0')
{
j=0;
p[0]='\0';
while(j<=i)
{
p[j]=s[j];
j++;
}
p[j]='\0';
textcolor(14);
gotoxy(70-i,12);
cprintf("%s",p);
if(s[j]!='\0')
{
textcolor(0);
gotoxy(70-i,12);
cprintf("%s",p);
textcolor(3);
gotoxy(8,25);
}
i++;
}
}

S-ar putea să vă placă și