Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Liste

#include <stdio.h>
#include <string.h>
#include <malloc.h>
struct Angajat{
unsigned int id;
char* nume;
char* pozitie;
float salariul;
};
struct Nod{
Angajat ang;
Nod* next;
};
//INSERARE LA SFARSIT
Nod* inserareSf(Nod* prim, Angajat a)
{
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->ang = a;
nou->next = 0;
if (!prim)
return nou;
else
{
Nod* t = prim;
while (t->next)
t = t->next;
t->next = nou;
return prim;
}
}
//INSERARE LA MIJLOC, DUPA ID
Nod* inserareMijl(Nod* prim, Angajat a)
{
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->ang = a;
nou->next = 0;
if (!prim)
return nou;
else if (prim->ang.id > a.id)
{
nou->next = prim;
return nou;
}
else
{
Nod* t = prim;
while (t->next && t->next->ang.id < a.id)
t = t->next;
if (t->next)
{
nou->next = t->next;
t->next = nou;
}
else
{

t->next = nou;
}
return prim;
}
}
//STERGERE NOD
Nod* stergereNod(Nod* prim, int id)
{
Nod* t = prim;
Nod* x;
if (t->ang.id == id)
{
x = t;
prim = prim->next;
}
else
{
while (t->next && t->next->ang.id != id)
t = t->next;
if (!t->next->next)
{
x = t->next;
t->next = 0;
}
else
{
x = t->next;
t->next = t->next->next;
}
}
free(x->ang.nume); free(x->ang.pozitie);
return prim;
}

//INTERSCHIMBARE NODURI
Nod* interschimbare(Nod* prim, int x, int y)
{
if (x == y) return prim;
Nod *prevX = NULL;
Nod* currX = prim;
while (currX
prevX
currX
}
Nod* prevY =
Nod* currY =
while (currY
{
prevY
currY
}

&& currX->ang.id != x){


= currX;
= currX->next;
NULL;
prim;
&& currY->ang.id != y)
= currY;
= currY->next;

if (!currX || !currY)
return prim;
if (prevX != NULL)
prevX->next = currY;
else prim = currY;

if (prevY != NULL)
prevY->next = currX;
else prim = currX;
Nod* tmp = currY->next;
currY->next = currX->next;
currX->next = tmp;
return prim;
}
//INTERCLASARE
Nod* interclasare(Nod* lista1, Nod* lista2)
{
Nod* lista = 0;
Nod* temp1 = lista1;
Nod* temp2 = lista2;
while (temp1->next)
{
while (temp2->next && temp2->ang.id <= temp1->ang.id)
{
lista = inserareMijl(lista, temp2->ang);
temp2 = temp2->next;
}
lista = inserareMijl(lista, temp1->ang);
temp1 = temp1->next;
}
while (temp1)
{
lista = inserareMijl(lista, temp1->ang);
temp1 = temp1->next;
}
while (temp2)
{
lista = inserareMijl(lista, temp2->ang);
temp2 = temp2->next;
}
return lista;
}

void Afisare(Nod* p)
{
Nod* t = p;
while (t)
{
printf("\n%d
t->ang.salariul);
t = t->next;
}
printf("\n");
}
void main()
{
Angajat a;
Nod* prim = 0;

%s

%s

%4.2f", t->ang.id, t->ang.nume, t->ang.pozitie,

//CITIRE DIN FISIER


FILE *f;
f = fopen("text.txt", "r");
char buffer[120];
while (fgets(buffer, sizeof(buffer), f)) //in ce baga, marimea, de unde
{
char pozBuf[20], numeBuf[50];
sscanf(buffer, "%d %f %[^;]; %[^\n]", &a.id, &a.salariul, pozBuf,
numeBuf);
a.nume = (char*)malloc((strlen(numeBuf) + 1)*sizeof(char));
strcpy(a.nume, numeBuf);
a.pozitie = (char*)malloc((strlen(pozBuf) + 1)*sizeof(char));
strcpy(a.pozitie, pozBuf);
prim = ins(prim, a);
}
Afisare(prim);
//printf("\nId nod de sters=");
int id;
//scanf("%d", &id);
//prim=stergereNod(prim, id);
//Afisare(prim);
int x, y;
printf("x=");
scanf("%d", &x);
printf("y=");
scanf("%d", &y);
prim=interschimbare(prim, x, y);
Afisare(prim);
}

Hash-chaining
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define DIM 250
struct Angajat {
unsigned int id;
char* nume;
char* pozitie;
float salariu;
};
struct Nod
{
Angajat ang;
Nod* next;
};

//FUNCTIA HASH
int Hash(char* str, int size)
{

int s = 0;
for (int i = 0; i < strlen(str); i++)
s += str[i];
return s%size;
}
//INSERARE IN TABELA
int inserareHash(Nod** hT, Angajat a,int size)
{
int poz = Hash(a.nume, size);
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->next = 0;
nou->ang = a;
if (hT[poz] == NULL)
{
hT[poz] = nou;
}
else
{
Nod* temp = hT[poz];
while (temp->next)
temp = temp->next;
temp->next = nou;
}
return 1;
}
int ins(Nod** hT, Angajat a, int size)
{
int er = 1;
int poz = Hash(a.nume, size);
Nod* nou;
nou = (Nod*)malloc(sizeof(Nod));
nou->ang = a;
nou->next = 0;
if (hT[poz] == NULL)
{
hT[poz] = nou;
}
else
{
Nod* t = hT[poz];
while (t->next)
t = t->next;
t->next = nou;
}
return er;
}
//AFISARE ANGAJAT
void afisareAngajat(Angajat a)
{
printf("\n%d %s", a.id, a.nume);
}
//PARCURGERE TABELA
void afisareHash(Nod** hT, int size)
{
for (int i = 0; i < size; i++)

{
if (hT[i])
{
Nod* t = hT[i];
while (t)
{
afisareAngajat(t->ang);
t = t->next;
}
}
}
}
void afisareInFisier(Nod**hT, int size, char* numeFis)
{
FILE *f = fopen(numeFis, "w");
for (int i = 0; i < size;i++)
if (hT[i])
{
Nod* t = hT[i];
while (t)
{
fprintf(f, "%id %s\n", t->ang.id, t->ang.nume);
t = t->next;
}
}
}
//STERGERE NOD LISTA
Nod* stergereNodLista(Nod* prim, char* nume)
{
Nod* t = prim;
Nod* x = 0;
if (strcmp(t->ang.nume,nume)==0)
{
x = t;
prim = prim->next;
}
else
{
while (t->next && strcmp(t->next->ang.nume,nume)!=0)
t = t->next;
if (strcmp(t->next->ang.nume, nume)==0)
{
if (!t->next->next)
{
x = t->next;
t->next = 0;
}
else
{
x = t->next;
t->next = x->next;
}
}
}
if (x)
{
free(x->ang.nume);
free(x->ang.pozitie);
}
return prim;

}
//STERGERE ANGAJAT
void stergereAngajat(Nod** hT, char* nume, int size)
{
int poz = Hash(nume, size);
if (hT[poz])
hT[poz]=stergereNodLista(hT[poz],nume);
}
void main()
{
Nod **HTable; Angajat a;
HTable = (Nod**)malloc(DIM*sizeof(Nod*));
for (int i = 0; i < DIM; i++)
HTable[i] = NULL;
FILE *f;
f = fopen("Text.txt", "r");
char buffer[128];
int inserat = 1;
while (fgets(buffer, sizeof(buffer), f) && inserat) {
char pozBuf[20], numeBuf[50];
sscanf(buffer, "%d %f %[^;]; %[^\n]", &a.id, &a.salariu, pozBuf,
numeBuf);
a.pozitie = (char*)malloc((strlen(pozBuf) + 1) * sizeof(char));
strcpy(a.pozitie, pozBuf);
a.nume = (char*)malloc((strlen(numeBuf) + 1) * sizeof(char));
strcpy(a.nume, numeBuf);
// inserare angajat in tabela hash
inserat = inss(HTable, a, DIM);
if (!inserat) {
free(a.pozitie);
free(a.nume);
}
}
afisareHash(HTable, DIM);
stergereAngajat(HTable, "Popescu Marian",DIM);
printf("\n");
afisareHash(HTable, DIM);
afisareInFisier(HTable, DIM, "ang.txt");
}

Problema2:

#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define DIM 250


enum StareTask{
DESCHIS, IN_LUCRU, DUPLICAT, REZOLVAT, INCHIS
};
struct Task{
char* id;
char* data;
int nivelComplexitate;
char* numeInginer;
StareTask stareTask;
};
struct Nod{
Task task;
Nod* next;
};
int functieHash(char* str, int size){
int cheie = 0;
for (int i = 0; i < strlen(str); i++)
cheie += str[i];
return cheie%size;
}
int inserareHash(Nod** ht, Task t, int size){
int er = 1;
int poz = functieHash(t.numeInginer, size);
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->task = t;
nou->next = 0;
if (!ht[poz]) ht[poz] = nou;
else{
Nod* temp = ht[poz];
while (temp->next) temp = temp->next;
temp->next = nou;
}
return er;
}
void afisareTask(Task t){
printf("\n %s %s %s %d", t.id, t.data, t.numeInginer, t.nivelComplexitate);
switch (t.stareTask){
case 0:
printf(" DESCHIS");
break;
case 1:
printf(" IN_LUCRU");
break;
case 2:
printf(" DUPLICAT");
break;
case 3:
printf(" REZOLVAT");
break;
case 4:
printf(" INCHIS");
break;

default:
break;
}
}
void afisareHash(Nod**ht, int size){
for (int i = 0; i < size; i++){
if (ht[i]){
Nod* temp = ht[i];
while (temp){
afisareTask(temp->task);
temp = temp->next;
}
}
}
}
Nod* stergereNume(Nod* prim, char* nume){
Nod* temp = prim;
if (strcmp(prim->task.numeInginer, nume) == 0)
return prim->next;
else{
while (strcmp(temp->next->task.numeInginer, nume) != 0)
temp = temp->next;
temp->next = temp->next->next;
}
return prim;
}
void modificareNume(Nod** ht, char* numeVechi, char* numeNou, int size){
int poz = functieHash(numeVechi, size);
if (!ht[poz]) printf("\n\nNumele cautat nu exista");
else
{
Nod* t = ht[poz];
while (t&&strcmp(t->task.numeInginer, numeVechi) != 0)
t = t->next;
if (!t)
printf("\n\nNumele cautat nu exista");
else{
Task tsk = t->task;
ht[poz] = stergereNume(ht[poz], numeVechi);
free(tsk.numeInginer);
tsk.numeInginer = (char*)malloc((strlen(numeNou) +
1)*sizeof(char));
strcpy(tsk.numeInginer, numeNou);
int inserat = inserareHash(ht, tsk, size);
}
}
afisareHash(ht, size);
}
Nod* stergereNodInchis(Nod* prim){
Nod* temp = prim;
if (temp->task.stareTask == 4)
return prim->next;

else{
while (temp->next){
while (temp->next&&temp->next->task.stareTask != 4)
temp = temp->next;
if (temp->next){
temp->next = temp->next->next;
}
return prim;
}
}
}
void stergereInchise(Nod** hT, int size){
for (int i = 0; i < size;i++)
if (hT[i]){
hT[i] = stergereNodInchis(hT[i]);
}
afisareHash(hT, size);
}

void salvare(Nod** hT, int size){


FILE*g = fopen("Text.txt", "w");
for (int i = 0; i < size;i++)
if (hT[i]){
Nod* t = hT[i];
while (t){
fprintf(g, "%s %s %s %d %d\n", t->task.id, t->task.data, t>task.numeInginer, t->task.nivelComplexitate, t->task.stareTask);
t = t->next;
}
}
printf("\nScris cu succes");
fclose(g);
}
void main(){
FILE* f = fopen("Text.txt", "r");
Task t;
Nod** hT;
//alocare hashTable
hT = (Nod**)malloc(DIM*sizeof(Nod*));
for (int i = 0; i < DIM; i++)
hT[i] = NULL;
char buffer[128]; int inserat = 1;
while (fgets(buffer, sizeof(buffer),f)&&inserat){
char idBuf[40], dataBuf[40], numeBuf[40];
sscanf(buffer, "%s %s %s %d %d", idBuf, dataBuf, numeBuf,
&t.nivelComplexitate, &t.stareTask);
t.id = (char*)malloc((strlen(idBuf) + 1)*sizeof(char));
strcpy(t.id, idBuf);
t.data = (char*)malloc((strlen(dataBuf) + 1)*sizeof(char));
strcpy(t.data, dataBuf);
t.numeInginer = (char*)malloc((strlen(numeBuf) + 1)*sizeof(char));
strcpy(t.numeInginer, numeBuf);

inserat = inserareHash(hT, t, DIM);


if (!inserat){
free(t.id);
free(t.data);
free(t.numeInginer);
}
}
fclose(f);
afisareHash(hT, DIM);
printf("\nNume vechi=");
char numeV[40],numeN[40];
scanf("%s", numeV);
printf("\nNume nou=");
scanf("%s", numeN);
modificareNume(hT, numeV, numeN, DIM);
printf("\n\n");
stergereInchise(hT, DIM);
salvare(hT, DIM);

Hash- linear probing


#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define DIM

250

struct Angajat {
unsigned int id;
char* nume;
char* pozitie;
float salariu;
};
int Hash(char *str, int size)
{
int cheie = 0;
for (int i = 0; i < strlen(str); i++)
cheie += str[i];
return cheie%size;
}
int inserareHash(Angajat *hT, Angajat a, int size)
{
int inserat = 1; //daca e 0=esec inserare
int poz = Hash(a.nume, size);
if (hT[poz].id == 0)
hT[poz] = a;
else //coliziune
{
int poznou = -1;
int i=poz+1;

while (hT[i].id != 0 && i < size)


i++;
if (i < size)
hT[i] = a;
else
{
int j = poz - 1;
while (hT[j].id != 0 && j >= 0)
j--;
if (j >= 0)
hT[j] = a;
else inserat = 0;
}
}
return inserat;
}
int ins(Angajat *hT, Angajat a, int size)
{
int er = 1;
int poz = Hash(a.nume, size);
if (hT[poz].id == 0)
hT[poz] = a;
else
{
int i = poz + 1;
while (i < size && hT[i].id != 0)
i++;
if (i < size)
hT[i] = a;
else
{
int j = poz - 1;
while (j >= 0 && hT[j].id != 0)
j--;
if (j >= 0)
hT[j] = a;
else er = 0;
}
}
return er;
}
int inss(Angajat *hT, Angajat a, int size)
{
int poz = Hash(a.nume, size);
if (hT[poz].id == 0)
hT[poz] = a;
else
{
int i = poz + 1;
while (i < size && hT[i].id != 0)
i++;
if (i < size)
hT[i] = a;
else
{
int j = poz - 1;
while (j >= 0 && hT[j].id != 0)
j--;
if (j >= 0)
hT[j] = a;

else
return 0;
}
}
}
//CAUTARE ANGAJAT DUPA NUME
int cautare(Angajat* hT, int size, char* nume)
{
int poz = Hash(nume,size);
int gasit = -1;
if (hT[poz].id == 0)
gasit = -1;
else if (strcmp(hT[poz].nume, nume) == 0)
gasit = poz;
else
{
int i = poz + 1;
while (hT[i].id != 0 && strcmp(hT[i].nume, nume) != 0 && i < size)
i++;
if (i==size || hT[i].id==0)
gasit = -1;
else gasit = i;
if (gasit == -1)
{
int j = poz - 1;
while (hT[j].id != 0 && strcmp(hT[j].nume, nume) != 0 && j >= 0)
j--;
if (j == -1|| hT[j].id == 0)
gasit = -1;
else gasit = j;
}
}
return gasit;
}
//STERGERE ANGAJAT
void stergereAngajat(Angajat* hT, char* nume, int size)
{
int hashPos = Hash(nume, size);
int realPos = cautare(hT, size, nume);
if (realPos != -1)
{
//dezalocam memoria pt angajatul care trebuie sters
free(hT[realPos].nume);
free(hT[realPos].pozitie);
hT[realPos].id = 0;
Angajat *newHT = (Angajat*)malloc(size*sizeof(Angajat));
for (int i = 0; i < size; i++)
newHT[i].id = 0;
for (int i = 0; i < size; i++)
{
if (hT[i].id != 0)
{
int inserat = 0;
inserat = inserareHash(newHT, hT[i], size);
}
}
for (int i = 0; i < size; i++)
hT[i] = newHT[i];
free(newHT);

}
}
void stergere(Angajat* hT, char* nume, int size)
{
int poz = cautare(hT, size, nume);
if (poz != -1)
{
free(hT[poz].nume);
free(hT[poz].pozitie);
hT[poz].id = 0;
Angajat* hNou=(Angajat*)malloc(size*sizeof(Angajat));
for (int i = 0; i < size; i++)
hNou[i].id = 0;
for (int i = 0; i < size;i++)
if (hT[i].id)
int x = inss(hNou, hT[i], size);
for (int i = 0; i < size; i++)
hT[i] = hNou[i];
free(hNou);
}

}
//AFISARE
void afisareHash(Angajat *hT, int size)
{
for (int i = 0; i < size;i++)
if (hT[i].id != 0)
printf("\n\n%d, %s, %d", hT[i].id, hT[i].nume,i);
}
void main()
{
Angajat *HTable, a;
HTable = (Angajat*)malloc(DIM*sizeof(Angajat));
for (int i = 0; i < DIM; i++)
{
HTable[i].id = 0;
}
FILE *f;
f = fopen("Text.txt", "r");
char buffer[128];
int inserat = 1;
while (fgets(buffer, sizeof(buffer), f) && inserat) {
char pozBuf[20], numeBuf[50];
sscanf(buffer, "%d %f %[^;]; %[^\n]", &a.id, &a.salariu, pozBuf,
numeBuf);
a.pozitie = (char*)malloc((strlen(pozBuf) + 1) * sizeof(char));
strcpy(a.pozitie, pozBuf);
a.nume = (char*)malloc((strlen(numeBuf) + 1) * sizeof(char));
strcpy(a.nume, numeBuf);
// inserare angajat in tabela hash
inserat = inss(HTable, a, DIM);

if (!inserat) {
free(a.pozitie);
free(a.nume);
}
}
afisareHash(HTable, DIM);
printf("\n%d", cautare(HTable, DIM, "Vasilescu"));
stergere(HTable, "Popescu Marian", DIM);
afisareHash(HTable, DIM);

Arbori binari de cautare


#include
#include
#include
#include

<stdio.h>
<malloc.h>
<string.h>
<math.h>

#define LINESIZE 128


#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif
struct Angajat {
unsigned int id;
char* nume;
char* pozitie;
float salariu;
};

struct NodABC{
Angajat ang;
NodABC *st, *dr;
};
//INSERARE NOD
NodABC *inserare(NodABC *r, Angajat a,int &err)
{
if (!r)
{
r = (NodABC*)malloc(sizeof(NodABC));
r->ang = a;
r->dr = 0; r->st = 0;
}
else
{
if (r->ang.id < a.id)
r->dr = inserare(r->dr, a,err);
else if (r->ang.id>a.id)
r->st = inserare(r->st, a,err);
else if (r->ang.id == a.id)

err = 1;
}
return r;
}
NodABC* ins(NodABC *r, Angajat a, int &err)
{
if (!r)
{
NodABC* nou = (NodABC*)malloc(sizeof(NodABC));
nou->ang = a;
nou->dr = 0; nou->st = 0;
return nou;
}
else
{
if (r->ang.id > a.id)
r->st = ins(r->st, a, err);
else if (r->ang.id < a.id)
r->dr = ins(r->dr, a, err);
else err = 1;
}
return r;
}
//PARCURGERE IN INORDINE
void inordine(NodABC *r)
{
if (r)
{
inordine(r->st);
printf("\n%d, %s", r->ang.id, r->ang.nume);
inordine(r->dr);
}
}
//NR NODURI
int numar(NodABC *r)
{
if (r)
return 1 + numar(r->st) + numar(r->dr);
return 0;
}
//AFISAREA NODURILOR DE PE UN NIVEL DAT
void afisareNivel(NodABC *r, int nivelCurr, int nivel)
{
if (r)
{
if (nivelCurr == nivel)
printf("\n%d,%s", r->ang.id, r->ang.nume);
else
{
afisareNivel(r->st, nivelCurr + 1, nivel);
afisareNivel(r->dr, nivelCurr + 1, nivel);
}
}
}
//NR NODURI DE PE UN NIVEL DAT
void nrNoduri(NodABC *r, int nivelCurr, int nivel,int *nr)
{

if (r)
{
if (nivelCurr == nivel)
*nr = *nr + 1;
else
{
nrNoduri(r->st, nivelCurr + 1, nivel,nr);
nrNoduri(r->dr, nivelCurr + 1, nivel,nr);
}
}
}

//INALTIME ARBORE
int inaltime(NodABC *r)
{
if (r)
return 1 + max(inaltime(r->dr), inaltime(r->st));
else return 0;
}

//STERGERE ANGAJAT
NodABC* stergere(NodABC *r, int id)
{
if (r)
{
if (r->ang.id > id)
r->st = stergere(r->st, id);
else if(r->ang.id<id) r->dr = stergere(r->dr, id);
else
{
if (r->st == NULL && r->dr == NULL)
{
free(r);
r = NULL;
}
else if (r->st == NULL)
{
NodABC *temp = r;
r = r->dr;
free(temp);
}
else if (r->dr == NULL)
{
NodABC *temp = r;
r = r->st;
free(temp);
}
else
{
NodABC *min = r->dr;
while (min->st)
min = min->st;
r->ang = min->ang;
r->dr = stergere(r->dr, r->ang.id);
}

}
return r;
}
}
//AFISARE FRUNZE
void afisareFrunze(NodABC* r)
{
if (r)
{
if (!r->st && !r->dr)
printf("\n%d, %s", r->ang.id, r->ang.nume);
else
{
afisareFrunze(r->st);
afisareFrunze(r->dr);
}
}
}

//DEZALOCARE
NodABC* dezalocare(NodABC* r)
{
if (r)
{
dezalocare(r->st);
dezalocare(r->dr);
free(r->ang.nume); free(r->ang.pozitie);
free(r);
r = NULL;
}
return r;
}
void main()
{
NodABC *root = 0;
Angajat a;
FILE *f = fopen("Text.txt", "r");
char buffer[128];
int inserat = 1;
while (fgets(buffer, sizeof(buffer), f) && inserat) {
char pozBuf[20], numeBuf[50];
sscanf(buffer, "%d %f %[^;]; %[^\n]", &a.id, &a.salariu, pozBuf,
numeBuf);
a.pozitie = (char*)malloc((strlen(pozBuf) + 1) * sizeof(char));
strcpy(a.pozitie, pozBuf);
a.nume = (char*)malloc((strlen(numeBuf) + 1) * sizeof(char));
strcpy(a.nume, numeBuf);
//inserare in ABC
int err = 0; //daca inserarea nu are loc, err devine 1; nu putem avea
angajati cu acelasi id
root = ins(root, a, err);
if (err){

printf("Angajatul cu id %d exista deja.\n", a.id);


free(a.pozitie); free(a.nume);
}
else
printf("Angajatul cu id %d a fost inserat.\n", a.id);
}
inordine(root);
printf("\nnr noduri:%d", numar(root));
printf("\n\ninaltime:%d", inaltime(root));
printf("\n\n Ang de pe nivelul 2:");
afisareNivel(root, 1, 2);
int nr = 0;
//nrNoduri(root, 1, 2, &nr);
//printf("\n\nnr noduri de pe nivelul 2:%d", nr);
root = stergere(root, 125);
printf("\n\n");
afisareNivel(root, 1, 2);
//printf("\n\n%d", root->dr->ang.id);
printf("\n\nFrunzele:");
afisareFrunze(root);
root=dezalocare(root);
if (!root)
printf("\ndap");
}

Problema cu ABC
#include <stdio.h>
#include<string.h>
#include <malloc.h>
enum Sursa{
TELEFONIC, ON_LINE, SHOW_ROOM
};
enum Stare{
IN_LUCRU, CONFIRMATA, LIVRARE, EXECUTATA
};
struct Comanda{
unsigned int id;
char* numeClient;
int nrProduse;
int valoare;
Sursa surse;
Stare stare;
};
struct nodABC{
Comanda com;
nodABC* st, *dr;
};
struct Nod{
Comanda c;
Nod* next;
};

nodABC* inserareABC(nodABC* r, Comanda c, int *err){


if (!r)
{
nodABC* nou = (nodABC*)malloc(sizeof(nodABC));
nou->com = c;
nou->st = 0;
nou->dr = 0;
r = nou;
}
else
{
if (r->com.id < c.id) r->dr = inserareABC(r->dr, c, err);
else if (r->com.id>c.id) r->st = inserareABC(r->st, c, err);
else
*err = 1;
}
return r;
}
void afisareComanda(Comanda c){
printf("%d %s %d %d ", c.id, c.numeClient, c.nrProduse, c.valoare);
switch (c.surse){
case 0:
printf(" telefonic ");
break;
case 1:
printf(" on-line ");
break;
case 2:
printf(" show room ");
break;
default:
break;
}
switch (c.stare)
{
case 0:
printf(" in lucru ");
break;
case 1:
printf(" confirmata ");
break;
case 2:
printf(" livrare ");
break;
case 3:
printf(" executata ");
break;
default:
break;
}
printf("\n");
}
void inordine(nodABC* r){
if (r){
inordine(r->st);
afisareComanda(r->com);
inordine(r->dr);
}
}

void modificarestare(nodABC* r){


if (r){
if (r->com.stare == 2)
r->com.stare = EXECUTATA;
modificarestare(r->st);
modificarestare(r->dr);
}
}
Nod* inserareLista(Nod* prim, Comanda comanda){
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->c = comanda;
nou->next = 0;
if (!prim)
prim = nou;
else{
Nod* temp = prim;
while (temp->next) temp = temp->next;
temp->next = nou;
return prim;
}
}
void DuplicareNivel(nodABC* r, int nivelCurrent, int nivel, Nod** prim){
if (r){
if (nivelCurrent == nivel)
(*prim) = inserareLista(*prim, r->com);
DuplicareNivel(r->st, nivelCurrent + 1, nivel, prim);
DuplicareNivel(r->dr, nivelCurrent + 1, nivel, prim);
}
}
void afisareLista(Nod* prim){
Nod* t = prim;
while (t){
afisareComanda(t->c);
t = t->next;
}
}
void main(){
FILE* f = fopen("Text.txt", "r");
Comanda c;
nodABC* root = 0;
while (!feof(f)){
char buffer[200];
fgets(buffer, sizeof(buffer), f);
char numeBuf[40], sursaBuf[40], stareBuf[40];
sscanf(buffer, "%d %s %d %d %[^;]; %[^\n]", &c.id, numeBuf, &c.nrProduse,
&c.valoare, sursaBuf, stareBuf);
c.numeClient = (char*)malloc((strlen(numeBuf) + 1)*sizeof(char));
strcpy(c.numeClient, numeBuf);
if (strcmp(sursaBuf, "telefonic") == 0)
c.surse = TELEFONIC;
else if (strcmp(sursaBuf, "on-line") == 0)
c.surse = ON_LINE;
else if (strcmp(sursaBuf, "show room") == 0)
c.surse = SHOW_ROOM;
if (strcmp(stareBuf, "in lucru") == 0)
c.stare = IN_LUCRU;

else if (strcmp(stareBuf, "confirmata") == 0)


c.stare = CONFIRMATA;
else if (strcmp(stareBuf, "livrare") == 0)
c.stare = LIVRARE;
else if (strcmp(stareBuf, "executata") == 0)
c.stare = EXECUTATA;
int err = 0;
root = inserareABC(root, c, &err);
if (err){
printf("\n Comanda cu id-ul %d exista deja.", c.id);
free(c.numeClient);
}
else
printf("\n Comanda cu id-ul %d a fost inserata.", c.id);
}
printf("\n\nINORDINE:\n");
inordine(root);
modificarestare(root);
printf("\n");
inordine(root);
Nod* prim = 0;
DuplicareNivel(root, 1, 2, &prim);
printf("afiseaza lista:\n");
afisareLista(prim);
}

AVL
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define max(a,b) ((a)>(b) ? (a):(b))
struct Angajat {
unsigned int id;
char* nume;
char* pozitie;
float salariu;
};
struct NodAVL{
Angajat ang;
int GE;
NodAVL *st, *dr;
};
//INALTIME
int inaltime(NodAVL *r)
{
if (r)
return 1 + max(inaltime(r->st), inaltime(r->dr));
else return 0;
}

//CALCULARE GRAD DE ECHILIBRU AL UNUI NOD


void calculGE(NodAVL *r)
{
if (r)
{
r->GE = inaltime(r->dr) - inaltime(r->st);
}
}
//ROTIRE SIMPLA LA DREAPTA
NodAVL *rotireSimplaDr(NodAVL *pivot, NodAVL *fiuSt)
{
pivot->st = fiuSt->dr;
calculGE(pivot);
fiuSt->dr = pivot;
calculGE(fiuSt);
return fiuSt;
}
NodAVL *rotireDr(NodAVL* pivot, NodAVL * fiuSt)
{
pivot->st = fiuSt->dr;
calculGE(pivot);
fiuSt->dr = pivot;
calculGE(fiuSt);
return fiuSt;
}
//ROTIRE SIMPLA LA STANGA
NodAVL *rotireSimplaSt(NodAVL *pivot, NodAVL *fiuDr)
{
pivot->dr = fiuDr->st;
calculGE(pivot);
fiuDr->st = pivot;
calculGE(fiuDr);
return fiuDr;
}
NodAVL *rotireSt(NodAVL *pivot, NodAVL *fiuDr)
{
pivot->dr = fiuDr->st;
calculGE(pivot);
fiuDr->st = pivot;
calculGE(fiuDr);
return fiuDr;
}
//ROTIRE DUBLA STANGA-DREAPTA
NodAVL* rotireStDr(NodAVL *pivot, NodAVL *fiuSt)
{
pivot->st = rotireSimplaSt(fiuSt, fiuSt->dr);
calculGE(pivot);
fiuSt = pivot->st;
fiuSt = rotireSimplaDr(pivot, fiuSt);
calculGE(fiuSt);
return fiuSt;
}
//ROTIRE DUBLA DREAPTA-STANGA
NodAVL *rotireDrSt(NodAVL *pivot, NodAVL *fiuDr)
{
pivot->dr = rotireSimplaDr(fiuDr, fiuDr->st);
calculGE(pivot);

fiuDr = pivot->dr;
pivot = rotireSimplaSt(pivot, fiuDr);
calculGE(pivot);
return pivot;
}
//INSERARE NOD
NodAVL *inserareAVL(NodAVL *r, Angajat a, int *err)
{
if (r)
{
if (r->ang.id < a.id) r->dr = inserareAVL(r->dr, a, err);
else if (r->ang.id>a.id) r->st = inserareAVL(r->st, a, err);
else *err = 1;
}
else
{
NodAVL *nou;
nou = (NodAVL*)malloc(sizeof(NodAVL));
nou->ang = a;
nou->dr = 0; nou->st = 0;
r = nou;
}
//recalculam gradul de echilibru al nodului curent
calculGE(r);
if (r->GE == 2)
{
if (r->dr->GE == -1)
//dezechilibru combinat dreapta-stanga
r = rotireDrSt(r, r->dr);
else
{
//dezechiliru dreapta
r = rotireSimplaSt(r, r -> dr);
}
}
else if (r->GE == -2)
{
if (r->st->GE == 1)
//dezechilivru combinat stanga-dreapta
r = rotireStDr(r, r->st);
else
//dezechilibru stanga
r = rotireSimplaDr(r, r->st);
}
return r;
}
//PREORDINE
void afisarePreordine(NodAVL *r)
{
if (r)
{
printf("\n%d, %s", r->ang.id, r->ang.nume);
afisarePreordine(r->st);
afisarePreordine(r->dr);
}
}
//POSTORDINE
void afisarePostordine(NodAVL *r)

{
if (r)
{
afisarePreordine(r->st);
afisarePreordine(r->dr);
printf("\n%d, %s", r->ang.id, r->ang.nume);
}
}
//AFISARE NIVEL SPECIFICAT
void afisareNivel(NodAVL *r, int nivel, int nivelCurr)
{
if (r)
{
if (nivel == nivelCurr)
printf("\n%d %s", r->ang.id, r->ang.nume);
else
{
afisareNivel(r->st, nivel, nivelCurr + 1);
afisareNivel(r->dr, nivel, nivelCurr + 1);
}
}
}
//AFISARE PE NIVELE
void afisarePeNivele(NodAVL *r)
{
int i = inaltime(r);
for (int j = 1; j <= i; j++)
{
printf("\n\nNivel %d", j);
afisareNivel(r, j, 1);
}
}
void main()
{
NodAVL *root = 0;
Angajat a;
FILE *f = fopen("Text.txt", "r");
char buffer[200];
while (!feof(f))
{
fgets(buffer,sizeof(buffer),f);
char pozBuf[20]; char numeBuf[40];
sscanf(buffer, "%d %d %[^;]; %[^\n]", &a.id, &a.salariu, pozBuf,
numeBuf);
a.pozitie = (char*)malloc((strlen(pozBuf + 1))*sizeof(char));
strcpy(a.pozitie, pozBuf);
a.nume = (char*)malloc((strlen(numeBuf + 1))*sizeof(char));
strcpy(a.nume, numeBuf);
int err = 0;
root = inserareAVL(root, a, &err);
if (err){
printf("\nAngajatul cu id %d deja exista.", a.id);
free(a.pozitie); free(a.nume);
}
else
printf("\nAngajatul cu id %d a fost inserat.", a.id);
}

afisarePeNivele(root);
}

Problema cu AVL
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct Client{
int idClient;
char* numeClient;
int bugetClient;
int durataProceduri;
int costPRoceduri;
};
struct NodAVL{
Client c;
NodAVL*st, *dr;
int GE;
};
struct Nod{
Client c;
Nod* next;
};
int max(int a, int b){
return a > b ? a : b;
}
int h(NodAVL* r){
if (r)
return 1 + max(h(r->dr), h(r->st));
else return 0;
}
void calculGE(NodAVL* r){
if (r)
r->GE = h(r->dr) - h(r->st);
}
NodAVL* rotireSimplaSt(NodAVL* pivot, NodAVL* fiuDr){
pivot->dr = fiuDr->st;
calculGE(pivot);
fiuDr->st = pivot;
calculGE(fiuDr);
return fiuDr;
}
NodAVL* rotireSimplaDr(NodAVL* pivot, NodAVL* fiuSt){
pivot->st = fiuSt->dr;
calculGE(pivot);
fiuSt->dr = pivot;
calculGE(fiuSt);
return fiuSt;
}
NodAVL* rotireDrSt(NodAVL* pivot, NodAVL* fiuDr){
pivot->dr = rotireSimplaDr(fiuDr, fiuDr->st);
calculGE(pivot);
fiuDr = pivot->dr;

fiuDr = rotireSimplaSt(pivot, fiuDr);


return fiuDr;
}
NodAVL* rotireStDr(NodAVL* pivot, NodAVL* fiuSt){
pivot->st = rotireSimplaSt(fiuSt, fiuSt->dr);
calculGE(pivot);
fiuSt = pivot->st;
fiuSt = rotireSimplaDr(pivot, fiuSt);
return fiuSt;
}
NodAVL* inserareAVL(NodAVL* r, Client client, int *err){
if (r){
if (r->c.idClient < client.idClient) r->dr = inserareAVL(r->dr, client,
err);
else if (r->c.idClient>client.idClient) r->st = inserareAVL(r->st,
client, err);
else
*err = 1;
}
else{
r = (NodAVL*)malloc(sizeof(NodAVL));
r->c = client;
r->dr = 0; r->st = 0;
}
calculGE(r);
if (r->GE == 2)
{
if (r->dr->GE == -1)
r = rotireDrSt(r, r->dr);
else
r = rotireSimplaSt(r, r->dr);
}
else if (r->GE == -2){
if (r->st->GE == 1)
r = rotireStDr(r, r->st);
else
r = rotireSimplaDr(r, r->st);
}
return r;
}
void afisareClient(Client c){
printf("\n%d %s %d %d %d", c.idClient, c.numeClient, c.bugetClient,
c.durataProceduri, c.costPRoceduri);
}
void afisarePreordine(NodAVL* r){
if (r){
afisareClient(r->c);
afisarePreordine(r->st);
afisarePreordine(r->dr);
}
}
void Suma(NodAVL* r){
if (r){
printf("\n %d suma=%d", r->c.idClient, r->c.bugetClient - r>c.costPRoceduri);
Suma(r->st);
Suma(r->dr);

}
}
Nod* inserareLista(Nod* prim, Client c){
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->c = c;
nou->next = 0;
if (!prim)
return nou;
else
{
Nod* temp = prim;
while (temp->next) temp = temp->next;
temp->next = nou;
return prim;
}
}
void afisareLista(Nod* prim){
Nod* temp = prim;
while (temp){
afisareClient(temp->c);
temp = temp->next;
}
}
void creareLista(NodAVL* r, Nod** prim){
if (r){
if (!r->dr&&!r->st)
*prim = inserareLista(*prim, r->c);
else
{
creareLista(r->st, prim);
creareLista(r->dr, prim);
}
}
}
int nrDatori(NodAVL* r){
if (r){
if (r->c.bugetClient - r->c.costPRoceduri < 0)
return 1 + nrDatori(r->st) + nrDatori(r->dr);
else return nrDatori(r->st) + nrDatori(r->dr);
}
else return 0;
}
void main(){
FILE* f = fopen("Text.txt", "r");
Client c;
NodAVL* root = 0;
while (!feof(f)){
char buffer[128];
char numeBuf[40];
fgets(buffer, sizeof(buffer), f);
sscanf(buffer, "%d %s %d %d %d", &c.idClient, numeBuf, &c.bugetClient,
&c.durataProceduri, &c.costPRoceduri);
c.numeClient = (char*)malloc((strlen(numeBuf) + 1)*sizeof(char));
strcpy(c.numeClient, numeBuf);

int err = 0;
root = inserareAVL(root, c, &err);

if (err)
printf("\nId-ul %d exista deja", c.idClient);
else
printf("\nId'ul %d a fost adaugat", c.idClient);
}
afisarePreordine(root);
printf("\n");
Suma(root);
Nod* prim = 0;
creareLista(root, &prim);
printf("\n");
afisareLista(prim);
int s = 0;
s = nrDatori(root);
printf("\nnumar datori=%d", s);
}

Liste Duble
#include <stdio.h>
#include <string.h>
#include <malloc.h>
struct Angajat{
unsigned int id;
char* nume;
char* pozitie;
float salariu;
};

//doar pozitiv

struct Nod{
Angajat ang;
Nod* next; //adresa catre urmatorul nod
Nod* prev; //adresa catre nodul precedent
};

Nod* inserare(Nod* prim, Angajat a)


{
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->ang = a;
nou->next = 0; nou->prev = 0;
if (!prim)
return nou;
else
{
Nod*t = prim;
while (t->next)
t = t->next;
t->next = nou;
nou->prev = t;
return prim;
}
}
Nod* inserareMijl(Nod* prim, Angajat a)
{
Nod* nou = (Nod*)malloc(sizeof(Nod));

nou->ang = a;
nou->next = 0; nou->prev = 0;
if (!prim)
return nou;
else if (prim->ang.id > a.id)
{
nou->next = prim;
prim->prev = nou;
return nou;
}
else
{
Nod *t = prim;
while (t->next && t->next->ang.id < a.id)
t = t->next;
nou->next = t->next;
if (nou->next)
{
Nod* x = nou->next;
x->prev = nou;
}
t->next = nou;
nou->prev = t;
return prim;
}
}
Nod* ins(Nod* prim, Angajat a)
{
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->ang = a;
nou->next = 0;
nou->prev = 0;
if (!prim)
return nou;
else if (prim->ang.id > a.id)
{
nou->next = prim;
prim->prev = nou;
return nou;
}
else
{
Nod* t = prim;
while (t->next && t->next->ang.id < a.id)
t = t->next;
if (t->next)
{
Nod* tNext = t->next;
t->next = nou;
nou->next = tNext;
nou->prev = t;
tNext->prev = nou;
}
else
{
t->next = nou;
nou->prev = t;
}
return prim;
}
}

void Afisare(Nod* p)
{
Nod*tmp = p;
while (tmp)
{
printf("\nId= %d, Nume= %s, Pozitie=%s, Salariu=%4.2f", tmp->ang.id, tmp>ang.nume, tmp->ang.pozitie, tmp->ang.salariu);
tmp = tmp->next;
}
printf("\n\n");
}
void AfisareInv(Nod*p)
{
Nod *tmp = p;
while (tmp->next)
{
tmp = tmp->next;
}
while (tmp)
{
printf("\nId= %d, Nume= %s, Pozitie=%s, Salariu=%4.2f", tmp->ang.id, tmp>ang.nume, tmp->ang.pozitie, tmp->ang.salariu);
tmp = tmp->prev;
}
}
void main()
{
Angajat a;
Nod* prim = 0;
FILE *f;
f = fopen("Angajati.txt", "r");
char buffer[128];
while (fgets(buffer, sizeof(buffer), f)) {
char pozBuf[20], numeBuf[50];
sscanf(buffer, "%d %f %[^;]; %[^\n]", &a.id, &a.salariu, pozBuf,
numeBuf);
a.pozitie = (char*)malloc((strlen(pozBuf) + 1) * sizeof(char));
strcpy(a.pozitie, pozBuf);
a.nume = (char*)malloc((strlen(numeBuf) + 1) * sizeof(char));
strcpy(a.nume, numeBuf);
// inserare Angajat in lista simpla
prim = ins(prim, a);
}
Afisare(prim);
}

GRAFURI
#include <stdio.h>
#include<malloc.h>
#include<string.h>
struct NodLS{
int idNodAdiacent;
NodLS* next;

};
struct NodLP{
int idVarf;
NodLS* primListaVecini;
NodLP* next;
};
//stiva/coada
struct Nod{
unsigned int idVarf;
Nod* next;
};

NodLP* inserareNodLP(NodLP* prim, int id, NodLS* primLS){


NodLP* nou = (NodLP*)malloc(sizeof(NodLP));
nou->idVarf = id;
nou->next = 0;
nou->primListaVecini = primLS;
if (!prim) return nou;
else{
NodLP* temp = prim;
while (temp->next) temp = temp->next;
temp->next = nou;
}
return prim;
}
NodLS* inserareNodLS(NodLS* primLS, int id){
NodLS* nou = (NodLS*)malloc(sizeof(NodLS));
nou->idNodAdiacent = id;
nou->next = 0;
if (!primLS) return nou;
else{
NodLS* temp = primLS;
while (temp->next)
temp = temp->next;
temp->next = nou;
}
return primLS;
}
NodLP* cautaNodLP(NodLP* primLP, int id){
NodLP* temp = primLP;
while (temp&&temp->idVarf != id){
temp = temp->next;
}
return temp;
}

Nod* push(Nod* prim, int id){


Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->idVarf = id;
nou->next = 0;
if (!prim) return nou;
else nou->next = prim;

return nou;
}
Nod* pop(Nod* prim, int *id){
Nod* temp;
temp = prim;
*id = prim->idVarf;
prim = prim->next;
free(temp);
return prim;
}
int* traversareDF(NodLP* primLP, int start, int nrVarfuri){
int *flags, *output, k = 0;
Nod* stack = 0;
//alocare flags
flags = (int*)malloc(nrVarfuri*sizeof(int));
//initializare flags
for (int i = 0; i < nrVarfuri; i++)
flags[i] = 0;
//vector iesire
output = (int*)malloc(nrVarfuri*sizeof(int));
stack = push(stack, start);
flags[start - 1] = 1;
while (stack)
{
int varf;
stack = pop(stack, &varf);
output[k] = varf;
k++;
//cautare vecini nod
NodLP* curentLP = cautaNodLP(primLP, varf);
NodLS* veciniCurentLP = curentLP->primListaVecini;
while (veciniCurentLP){
//adaug vecini in stack
if (flags[veciniCurentLP->idNodAdiacent - 1] == 0)
{
stack = push(stack, veciniCurentLP->idNodAdiacent);
flags[veciniCurentLP->idNodAdiacent - 1] = 1;
}
veciniCurentLP = veciniCurentLP->next;
}
}
return output;
}
Nod* put(Nod* prim, int id){
Nod* temp = prim;
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->idVarf = id;
nou->next = 0;
if (!prim) return nou;
else{
while (temp->next)
temp = temp->next;
temp->next = nou;
}
return prim;
}
Nod* get(Nod* prim, int* id){
Nod* temp = prim;

*id = prim->idVarf;
prim = prim->next;
free(temp);
return prim;
}
int* traversareBF(NodLP* primLP, int start, int nrVarfuri){
int *flags, *output, k = 0;
Nod* queue = 0;
//alocare vect flags
flags = (int*)malloc(nrVarfuri*sizeof(int));
for (int i = 0; i < nrVarfuri; i++)
flags[i] = 0;
output = (int*)malloc(nrVarfuri*sizeof(int));
queue = put(queue, start);
flags[start - 1] = 1;
while (queue){
int varf;
queue = get(queue, &varf);
output[k] = varf;
k++;
//cautare vecini
NodLP* curentLP = cautaNodLP(primLP, varf);
NodLS* veciniCurentLP = curentLP->primListaVecini;
while (veciniCurentLP){
//adaug vecini in stak
if (flags[veciniCurentLP->idNodAdiacent - 1] == 0){
queue = put(queue, veciniCurentLP->idNodAdiacent);
flags[veciniCurentLP->idNodAdiacent - 1] = 1;
}
veciniCurentLP = veciniCurentLP->next;
}
}
return output;
}
void afisare(NodLP* prim){
NodLP* tempLP = prim;
while (tempLP){
printf("\n Varful %d, vecini: ", tempLP->idVarf);
NodLS* tempLS = tempLP->primListaVecini;
while (tempLS){
printf(" %d ", tempLS->idNodAdiacent);
tempLS = tempLS->next;
}
tempLP = tempLP->next;
}
}
void main(){
FILE *f;
f = fopen("graf.txt", "r");
char buffer[128];
fgets(buffer, sizeof(buffer), f);
NodLP *pLista = 0;
unsigned int nrVarfuri = 0;
sscanf(buffer, "%d", &nrVarfuri);
for (int i = 1; i <= nrVarfuri; i++)

pLista = inserareNodLP(pLista, i, NULL);


while (fgets(buffer, sizeof(buffer), f)){
int sursa, dest;
sscanf(buffer, "%d %d", &sursa, &dest);
NodLP* tempLP = cautaNodLP(pLista, sursa);
if (tempLP){
tempLP->primListaVecini = inserareNodLS(tempLP->primListaVecini,
dest);
}
else{
printf("u exista ndoul cu id %d ", sursa);
}
}
afisare(pLista);
int *vectorDf = (int*)malloc(nrVarfuri*sizeof(int));
vectorDf = traversareDF(pLista, 1, nrVarfuri);
printf("\nDF:");
for (int i = 0; i < nrVarfuri; i++){
printf(" %d ", vectorDf[i]);
}
int *vectorBf = (int*)malloc(nrVarfuri*sizeof(int));
vectorBf = traversareBF(pLista, 1, nrVarfuri);
printf("\nBF:");
for (int i = 0; i < nrVarfuri; i++)
{
printf(" %d ", vectorBf[i]);
}
}

Problema cu GRAF:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
enum Tip{
GRAU, ORZ, SECARA, PORUMB, OREZ
};
struct Depozit{
char* idDepozit;
unsigned int capacitate;
unsigned int incarcare;
char* tipCereale;
};
struct NodLS{
char* idDep;
NodLS* next;
};
struct NodLP{
Depozit dep;
NodLP* next;
NodLS* primListaVecini;
};
NodLP* inserareLP(NodLP* prim, Depozit d, NodLS* listaVecini){
NodLP* nou = (NodLP*)malloc(sizeof(NodLP));
nou->dep = d;

nou->primListaVecini = listaVecini;
nou->next = 0;
if (!prim)
return nou;
else{
NodLP* t = prim;
while (t->next) t = t->next;
t->next = nou;
return prim;
}
}
NodLP* cautaNodLP(NodLP* prim, char* id){
NodLP* t = prim;
while (t&&strcmp(t->dep.idDepozit, id) != 0)
t = t->next;
return t;
}
NodLS* inserareLS(NodLS* prim, char* id){
NodLS* nou = (NodLS*)malloc(sizeof(NodLS));
nou->idDep = id;
nou->next = 0;
if (!prim) return nou;
else{
NodLS* temp = prim;
while (temp->next)
temp = temp->next;
temp->next = nou;
return prim;
}
}
NodLP* transfer(NodLP* prim, char* id){
NodLP* temp = prim;
NodLP* destinatie = cautaNodLP(prim, id);
while (temp){
if (strcmp(temp->dep.tipCereale, destinatie->dep.tipCereale) == 0 &&
strcmp(temp->dep.idDepozit, id) != 0){
int val1 = 0.2*temp->dep.incarcare;
if (val1 + destinatie->dep.incarcare <= destinatie>dep.capacitate){
temp->dep.incarcare -= val1;
destinatie->dep.incarcare += val1;
}
else{
int val2 = destinatie->dep.capacitate - destinatie>dep.incarcare;
temp->dep.incarcare -= val2;
destinatie->dep.incarcare += val2;
}
}
temp = temp->next;
}
return prim;
}
void afisare(NodLP* primLP){
NodLP* t = primLP;
while (t){
printf("\n %s %d %d %s \n
Vecinii lui:", t->dep.idDepozit, t>dep.capacitate, t->dep.incarcare, t->dep.tipCereale);
NodLS *tls = t->primListaVecini;
while (tls){
printf(" %s ", tls->idDep);

tls = tls->next;
}
t = t->next;
}
}
void main(){
FILE* f = fopen("Text.txt", "r");
Depozit d;
NodLP *primLP = 0;
int nrVarfuri;
fscanf(f, "%d", &nrVarfuri);
for (int i = 1; i <= nrVarfuri; i++){
char idBuf[40], tipCer[40];
fscanf(f, "%s %d %d %s", idBuf, &d.capacitate, &d.incarcare, tipCer);
d.idDepozit = (char*)malloc((strlen(idBuf) + 1)*sizeof(char));
strcpy(d.idDepozit, idBuf);
d.tipCereale = (char*)malloc((strlen(tipCer) + 1)*sizeof(char));
strcpy(d.tipCereale, tipCer);
primLP = inserareLP(primLP, d, NULL);
}
while (!feof(f)){
char sursa[40], dest[40];
fscanf(f, "%s %s", sursa, dest);
NodLP* curent = cautaNodLP(primLP, sursa);
if (!curent)
printf("\n Nodul cu id-ul %s nu exista in graf.", sursa);
else
curent->primListaVecini = inserareLS(curent->primListaVecini,
dest);
}
afisare(primLP);
printf("\n\nNoul depozit:");
Depozit dep;
char idBuf[40], tipBuf[40];
printf("id=");
scanf("%s", idBuf);
dep.idDepozit = (char*)malloc((strlen(idBuf) + 1)*sizeof(char));
strcpy(dep.idDepozit, idBuf);
printf("capacitate=");
scanf("%d", &dep.capacitate);
printf("incarcare=");
scanf("%d", &dep.incarcare);
printf("tip cereale=");
scanf("%s", tipBuf);
dep.tipCereale = (char*)malloc((strlen(tipBuf) + 1)*sizeof(char));
strcpy(dep.tipCereale, tipBuf);
printf("\n");
NodLS* pLS = 0;
printf("vecin=");
char vecin[40];
scanf("%s", vecin);
while (!feof(stdin)){
pLS = inserareLS(pLS, vecin);
printf("vecin=");
scanf("%s", vecin);
}

primLP = inserareLP(primLP, dep, pLS);


afisare(primLP);
printf("\n\n");
primLP = transfer(primLP, idBuf);
afisare(primLP);
}

MIN/MAX HEAP
#include <stdio.h>
#include <malloc.h>
struct Heap{
int* vector;
int dimensiune;
};
//filtrarea max heap9
void filtrareMaxHeap(Heap &h, int poz){
int pozMax = poz;
int pozSt = 2 * poz + 1;
int pozDr = 2 * poz + 2;
if (pozSt<h.dimensiune&&h.vector[pozSt]>h.vector[pozMax])
pozMax = pozSt;
if (pozDr<h.dimensiune&&h.vector[pozDr]>h.vector[pozMax])
pozMax = pozDr;
if (poz != pozMax){
int aux = h.vector[pozMax];
h.vector[pozMax] = aux;
if (pozMax <= (h.dimensiune - 1) / 2)
filtrareMaxHeap(h, pozMax);
}
}
//filtrare min heap
void filtrareMinHeap(Heap &h, int poz){
int pozMin = poz;
int pozSt = 2 * poz + 1;
int pozDr = 2 * poz + 2;
if (pozSt < h.dimensiune&&h.vector[pozSt] < h.vector[pozMin])
pozMin = pozSt;
if (pozSt < h.dimensiune&&h.vector[pozDr] < h.vector[pozMin])
pozMin = pozDr;
if (poz != pozMin){
int aux = h.vector[poz];
h.vector[poz] = aux;
h.vector[pozMin] = aux;
if (pozMin <= (h.dimensiune - 1) / 2)
filtrareMinHeap(h, pozMin);
}
}
//inserare nod
void inserare(Heap &h, int x){

int *v = (int*)malloc((h.dimensiune + 1)*sizeof(int));


for (int i = 0; i < h.dimensiune; i++)
v[i] = h.vector[i];
v[h.dimensiune] = x;
h.dimensiune++;
free(h.vector);
h.vector = (int*)malloc(h.dimensiune*sizeof(int));
for (int i = 0; i < h.dimensiune; i++)
h.vector[i] = v[i];
free(v);
//se filtreaza din nou arborele
for (int i = (h.dimensiune - 1) / 2; i >= 0; i--)
filtrareMaxHeap(h, i);
//filtrareMinHeap(h,i);
}
//extragere nod
int extragere(Heap &h){
int *v = (int*)malloc((h.dimensiune - 1)*sizeof(int));
int element = h.vector[0];
//se invers primul cuult nod
int aux = h.vector[0];
h.vector[0] = h.vector[h.dimensiune - 1];
h.vector[h.dimensiune - 1] = aux;
for (int i = 0; i < h.dimensiune - 1; i++)
v[i] = h.vector[i];
h.dimensiune--;
free(h.vector);
h.vector = v;
//se filtreaza din nou:
for (int i = (h.dimensiune - 1) / 2; i >= 0; i--)
filtrareMaxHeap(h, i);
//filtrareMINHeap(h, i);
return element;
}
void afisare(Heap h){
for (int i = 0; i < h.dimensiune; i++)
printf("%d ", h.vector[i]);
printf("\n");
}
void main(){
Heap h;
FILE *f = fopen("heap.txt", "r");
fscanf(f, "%d", &h.dimensiune);
h.vector = (int*)malloc(h.dimensiune*sizeof(int));
for (int i = 0; i < h.dimensiune; i++)
fscanf(f, "%d", &h.vector[i]);
afisare(h);
for (int i = (h.dimensiune - 1) / 2; i >= 0; i--)
filtrareMaxHeap(h, i);
afisare(h);
inserare(h, 17);
afisare(h);
int element = extragere(h);
afisare(h);

printf("\n%d", element);
}

Aplicatie min/max heap

#include <stdio.h>
#include <string.h>
#include <malloc.h>
enum CanalTransmitere
{
apelTelefonic,
aplicatieMobila
};
enum Status
{
confirmata,
asteptare
};
struct Rezervari
{
unsigned int idRezervare;
char* numeClient;
char* telefon;
CanalTransmitere canal;
unsigned int durata;
Status status;
};
struct Heap
{
Rezervari* vector;
int dim;
};
struct coada
{
Rezervari r;
coada*next;
};
coada*put(coada*p, Rezervari r)
{
coada* nou = (coada*)malloc(sizeof(coada));
nou->r = r;
nou->next = NULL;
if (!p)
{
p = nou;
}
else
{
coada*temp = p;
while (temp->next)
{
temp = temp->next;

}
temp->next = nou;
}
return p;
}
void afisareCoada(coada*p)
{
coada*temp = p;
while (temp)
{
printf("%d,%s,%s,%d,%d,%d\n", temp->r.idRezervare, temp->r.numeClient,
temp->r.telefon, temp->r.canal, temp->r.durata, temp->r.status);
temp = temp->next;
}
}
void filtrareMaxHeap(Heap&h, int poz)
{
int pozMax = poz;
int pozSt = 2 * poz + 1;
int pozDr = 2 * poz + 2;
if (pozSt<h.dim
pozMax =
if (pozDr<h.dim
pozMax =

&& h.vector[pozSt].durata>h.vector[pozMax].durata)
pozSt;
&& h.vector[pozDr].durata>h.vector[pozMax].durata)
pozDr;

if (pozMax != poz)
{
Rezervari aux = h.vector[pozMax];
h.vector[pozMax] = h.vector[poz];
h.vector[poz] = aux;
if (pozMax <= (h.dim - 1) / 2)
filtrareMaxHeap(h, pozMax);
}
}
void inserareHeap(Heap&h, Rezervari r)
{
Rezervari* v = (Rezervari*)malloc((h.dim + 1)*sizeof(Rezervari));
for (int i = 0; i < h.dim; i++)
{
v[i].canal = h.vector[i].canal;
v[i].durata = h.vector[i].durata;
v[i].idRezervare = h.vector[i].idRezervare;
v[i].numeClient = strdup(h.vector[i].numeClient);
v[i].status = h.vector[i].status;
v[i].telefon = strdup(h.vector[i].telefon);
}
v[h.dim] = r;
if (h.vector)
{
for (int i = 0; i < h.dim; i++)
{
free(h.vector[i].numeClient);
free(h.vector[i].telefon);
}
free(h.vector);
}
h.dim++;
h.vector = (Rezervari*)malloc(h.dim*sizeof(Rezervari));

for (int i = 0; i < h.dim; i++)


{
h.vector[i].canal = v[i].canal;
h.vector[i].durata = v[i].durata;
h.vector[i].idRezervare = v[i].idRezervare;
h.vector[i].numeClient = strdup(v[i].numeClient);
h.vector[i].status =v[i].status ;
h.vector[i].telefon = strdup(v[i].telefon);
}
free(v);
for (int i = (h.dim - 1) / 2; i >= 0; i--)
filtrareMaxHeap(h, i);
}
void afisareHeap(Heap&h)
{
for (int i = 0; i < h.dim; i++)
{
printf("%d,%s,%s,%d,%d,%d\n", h.vector[i].idRezervare,
h.vector[i].numeClient, h.vector[i].telefon, h.vector[i].canal, h.vector[i].durata,
h.vector[i].status);
}
}
Rezervari extrage(Heap&h)
{
Rezervari element;
element.canal = h.vector[0].canal;
element.durata = h.vector[0].durata;
element.idRezervare = h.vector[0].idRezervare;
element.numeClient = strdup(h.vector[0].numeClient);
element.status = h.vector[0].status;
element.telefon = strdup(h.vector[0].telefon);
if (h.dim > 1)
{
Rezervari* v = (Rezervari*)malloc((h.dim - 1)*sizeof(Rezervari));
for (int i = 1; i < h.dim - 1; i++)
{
v[i].canal = h.vector[i].canal;
v[i].durata = h.vector[i].durata;
v[i].idRezervare = h.vector[i].idRezervare;
v[i].numeClient = strdup(h.vector[i].numeClient);
v[i].status = h.vector[i].status;
v[i].telefon = strdup(h.vector[i].telefon);
}
v[0].canal = h.vector[h.dim - 1].canal;
v[0].durata = h.vector[h.dim - 1].durata;
v[0].idRezervare = h.vector[h.dim - 1].idRezervare;
v[0].numeClient = strdup(h.vector[h.dim - 1].numeClient);
v[0].status = h.vector[h.dim - 1].status;
v[0].telefon = strdup(h.vector[h.dim - 1].telefon);
if (h.vector)
{
for (int i = 0; i < h.dim; i++)
{
free(h.vector[i].numeClient);
free(h.vector[i].telefon);
}
free(h.vector);
}

h.dim--;
h.vector = (Rezervari*)malloc(h.dim*sizeof(Rezervari));
for (int i = 0; i < h.dim; i++)
{
h.vector[i].canal = v[i].canal;
h.vector[i].durata = v[i].durata;
h.vector[i].idRezervare = v[i].idRezervare;
h.vector[i].numeClient = strdup(v[i].numeClient);
h.vector[i].status = v[i].status;
h.vector[i].telefon = strdup(v[i].telefon);
}
free(v);
for (int i = (h.dim - 1) / 2; i >= 0; i--)
filtrareMaxHeap(h, i);
}
else
{
h.dim--;
free(h.vector);
}
return element;
}
coada* confirmare(Heap &h,coada*p)
{
p= put(p, extrage(h));
return p;
}
void salvareFisier(Heap h, FILE*f)
{
Heap v;
v.vector = (Rezervari*)malloc(h.dim*sizeof(Rezervari));
for (int i = 0; i < h.dim; i++)
{
v.vector[i].canal = h.vector[i].canal;
v.vector[i].durata = h.vector[i].durata;
v.vector[i].idRezervare = h.vector[i].idRezervare;
v.vector[i].numeClient = strdup(h.vector[i].numeClient);
v.vector[i].status = h.vector[i].status;
v.vector[i].telefon = strdup(h.vector[i].telefon);
}
v.dim = h.dim;
Rezervari aux;
do
{
aux = extrage(v);
fprintf(f,"%d,%d,%s\n", aux.idRezervare, aux.durata, aux.numeClient);
} while (v.dim >= 1);
}
void raport2(coada*p, FILE*f)
{
coada*temp = p;
while (temp)
{
fprintf(f,"%d,%s,%s,%d,%d,%d\n", temp->r.idRezervare, temp->r.numeClient,
temp->r.telefon, temp->r.canal, temp->r.durata, temp->r.status);
temp = temp->next;
}
}
void main()

{
FILE* f = fopen("Rezervari.txt", "r");
Heap h;
h.dim = 0;
h.vector = NULL;
char buffer[128];
while (fgets(buffer, sizeof(buffer), f))
{
char numebuf[20];
char telefonbuf[12];
int status, canal;
Rezervari r;
sscanf(buffer, "%d,%[^,],%[^,],%d,%d,%d", &r.idRezervare, numebuf,
telefonbuf, &canal, &r.durata, &status);
r.numeClient = strdup(numebuf);
r.telefon = strdup(telefonbuf);
if (status < 2 && canal < 2)
{
r.status = Status(status);
r.canal = CanalTransmitere(canal);
/*printf("%d,%s,%s,%d,%d,%d\n", r.idRezervare, r.numeClient,
r.telefon, r.canal, r.durata, r.status);*/
inserareHeap(h, r);
}
}
//extrage(h);
afisareHeap(h);
fclose(f);
f = fopen("Output.txt", "w");
salvareFisier(h, f);
printf("\n\n\n");
coada*p = NULL;
p = confirmare(h, p);
p = confirmare(h, p);
afisareCoada(p);
printf("\n\n\n");
afisareHeap(h);
fclose(f);
f = fopen("Output2.txt", "w");
raport2(p, f);
}

GRAF cu matrice de adiacenta


#include <stdio.h>
#include <malloc.h>
struct Nod{
int id;
Nod* next;
};
Nod* push(Nod* prim, int i){
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->id = i;
nou->next = prim;
return nou;
}
Nod* pop(Nod* prim, int* i){

Nod* temp = prim;


*i = prim->id;
prim = prim->next;
free(temp);
return prim;
}
int* parcurgereDF(int** matr, int start, int nrVf){
int* output, *flags, k = 0;
Nod* stack = 0;
output = (int*)malloc(nrVf*sizeof(int));
flags = (int*)malloc(nrVf*sizeof(int));
for (int i = 0; i < nrVf; i++)
flags[i] = 0;
stack = push(stack, start);
flags[start - 1] = 1;
while (stack){
int varf;
stack = pop(stack, &varf);
output[k] = varf;
k++;
for (int i = 1; i <= nrVf;i++)
if (matr[varf][i] == 1 && flags[i - 1] == 0){
stack = push(stack, i);
flags[i - 1] = 1;
}
}
return output;
}
Nod* put(Nod* prim, int id){
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->id = id;
nou->next = 0;
if (!prim)
return nou;
else{
Nod* temp = prim;
while (temp->next)
temp = temp->next;
temp->next = nou;
return prim;
}
}
Nod* get(Nod* prim, int *i){
Nod* temp = prim;
*i = prim->id;
prim = prim->next;
free(temp);
return prim;
}
int* parcurgereBF(int** matr, int start, int nrVarfuri){
int* output, *flags, k = 0;
Nod* queue=0;
output = (int*)malloc(nrVarfuri*sizeof(int));
flags = (int*)malloc(nrVarfuri*sizeof(int));
for (int i = 0; i < nrVarfuri; i++)
flags[i] = 0;
queue = put(queue, start);

flags[start - 1] = 1;
while (queue){
int varf;
queue = get(queue, &varf);
output[k] = varf;
k++;
for (int i = 1; i <= nrVarfuri;i++)
if (matr[varf][i] == 1 && flags[i - 1] == 0){
queue = put(queue, i);
flags[i - 1] = 1;
}
}
return output;
}
void main(){
FILE* f = fopen("Text.txt", "r");
int nrVarfuri;
fscanf(f, "%d", &nrVarfuri);
int** matr;
matr = (int**)malloc(nrVarfuri*sizeof(int*));
for (int i = 1; i <= nrVarfuri; i++)
matr[i] = (int*)malloc(nrVarfuri*sizeof(int));
for (int i = 1; i <= nrVarfuri;i++)
for (int j = 1; j <= nrVarfuri; j++)
matr[i][j] = 0;
while (!feof(f)){
int sursa, dest;
fscanf(f, "%d %d", &sursa, &dest);
if (sursa <= nrVarfuri && dest <= nrVarfuri)
matr[sursa][dest] = 1;
}
int* outputDF;
outputDF = parcurgereDF(matr, 1, nrVarfuri);
printf("\nDF:");
for (int i = 0; i < nrVarfuri; i++)
printf(" %d", outputDF[i]);
int* outputBF;
outputBF= parcurgereBF(matr, 1, nrVarfuri);
printf("\nBF:");
for (int i = 0; i < nrVarfuri; i++)
printf(" %d", outputBF[i]);
}

STIVA
#include <stdio.h>
#include <malloc.h>
struct Pair{
int inf, sup;
};

struct Nod
{
Pair item;
Nod*next;
};
Nod* push(Nod* prim, Pair p)
//introducere la inceput
{
Nod* nou = (Nod*)malloc(sizeof(Nod));
nou->item = p;
nou->next = prim;
return nou;
}
Nod* pop(Nod* prim, Pair *p)
{
Nod* temp = prim;
*p = prim->item;
prim = prim->next;
free(temp);
return prim;
}

//extragere de la inceput

void Quicksort(float *v, Pair p)


{
Nod* stack = 0;
stack = push(stack, p);
int i, j, piv, flagInterschimb;
while (stack)
{
Pair lim_sub; //limitele subintervalului;
stack = pop(stack, &lim_sub);
i = lim_sub.inf - 1;
j = lim_sub.sup - 1;
piv = i;
flagInterschimb = 0; //parsare de la dreapta la stanga
//flagInterschimb=1 //parsare de la stanga la dreapta
while (i < j)
{
if (!flagInterschimb)
//parsare dreapta-stanga
{
while (j>piv && v[piv] < v[j])
j--;
if (v[j] < v[piv])
{
float aux;
aux = v[j];
v[j] = v[piv];
v[piv] = aux;
piv = j;
flagInterschimb = 1;
}
}
else //parsare stanga-dreapta
{
while (i<piv && v[piv]>v[i])
i++;
if (v[piv] < v[i])
{
float aux;

aux = v[i];
v[i] = v[piv];
v[piv] = aux;
piv = i;
flagInterschimb = 0;
}
}
}
Pair aux;
int ok = 0;
if (piv == lim_sub.inf - 1 || piv == lim_sub.inf)
{
ok = 1;
}
else if (piv == lim_sub.sup - 1 || piv == lim_sub.sup - 2)
{
ok == 2;
}
if (lim_sub.inf < lim_sub.sup)
{
if (ok == 1)
{
aux.sup = lim_sub.sup;
aux.inf = piv + 2;
stack = push(stack, aux);
}
else if (ok ==
{
aux.inf
aux.sup
stack =
}
else
{
aux.inf
aux.sup
stack =
aux.inf
aux.sup
stack =
}

2)
= lim_sub.inf;
= piv;
push(stack, aux);

= piv + 2;
= lim_sub.sup;
push(stack, aux);
= lim_sub.inf;
= piv;
push(stack, aux);

}
}
void main()
{
FILE *f = fopen("Vector.txt", "r");
float *vector, val;
int size = 0, i;
fscanf(f, "%f", &val);
while (!feof(f))
{
size++;
fscanf(f, "%f", &val);
}

vector = (float*)malloc(size*sizeof(float));
fseek(f, 0, 0);
for (int i = 0; i < size; i++)
{
fscanf(f, "%f", &val);
vector[i] = val;
}
for (int i = 0; i < size; i++)
printf("%4.2f ", vector[i]);
printf("\n\n");
Pair limite;
limite.inf = 1;
limite.sup = size;
Quicksort(vector, limite);
for (int i = 0; i < size; i++)
printf("%4.2f ", vector[i]);
printf("\n\n");
fclose(f);
}

You might also like