Sunteți pe pagina 1din 19

Ministerul Educaiei i Tineretului al Republicii Moldova

Universitatea Tehnic a Moldovei

Catedra: Automatic i tehnologii informaionale

RAPORT
Lucrare de laborator Nr.4 si 5
la structuri de date i algoritm

A efectuat:

St. gr. xxxx


xxxxxx

A verificat:

lector asistent
xxxxxx

Chiinu 2013
Lucrari de laborator 4 si 5 la SDA

Tema: Implementarea tipului de date abstract Arbore binar in limbajul C. Algoritmi iterativi
si algoritmi recursive.
Scopul lucrarii: Obtinerea deprinderilor practice de implementare in limbajul C tipului de
date abstract (TDA) Arbore binar utilizind algoritmi iterativi si recursivi.
Sarcina de lucru: De scris trei fisiere in limbajul C pentru implementarea (2 fisiere) si
utilizarea (1 fisier program cu functia main ) a TDA Arbore binar:
1. Fisierul antet cu extensia .h care descrie structura nodului al arborelui binar (conform
variantei din Lucrarea de laborator 1) i prototipurile funciilor care asigura operatiunile
de prelucrare a arborelui binar. Pentru Lucrarea de laborator 4 in acest fisier trebuie de
adaugat descrierea structurilor elementelor cozei si a stivei unde vor fi stocate adresele
nodurilor a arborelui binar.
2. Fisier cu extensia .cpp (sau .c) care contine codurile (implementarile)
declarate n fisierul antet.

tuturor functiilor

3. Fisierul utilizatorului - programul cu functia main ( ) pentru prelucrarea arborelui binar cu


afisarea meniului de optiuni pe ecran, si anume: crearea arborelui binar si introducerea
informatiei despre nodurile arborelui de la tastatura, afisarea informatiei despre nodurile
arborelui pe ecran, cautarea nodului dupa un cimp informational, determinarea numarului de
noduri in arbore, determinarea inaltimei a arborelui, eliberarea memoriei dinamice alocate
pentru arbore si iesirea din program.
Pentru Lucrarea de laborator 4 trebuie impementate functiile de prelucrare ale arborelui binar
folosind algoritmi iterativi in 2 versiuni: a) utilizind coada (parcurgerea arborelui pe niveluri
in largime) si b) utilizind stiva (parcurgerea arborelui in adincime - radacina, subarbore sting,
subarbore drept).
Pentru Lucrarea de laborator 5 trebuie impementate functiile de prelucrare ale arborelui binar
folosind algoritmi recursivi corespunzatori.

Textul programului in limbajul C


Struct.h
/*********** START Arbore ************/
typedef struct nod{
char num[40];
char pre[40];
char spec[40];
int anu;
float med;
struct nod *left;
struct nod *right;
}nod;
nod *root = NULL;
/*********** END Arbore ************/
1

/*********** START Coada ************/


typedef struct elq{
nod *adrnod;
struct elq *next;
}elq;
elq *first = NULL;
elq *last = NULL;
/*********** END Coada ************/
/*********** START Stiva ************/
typedef struct els{
nod *adrnod;
struct els *prev;
}els;
els *top = NULL;
/*********** END Stiva ************/
/*********** Prototipuri ************/
void find_error(int ord);
int enq(nod *inf);
nod* deq();
int push(nod *inf);
nod* pop();
int creat_q();
int creat_s();
nod* creat_r();
int show_q();
int show_s();
void show_r(nod *c);
nod* search_q(char *fname);
nod* search_s(char *fname);
nod* search_r(nod* c, char *fname);
int size_q();
int size_s();
int size_r(nod *c);
int read_file(char *fname);
int write_file(char *fname);
void print(nod *c,int k=0);
void freemem_r(nod *c);
int freemem_q();
int freemem_s();
int height_r(nod *c);
/*********** End Prototipuri ************/

funct.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "struct.h"
#define print_scan printf("\nIntroduceti datele :\n");\
printf("Numele : "); scanf("%s",&c->num);\
printf("Prenume : "); scanf("%s",&c->pre);\
printf("Specialitatea : "); scanf("%s",&c->spec);\
printf("Anu : "); scanf("%d",&c->anu);\
printf("Media : "); scanf("%f",&c->med);
#define pause printf("\n\n"); system("pause");
void find_error(int ord){
switch(ord){
case -6:
printf("\a\n>> Eroare la crearea fisierului! <<\n");
break;
case -5:
printf("\a\n>> Eroare la adaugarea nodului in stiva! <<\n");
break;
case -4:
printf("\a\n>> Eroare la deschiderea fisierului! <<\n");
system("pause"); exit(1); break;
case -3:
printf("\a\n>> Eroare la alocarea memoriei pentru fecior! <<\n");
break;
case -2:
printf("\a\n>> Eroare la adaugarea nodului in coada! <<\n");
break;
case -1:
printf("\a\n>> Eroare la alocarea memoriei pentru radacina! <<\n");
break;
case 0:
printf("\a\n>> Eroare ! <<\n"); break;
case 1:
printf("\aOperatia a avut loc cu succes!\n"); break;
default:
printf("<< %d >>\n",ord); break;
}
}
int enq(nod *inf){
elq *c;
c=(elq*)malloc(sizeof(elq));
if(!c) return 0;
if(!first){
first = c;
} else {
last->next=c;
}
last=c;
c->next=NULL;
c->adrnod=inf;
3

return 1;
}
nod* deq(){
elq *c=first;
nod *inf;
if(c == last){
first=last=NULL;
} else {
first=c->next;
}
inf=c->adrnod;
free(c);
return inf;
}
int push(nod *inf){
els *c;
c=(els*)malloc(sizeof(els));
if(!c) return 0;
c->prev=top;
c->adrnod=inf;
top=c;
return 1;
}
nod* pop(){
els *c=top;
nod *inf;
top=c->prev;
inf=c->adrnod;
free(c);
return inf;
}
int creat_q(){
int f;
nod *c,*p;
first=last=NULL;
printf("De creat radacina arborelui (1/0)? : ");
scanf("%d",&f);
if(f){
c=(nod*)malloc(sizeof(nod));
if(!c) return -1;
print_scan
if(!enq(c)) return -2;
root = c;
}else {
root = NULL;
}
while(first){
p = deq();
printf("De creat copilul sting al nodului %s (1/0)? : ",p->num);
scanf("%d",&f);
if(f){
c=(nod*)malloc(sizeof(nod));
if(!c) return -3;
print_scan
4

p->left=c;
if(!enq(c)) return -2;
} else {
p->left=NULL;
}
printf("De creat copilul drept al nodului %s (1/0)? : ",p->num);
scanf("%d",&f);
if(f){
c=(nod*)malloc(sizeof(nod));
if(!c) return -3;
print_scan
p->right = c;
if(!enq(c)) return -2;
} else {
p->right = NULL;
}
}
return 1;
}
int show_q(){
nod *p,*c;
first=last=NULL;
if(!root) return 0;
p=root;
if(!enq(p)) return -2;
printf("Lista Studentilor\n\n");
while(first){
p=deq();
printf("***************************************\n\n");
printf("Numele
%s\n",p->num);
printf("Prenume
%s\n",p->pre);
printf("Specialitatea
%s\n",p->spec);
printf("Anul
%d\n",p->anu);
printf("Media
%.2f\n\n",p->med);
c=p->left;
if(c){
if(!enq(c)) return -2;
}
c=p->right;
if(c){
if(!enq(c)) return -2;
}
}
return 1;
}
nod* search_q(char *fname){
nod *p,*c;
first=last=NULL;
if(!root) return NULL;
p=root;
if(!enq(p)) return NULL;
while(first){
p=deq();
if(!strcmp(p->num,fname)) return p;
5

c=p->left;
if(c){
if(!enq(c)) return NULL;
}
c=p->right;
if(c){
if(!enq(c)) return NULL;
}
}
return NULL;
}
int size_q(){
int s=0;
nod *p,*c;
first=last=NULL;
if(!root) return 0;
p=root;
if(!enq(p)) return -2;
while(first){
p=deq();
s++;
c=p->left;
if(c){
if(!enq(c)) return -2;
}
c=p->right;
if(c){
if(!enq(c)) return -2;
}
}
return s;
}
int freemem_q(){
nod *p,*c;
first=last=NULL;
if(!root) return 0;
p=root;
if(!enq(p)) return -2;
while(first){
p=deq();
c=p->left;
if(c){
if(!enq(c)) return -2;
}
c=p->right;
if(c){
if(!enq(c)) return -2;
}
free(p);
}
return 1;
}
int creat_s(){
nod *p, *c;
6

int f;
root=NULL;
top=NULL;
printf("De creat radacina arborelui (1/0)? : ");
fflush(stdin);
scanf("%d",&f);
if(f){
c=(nod*)malloc(sizeof(nod));
if(!c) return -1;
print_scan
if(!push(c)) return -5;
root=c;
while(top){
p=pop();
printf("De creat copilul drept al nodului %s (1/0)? : ",p->num);
fflush(stdin);
scanf("%d",&f);
if(f){
c=(nod*)malloc(sizeof(nod));
if(!c) return -3;
print_scan
p->right = c;
if(!push(c)) return -5;
} else {
p->right = NULL;
}
printf("De creat copilul sting al nodului %s (1/0)? : ",p->num);
fflush(stdin);
scanf("%d",&f);
if(f){
c=(nod*)malloc(sizeof(nod));
if(!c) return -3;
print_scan
p->left=c;
if(!push(c)) return -5;
} else {
p->left=NULL;
}
}
}
return 1;
}
int show_s(){
nod *p,*c;
top=NULL;
if(!root) return 0;
p=root;
if(!push(p)) return -5;
printf("Lista Studentilor\n\n");
while(top){
p=pop();
printf("***************************************\n\n");
printf("Numele
%s\n",p->num);
printf("Prenume
%s\n",p->pre);
7

printf("Specialitatea
%s\n",p->spec);
printf("Anul
%d\n",p->anu);
printf("Media
%.2f\n\n",p->med);
c=p->right;
if(c!=NULL){
if(!push(c)) return -5;
}
c=p->left;
if(c!=NULL){
if(!push(c)) return -5;
}
}
pause
return 1;
}
int size_s(){
nod *p,*c;
int s=0;
top=NULL;
if(!root) return 0;
p=root;
if(!push(p)) return -5;
while(top){
p=pop(); s++;
c=p->right;
if(c!=NULL){
if(!push(c)) return -5;
}
c=p->left;
if(c!=NULL){
if(!push(c)) return -5;
}
}
return s;
}
nod* search_s(char *fname){
nod *p,*c;
top=NULL;
if(!root) return NULL;
p=root;
if(!push(p)) return NULL;
while(top){
p=pop();
if(!strcmp(fname,p->num)) return p;
c=p->right;
if(c!=NULL){
if(!push(c)) return NULL;
}
c=p->left;
if(c!=NULL){
if(!push(c)) return NULL;
}
}
return NULL;
8

}
int freemem_s(){
nod *p,*c;
top=NULL;
if(!root) return 0;
p=root;
if(!push(p)) return -5;
while(top){
p=pop();
c=p->right;
if(c!=NULL){
if(!push(c)) return -5;
}
c=p->left;
if(c!=NULL){
if(!push(c)) return -5;
}
free(p);
}
return 1;
}
int read_file(char *fname){
int f;
nod *c,*p;
FILE *demo=fopen(fname,"rt");
if(!demo) return -4;
first=last=NULL;
c=(nod*)malloc(sizeof(nod));
if(!c) return -1;
fscanf(demo,"%s%s%s%d%f",&c->num,&c->pre,&c->spec,&c->anu,&c->med);
if(!enq(c)) return -2;
root = c;
while(first){
p = deq();
fscanf(demo,"%d",&f);
if(f){
c=(nod*)malloc(sizeof(nod));
if(!c) return -3;
fscanf(demo,"%s%s%s%d%f",&c->num,&c->pre,&c->spec,&c->anu,&c->med);
p->left=c;
if(!enq(c)) return -2;
} else {
p->left=NULL;
}
fscanf(demo,"%d",&f);
if(f){
c=(nod*)malloc(sizeof(nod));
if(!c) return -3;
fscanf(demo,"%s%s%s%d%f",&c->num,&c->pre,&c->spec,&c->anu,&c->med);
p->right = c;
if(!enq(c)) return -2;
} else {
p->right = NULL;
}
9

}
return 1;
}
int write_file(char *fname){
nod *p,*c;
FILE *demo=fopen(fname,"w");
first=last=NULL;
if(!root) return 0;
p=root;
fprintf(demo,"%s %s %s %d %.2f",p->num,p->pre,p->spec,p->anu,p->med);
if(!enq(p)) return -2;
while(first){
p=deq();
c=p->left;
if(c){
if(!enq(c)) return -2;
fprintf(demo,"\n1\n%s %s %s %d %.2f",c->num,c->pre,c->spec,c->anu,c->med);
} else {
fprintf(demo,"\n0");
}
c=p->right;
if(c){
if(!enq(c)) return -2;
fprintf(demo,"\n1\n%s %s %s %d %.2f",c->num,c->pre,c->spec,c->anu,c->med);
} else {
fprintf(demo,"\n0");
}
}
fclose(demo);
return 1;
}
nod* creat_r(){
nod *c;
int f;
c=(nod*)malloc(sizeof(nod));
if(!c) return NULL;
print_scan
printf("De creat copilul sting al nodului %s (1/0)? : ",c->num);
fflush(stdin);
scanf("%d",&f);
if(f){
c->left=creat_r();
} else {
c->left=NULL;
}
printf("De creat copilul drept al nodului %s (1/0)? : ",c->num);
fflush(stdin);
scanf("%d",&f);
if(f){
c->right=creat_r();
} else {
c->right=NULL;
}
}
10

void show_r(nod *c){


if(!c){
return;
}
printf("***************************************\n\n");
printf("Numele
%s\n",c->num);
printf("Prenume
%s\n",c->pre);
printf("Specialitatea
%s\n",c->spec);
printf("Anul
%d\n",c->anu);
printf("Media
%.2f\n\n",c->med);
show_r(c->left);
show_r(c->right);
}
void print(nod *c,int k){
int i;
if(c){
print(c->right,k+3);
for(i=0;i<k;i++){
printf(" ");
}
printf("%s\n\n",c->num);
print(c->left,k+3);
}
}
void freemem_r(nod *c){
if(!c) return;
freemem_r(c->left);
freemem_r(c->right);
free(c);
}
nod* search_r(nod* c, char *fname){
nod* t=NULL;
if(!c) return NULL;
if (!strcmp(c->num,fname)) return c;
t=search_r(c->left,fname);
if (t) return t;
t=search_r(c->right,fname);
if (t) return t;
return NULL;
}
int size_r(nod *c){
static int n=0;
if(!c) return n;
n++;
size_r(c->left);
size_r(c->right);
return n;
}
int height_r(nod *c){
int l,r;
if(!c) return -1;
l=1+height_r(c->left);
r=1+height_r(c->right);
return (r>=l) ? 1+l : 1+r;
11

}
main.cpp
#include "funct.cpp"
int main(){
int comand,m=1,m1=0,m2=0,m3=0;
char fname[10];
nod *t=NULL;
while(1){
while(m){m=0; system("cls"); fflush(stdin);
printf("Meniu:\n\n");
printf("[ 1 ] Manual\n");
printf("[ 2 ] Fisier\n\n");
printf("[ 0 ] Exit\n\n");
printf("Comand >> ");
scanf("%d",&comand);
system("cls");
switch(comand){
case 0: exit(0); break;
case 1:
printf("Crearea arborelui :\n\n");
printf("[ 1 ] Folosind coada\n");
printf("[ 2 ] Folosind stiva\n");
printf("[ 3 ] Folosind functie recursiva\n\n");
printf("[ 0 ] Exit\n\n");
printf("Comand >> "); fflush(stdin);
scanf("%d",&comand);
system("cls");
switch(comand){
case 0: exit(0); break;
case 1: find_error(creat_q()); break;
case 2: find_error(creat_s()); break;
case 3: creat_r(); break;
default: printf("\a\nAti introdus o comanda gresita!\n");
m=1; break;
}
break;
case 2:
printf("Introduceti numele fisierului : ");
scanf("%s",&fname); fflush(stdin);
strcat(fname,".txt");
find_error(read_file(fname));
break;
default:
printf("\a\nAti introdus o comanda gresita!\n");
m=1; break;
} pause
}
system("cls"); fflush(stdin);
printf("Alegeti modul de lucru :\n");
printf("[ 1 ] Coada\n");
printf("[ 2 ] Stiva\n");
printf("[ 3 ] Recursie\n");
12

printf("[ 4 ] Meniul de introducere a datelor\n");


printf("[ 5 ] Afisarea vizuala\n");
printf("[ 6 ] Alege modul de lucru\n\n");
printf("[ 0 ] Exit\n\n");
printf("Comand >> ");
scanf("%d",&comand);
system("cls");
switch(comand){
case 0: exit(0); break;
case 1: m1=1; break;
case 2: m2=1; break;
case 3: m3=1; break;
case 4: m=1; break;
case 5: print(root); pause break;
case 6: printf("Introdu numele fisierului cu extensia .txt\n");
scanf("%s",&fname); write_file(fname); break;
default: printf("\a\nAti introdus o comanda gresita!\n"); break;
}
while(m1){ system("cls"); fflush(stdin);
printf("Operatii - Coada!\n\n");
printf("[ 1 ] Afisare\n");
printf("[ 2 ] Cautare\n");
printf("[ 3 ] Marimea arborelui\n");
printf("[ 4 ] Inaltimea !\n");
printf("[ 5 ] Eliberarea memoriei\n");
printf("[ 6 ] Alege modul de lucru\n\n");
printf("[ 0 ] Exit\n\n");
printf("Comand >> ");
scanf("%d",&comand);
system("cls");
switch(comand){
case 0: exit(0); break;
case 1:
printf("Afisare in largime\n");
find_error(show_q()); break;
case 2:
printf("Introdu Numele pentru cautare : ");
fflush(stdin); scanf("%s",&fname);
t=search_q(fname);
if(t){
printf("***************************************\n\n");
printf("Numele
%s\n",t->num);
printf("Prenume
%s\n",t->pre);
printf("Specialitatea
%s\n",t->spec);
printf("Anul
%d\n",t->anu);
printf("Media
%.2f\n",t->med);
} else {
printf("Nu sa gasit nici un student cu asa nume!\a");
} break;
case 3:
printf("Arborele are noduri ");
find_error(size_q()); break;
case 4:
printf("Inaltimea arborelui este %d",height_r(root));
13

break;
case 5:
find_error(freemem_q()); root=NULL; break;
case 6: m1=0; break;
default: printf("\a\nAti introdus o comanda gresita!\n"); break;
} pause
}
while(m2){ system("cls"); fflush(stdin);
printf("Operatii - Stiva!\n\n");
printf("[ 1 ] Afisare\n");
printf("[ 2 ] Cautare\n");
printf("[ 3 ] Marimea arborelui\n");
printf("[ 4 ] Inaltimea !\n");
printf("[ 5 ] Eliberarea memoriei\n");
printf("[ 6 ] Alege modul de lucru\n\n");
printf("[ 0 ] Exit\n\n");
printf("Comand >> ");
scanf("%d",&comand);
system("cls");
switch(comand){
case 0: exit(0); break;
case 1:
printf("Afisare in adincime\n");
find_error(show_s()); break;
case 2:
printf("Introdu Numele pentru cautare : ");
fflush(stdin); scanf("%s",&fname);
t=search_s(fname);
if(t){
printf("***************************************\n\n");
printf("Numele
%s\n",t->num);
printf("Prenume
%s\n",t->pre);
printf("Specialitatea
%s\n",t->spec);
printf("Anul
%d\n",t->anu);
printf("Media
%.2f\n",t->med);
} else {
printf("Nu sa gasit nici un student cu asa nume!\a");
} break;
case 3:
printf("Arborele are noduri ");
find_error(size_s()); break;
case 4:
printf("Inaltimea arborelui este %d",height_r(root));
break;
case 5:find_error(freemem_s()); root=NULL; break;
case 6: m2=0; break;
default: printf("\a\nAti introdus o comanda gresita!\n"); break;
} pause
}
while(m3){ system("cls"); fflush(stdin);
printf("Operatii - Recursie!\n\n");
printf("[ 1 ] Afisare\n");
printf("[ 2 ] Cautare\n");
printf("[ 3 ] Marimea arborelui\n");
14

printf("[ 4 ] Inaltimea !\n");


printf("[ 5 ] Eliberarea memoriei\n");
printf("[ 6 ] Alege modul de lucru\n\n");
printf("[ 0 ] Exit\n\n");
printf("Comand >> ");
scanf("%d",&comand);
system("cls");
switch(comand){
case 0: exit(0); break;
case 1:
printf("Afisare in adincime\n\n");
show_r(root); break;
case 2:
printf("Introdu Numele pentru cautare : ");
fflush(stdin); scanf("%s",&fname);
t=search_r(root,fname);
if(t){
printf("***************************************\n\n");
printf("Numele
%s\n",t->num);
printf("Prenume
%s\n",t->pre);
printf("Specialitatea
%s\n",t->spec);
printf("Anul
%d\n",t->anu);
printf("Media
%.2f\n",t->med);
} else {
printf("Nu sa gasit nici un student cu asa nume!\a");
} break;
case 3:
printf("Arborele are noduri ");
find_error(size_r(root)); break;
case 4:
printf("Inaltimea arborelui este %d",height_r(root));
break;
case 5:
freemem_r(root); root=NULL;
printf("Eliberarea memoriei a avut loc cu succes!\a");
break;
case 6: m3=0; break;
default: printf("\a\nAti introdus o comanda gresita!\n"); break;
} pause
}
}
return 0;

15

16

17

Concluzii: In urma acestei lucrari de laborator am ajuns la ideia ca e destul de usor de


lucrat cu structuri de date liste, ele sunt usor de modificat si lucrul dat nu necesita mult
timp. Deasemenea verificarea rezultatelor demonstreaza ca valoariile primite sunt
adevarate ceea ce arata ca programul lucreaza .
Bibliografie:

http://programmersclub.ru/01/

TOTUL DESPRE C SI C++ (MANUALUL FUNDAMENTAL DE


PROGRAMARE IN C SI C++)[RO][Kris Jamsa][Lars Kland]

Limbajul C pentru clasa a-IX-a

http://www.scribd.com/doc/41293824/Negrescu-Liviu-Limbajele-C-si-Cpentru-incepatori-vol-1

http://devcentral.iftech.com/learning/tutorials/c-cpp/c

18