Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.07 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<string.h>
  5.  
  6. typedef struct elev
  7. {
  8. char nume[20];
  9. float nota;
  10. struct elev *stang;
  11. struct elev *drept;
  12. }nod;
  13.  
  14. nod *root = NULL;
  15.  
  16. nod *adaugare(nod *t, float nota, char nume[])
  17. {
  18. if (t == NULL)
  19. if ((t = (nod*)malloc(sizeof(nod))) == NULL)
  20. {
  21. printf("EROARE de alocare!");
  22. }
  23. else
  24. {
  25. t->nota = nota;
  26. strcpy(t->nume, nume);
  27. t->stang = t->drept = NULL;
  28. }
  29. else
  30.  
  31. if (strcmp(t->nume, nume)<0)
  32. t->drept = adaugare(t->drept, nota, nume);
  33.  
  34. else
  35. if (strcmp(t->nume, nume)>0)
  36. t->stang = adaugare(t->stang, nota, nume);
  37. else
  38. printf("Nodul nu a fsot gasit");
  39. return t;
  40. }
  41. void citire()
  42. {
  43. FILE *f;
  44. char nume[20];
  45. float nota;
  46.  
  47. if ((f = fopen("Elev.txt", "rt")) == NULL)
  48.  
  49. printf("Eroare la deschiderea fisierului");
  50. else
  51.  
  52. while (!feof(f))
  53. {
  54. fscanf(f, "%s %f", nume, &nota);
  55. root = adaugare(root, nota, nume);
  56. }
  57. fclose(f);
  58. }
  59.  
  60.  
  61. void afisare(nod *t)
  62. {
  63. if (t != NULL)
  64. {
  65. afisare(t->stang);
  66. printf("\n %s %.2f", t->nume, t->nota);
  67. afisare(t->drept);
  68. }
  69. }
  70.  
  71. nod *supred(nod *t, nod *p) {
  72. nod *q, *q_aux;
  73. q = t;
  74. if (q->drept != NULL)
  75. q->drept = supred(q->drept, p);
  76. else {
  77. strcpy(p->nume, q->nume);
  78. p->nota = q->nota;
  79. q_aux = q;
  80. q = q->stang;
  81. free(q_aux);
  82. }
  83. return q;
  84. }
  85.  
  86. nod * elimina(nod *p, char v[]) {
  87. nod *q, *q1;
  88. char *s;
  89. if (p == NULL)
  90. printf("Eroare: %d nu apare in evidenta \n", v);
  91. else
  92. if (strcmp(p->nume, v)<0)
  93. p->drept = elimina(p->drept, v);
  94. else
  95. if (strcmp(p->nume, v)>0)
  96. p->stang = elimina(p->stang, v);
  97. else
  98. if (p->stang == NULL) {
  99. q = p->drept;
  100. free(p);
  101. return q;
  102. }
  103. else
  104. if (p->drept == NULL) {
  105. q = p->stang;
  106. free(p);
  107. return q;
  108. }
  109. else
  110. p->stang = supred(p->stang, p);
  111. return p;
  112. }
  113.  
  114.  
  115.  
  116. int main()
  117. {
  118. int opt;
  119. char x[10];
  120. do {
  121. puts("1. Citire \n");
  122. puts("2. Afisare \n");
  123. puts("3. Stergere \n");
  124. puts("0. EXIT \n");
  125. puts("Introduceti optiunea: \n");
  126. scanf("%d", &opt);
  127.  
  128. switch (tolower(opt))
  129. {
  130. case 1:
  131. citire();
  132. break;
  133. case 2:
  134. afisare(root);
  135. break;
  136. case 3:
  137. printf("introduceti numele care va fi sters \n");
  138. scanf("%s", x);
  139. root = elimina(root, x);
  140. break;
  141. case 0:
  142. exit(EXIT_FAILURE);
  143. }
  144. } while (opt);
  145. getch();
  146. return 0;
  147. }
  148.  
  149.  
  150.  
  151.  
  152. #include<stdio.h>
  153. #include<conio.h>
  154. #include<string.h>
  155. #include<stdlib.h>
  156.  
  157. typedef enum{stanga,dreapta}mana;
  158.  
  159. typedef struct nod {
  160. char nume[100];
  161. int varsta;
  162. char tara[100];
  163. int loc;
  164. int titluri;
  165. mana man;
  166. struct nod *st;
  167. struct nod *dr;
  168. }nod;
  169.  
  170. nod *root = NULL;
  171.  
  172. nod *adauga(nod *t, char *num, int varst, char *tar, int lc, mana mn, int titl)
  173. {
  174. if (t == NULL)
  175. {
  176. t = (nod*)malloc(sizeof(nod));
  177. strcpy(t->nume, num);
  178. t->varsta = varst;
  179. strcpy(t->tara, tar);
  180. t->loc = lc;
  181. t->man = mn;
  182. t->titluri = titl;
  183. t->st = t->dr = NULL;
  184. }
  185. else if (strcmp(num, t->nume) < 0)
  186. t->st = adauga(t->st, num, varst, tar, lc, titl,mn);
  187. else if (strcmp(num, t->nume) > 0)
  188. t->dr = adauga(t->dr, num, varst, tar, lc, titl,mn);
  189. else
  190. printf("Nodul exista.");
  191. return t;
  192. }
  193.  
  194. void afisare(nod *t)
  195. {
  196. if (t != NULL)
  197. {
  198. afisare(t->st);
  199. printf("%s %d %s %d %d ", t->nume, t->varsta, t->tara, t->loc, t->titluri);
  200. if (t->man == 0)
  201. printf("stanga\n");
  202. else
  203. printf("dreapta\n");
  204. afisare(t->dr);
  205. }
  206. }
  207.  
  208. void afisare_cond(nod *t)
  209. {
  210. if (t != NULL)
  211. {
  212. afisare_cond(t->st);
  213. if (t->varsta < 30 && t->man == 1)
  214. {
  215. printf("%s %d %s %d %d ", t->nume, t->varsta, t->tara, t->loc, t->titluri);
  216. if (t->man == 0)
  217. printf("stanga\n");
  218. else
  219. printf("dreapta\n");
  220. }
  221. afisare_cond(t->dr);
  222. }
  223. }
  224.  
  225. nod *supred(nod *t, nod *p)
  226. {
  227. nod *q, *aux;
  228. q = t;
  229. if (q->dr != NULL)
  230. q->dr = supred(q->dr, p);
  231. else
  232. {
  233. strcpy(p->nume, q->nume);
  234. strcpy(p->tara, q->tara);
  235. p->varsta = q->varsta;
  236. p->loc = q->loc;
  237. p->titluri = q->titluri;
  238. p->man = q->man;
  239. aux = q;
  240. q = q->st;
  241. free(aux);
  242. }
  243. return q;
  244. }
  245.  
  246. void citire()
  247. {
  248. FILE *f;
  249. char nume[100];
  250. int varsta;
  251. char tara[100];
  252. int loc;
  253. int titluri;
  254. mana man;
  255. f = fopen("tenis.txt", "rt");
  256. if (f == NULL)
  257. printf("Nu exista.\n");
  258. else
  259. {
  260. while (!feof(f))
  261. {
  262. fscanf(f, "%s %d %s %d %d %d", nume, &varsta, tara, &loc, &titluri, &man);
  263. root = adauga(root, nume, varsta, tara, loc,titluri, man);
  264. }
  265. }
  266. }
  267.  
  268. nod *elimina(nod *t, char nume[100])
  269. {
  270. nod *aux;
  271. if (t == NULL)
  272. printf("Nodul nu exista!\n");
  273. else
  274. {
  275. if (strcmp(nume, t->nume) < 0)
  276. t->st = elimina(t->st, nume);
  277. else if (strcmp(nume, t->nume) > 0)
  278. t->dr = elimina(t->dr, nume);
  279. else
  280. {
  281. if (t->st == NULL)
  282. {
  283. aux = t;
  284. t = t->st;
  285. free(aux);
  286. }
  287. else if (t->dr == NULL)
  288. {
  289. aux = t;
  290. t = t->dr;
  291. free(aux);
  292. }
  293. else
  294. t->st = supred(t->st, t);
  295. }
  296. }
  297. return t;
  298. }
  299.  
  300. int x;
  301.  
  302. nod *stergere(nod *t, int loc)
  303. {
  304. nod *aux;
  305. if (t == NULL)
  306. x++;
  307. else if (t->loc < 19)
  308. {
  309. t->st = stergere(t->st, loc);
  310. t->dr = stergere(t->dr, loc);
  311. }
  312. else
  313. if (t->st == NULL)
  314. {
  315. aux = t;
  316. t = t->dr;
  317. free(aux);
  318. }
  319. else if (t->dr == NULL)
  320. {
  321. aux = t;
  322. t = t->st;
  323. free(aux);
  324. }
  325. else
  326. t->st = supred(t->st, t);
  327. return t;
  328. }
  329.  
  330. int main()
  331. {
  332. citire();
  333. afisare(root);
  334. printf("\n");
  335. _getch();
  336. afisare_cond(root);
  337. printf("\n");
  338. _getch();
  339. while (x < 50)
  340. root = stergere(root, 49);
  341. printf("\n");
  342. afisare(root);
  343. system("pause");
  344. return 0;
  345. }
  346.  
  347.  
  348.  
  349.  
  350.  
  351. #include <stdio.h>
  352. #include <conio.h>
  353. #include <string.h>
  354. #include <stdlib.h>
  355.  
  356. typedef enum { GDDR3, DDR3, GDDR5, HBM2 }memorie;
  357.  
  358. typedef struct nod {
  359. char nume_placa[50];
  360. memorie mem;
  361. int dim_mem;
  362. float pret;
  363. struct nod *st, *dr;
  364.  
  365. }nod;
  366.  
  367. nod *root = NULL;
  368.  
  369. nod *adaugare(nod *t, char nume[50], memorie m, int dim_mem, float pret) {
  370. if (t == NULL) {
  371. t = (nod*)malloc(sizeof(nod));
  372. strcpy(t->nume_placa, nume);
  373. t->mem = m;
  374. t->dim_mem = dim_mem;
  375. t->pret = pret;
  376. t->st = NULL;
  377. t->dr = NULL;
  378. }
  379. else
  380. if (strcmp(t->nume_placa, nume) > 0)
  381. t->st = adaugare(t->st, nume, m, dim_mem, pret);
  382. else
  383. if (strcmp(t->nume_placa, nume) < 0)
  384. t->dr = adaugare(t->dr, nume, m, dim_mem, pret);
  385. else
  386. printf("Exista in arbore.\n");
  387. return t;
  388. }
  389.  
  390. void citire_fisier() {
  391. char nume[50];
  392. char mem[50];
  393. memorie t_memorie;
  394. int dim;
  395. float pret;
  396.  
  397. FILE *f;
  398. fopen_s(&f, "Graphic_Cards.txt", "rt");
  399.  
  400. while (!feof(f)) {
  401. fscanf(f, "%s", nume);
  402. fscanf(f, "%s", mem);
  403. if (strcmp(mem, "GDDR3") == 0)
  404. t_memorie = GDDR3;
  405. else
  406. if (strcmp(mem, "DDR3") == 0)
  407. t_memorie = DDR3;
  408. else
  409. if (strcmp(mem, "GDDR5") == 0)
  410. t_memorie = GDDR5;
  411. else
  412. if (strcmp(mem, "HBM2") == 0)
  413. t_memorie = HBM2;
  414.  
  415.  
  416. fscanf(f, "%d", &dim);
  417. fscanf(f, "%f", &pret);
  418. root = adaugare(root, nume, t_memorie, dim, pret);
  419.  
  420.  
  421. }
  422. }
  423.  
  424.  
  425. nod *supred(nod *t, nod *p)
  426. {
  427. nod *q, *aux;
  428. q = t;
  429. if (q->dr != NULL)
  430. {
  431. q->dr = supred(q->dr, p);
  432. }
  433. else
  434. {
  435. strcpy(p->nume_placa, q->nume_placa);
  436. p->mem = q->mem;
  437. p->dim_mem = q->dim_mem;
  438. p->pret = q->pret;
  439. aux = q;
  440. q = q->st;
  441. free(aux);
  442. }
  443. return q;
  444.  
  445. }
  446.  
  447. int chestie;
  448.  
  449. nod* stergere_ss(nod *t, char *prenume)
  450. {
  451. nod *aux;
  452. if (t == NULL)
  453. {
  454. chestie++;
  455. }
  456. else if (strcmp(t->nume_placa, prenume) != 0)
  457. {
  458. t->st = stergere_ss(t->st, prenume);
  459. t->dr = stergere_ss(t->dr, prenume);
  460. }
  461. else
  462. if (t->st == NULL)
  463. {
  464. aux = t;
  465. t = t->dr;
  466. free(aux);
  467. }
  468. else if (t->dr == NULL)
  469. {
  470. aux = t;
  471. t = t->st;
  472. free(aux);
  473. }
  474. else
  475. {
  476. t->st = supred(t->st, t);
  477. }
  478. return t;
  479.  
  480. }
  481.  
  482. //int chestie;
  483.  
  484. nod *elimina2(nod *p, int v)
  485. {
  486. nod *q;
  487. if (p == NULL)
  488. return NULL;
  489. p->st = elimina2(p->st, v);
  490. p->dr = elimina2(p->dr, v);
  491. if (p->st == NULL && (p->dim_mem < v))
  492. {
  493. q = p->dr;
  494. free(p);
  495. return q;
  496. }
  497. else
  498. if (p->dr == NULL && (p->dim_mem < v))
  499. {
  500. q = p->st;
  501. free(p);
  502. return q;
  503. }
  504. else
  505. if (p->dim_mem < v)
  506. p->st = supred(p->st, p);
  507. return p;
  508. }
  509.  
  510.  
  511.  
  512.  
  513.  
  514. void afisare(nod *t) {
  515. if (t != NULL) {
  516. printf("\n");
  517. afisare(t->st);
  518. printf("Numele placii: %s \n Tipul memoriei : %d \n Dimensiunea memoriei: %d \n Pretul: %f \n\n", t->nume_placa, t->mem, t->dim_mem, t->pret);
  519. afisare(t->dr);
  520. }
  521. }
  522.  
  523. void afisare_criteriu(nod *t)
  524. {
  525. if (t != NULL)
  526. {
  527. printf("\n");
  528. afisare_criteriu(t->st);
  529. if (t->mem == 0 && t->pret < 300)
  530. printf("Numele placii: %s\nTipul memoriei: %d\nDimensiunea memoriei: %d\nPretul: %f\n\n", t->nume_placa, t->mem, t->dim_mem, t->pret);
  531. afisare_criteriu(t->dr);
  532. }
  533.  
  534. }
  535. int main()
  536. {
  537. int dimensiune;
  538. int opt;
  539. do {
  540. printf("1.Citire in arbore.\n");
  541. printf("2.Afisarea arborelui.\n");
  542. printf("3.Afisare criteriu.\n");
  543. printf("4.Stergere \n");
  544. printf("5.Stergere cond \n");
  545. printf("0.Exit.\n");
  546. printf("Alegeti: ");
  547. scanf("%d", &opt);
  548. switch (opt)
  549. {
  550. case 1:
  551. citire_fisier();
  552. break;
  553. case 2:
  554. afisare(root);
  555. break;
  556. case 3:
  557. afisare_criteriu(root);
  558. break;
  559. case 4: {
  560. root = stergere_ss(root, "GIGABYTE_GeForce_GT_730");
  561. root = stergere_ss(root, "GIGABYTE_GeForce_GTX_1070_Ti");
  562. afisare(root);
  563. break;
  564. }
  565. case 5: {
  566. printf("dati dimensiunea : \n");
  567. scanf("%d", &dimensiune);
  568. root = elimina2(root, dimensiune);
  569. afisare(root);
  570. break;
  571. }
  572. }
  573.  
  574. } while (opt != 0);
  575. getch();
  576. return 0;
  577. }
  578.  
  579.  
  580.  
  581.  
  582. #include<conio.h>
  583. #include<stdio.h>
  584. #include<string.h>
  585. #include<stdlib.h>
  586.  
  587.  
  588. typedef struct arbore {
  589. char nume[100];
  590. char profesie[100];
  591. float venit;
  592. struct arbore *stang, *drept;
  593.  
  594. }nod;
  595. nod *adaugare(nod *t, char *nume, char *profesie, float venit)
  596. {
  597. if (t == NULL)
  598. {
  599. t = (nod*)malloc(sizeof(nod));
  600. strcpy(t->nume, nume);
  601. strcpy(t->profesie, profesie);
  602. t->venit = venit;
  603. t->stang = t->drept = NULL;
  604. }
  605. else
  606. if (strcmp(t->nume, nume) < 0)
  607. t->stang = adaugare(t->stang, nume, profesie, venit);
  608. else
  609. if (strcmp(t->nume, nume) > 0)
  610. t->drept = adaugare(t->drept, nume, profesie, venit);
  611. else
  612. printf("Valoarea deja exista in evidenta!\n");
  613. return t;
  614. }
  615.  
  616. nod *citirefisier(nod *r)
  617. {
  618. FILE *f;
  619. char nume[100];
  620. char profesie[100];
  621. float venit;
  622. f = fopen("Text.txt", "rt");
  623. if (f == NULL)
  624. {
  625. printf("eroare la deschidere");
  626. _getch();
  627. return r;
  628. }
  629. while (!feof(f))
  630. {
  631. fscanf(f, "%s %s %f", nume, profesie, &venit);
  632. r = adaugare(r, nume, profesie, venit);
  633. }
  634. fclose(f);
  635. return r;
  636. }
  637. nod *supred(nod *t, nod *p)
  638. {
  639. nod *q, *q_aux;
  640. q = t;
  641. if (q->drept != NULL)
  642. q->drept = supred(q->drept, p);
  643. else
  644. {
  645. strcpy(p->nume, q->nume);
  646. strcpy(p->profesie, q->profesie);
  647. p->venit = q->venit;
  648. q_aux = q;
  649. q = q->stang;
  650. free(q_aux);
  651. }
  652. return q;
  653. }
  654. nod *elimina(nod *p, char *nume)
  655. {
  656. nod *q;
  657. if (p == NULL)
  658. printf("Nu apare in evidenta!");
  659. else
  660. if (strcmp(p->nume, nume) > 0)
  661. p->drept = elimina(p->drept, nume);
  662. else
  663. if (strcmp(p->nume, nume) < 0)
  664. p->stang = elimina(p->stang, nume);
  665. else
  666. if (p->stang == NULL)
  667. {
  668. q = p->drept;
  669. free(p);
  670. return q;
  671. }
  672. else
  673. if (p->drept == NULL)
  674. {
  675. q = p->stang;
  676. free(p);
  677. return q;
  678. }
  679. else
  680. p->stang = supred(p->stang, p);
  681. return p;
  682. }
  683. nod *elimina2(nod *p, int v)
  684. {
  685. nod *q;
  686. if (p == NULL)
  687. return NULL;
  688. p->stang = elimina2(p->stang, v);
  689. p->drept = elimina2(p->drept, v);
  690. if (p->stang == NULL&&(p->venit < v))
  691. {
  692. q = p->drept;
  693. free(p);
  694. return q;
  695. }
  696. else
  697. if (p->drept == NULL && (p->venit < v))
  698. {
  699. q = p->stang;
  700. free(p);
  701. return q;
  702. }
  703. else
  704. if (p->venit < v)
  705. p->stang = supred(p->stang, p);
  706. return p;
  707. }
  708. void afisare(nod *p)
  709. {
  710. if (p != NULL)
  711. {
  712. afisare(p->stang);
  713. printf("\n %s %s %f", p->nume, p->profesie, p->venit);
  714. afisare(p->drept);
  715. }
  716. }
  717.  
  718. void afisare_salariu30(nod *p)
  719. {
  720. if (p != NULL)
  721. {
  722. afisare_salariu30(p->stang);
  723. if (p->venit>30)
  724. printf("\n %s %s %f", p->nume, p->profesie, p->venit);
  725. afisare_salariu30(p->drept);
  726. }
  727. }
  728. void main()
  729. {
  730.  
  731. int opt;
  732. nod *radacina;
  733. radacina = NULL;
  734. char nume[100];
  735. int v;
  736. float venit;
  737. do {
  738. printf("\n1.citire din fisier\n");
  739. printf("2.afisare\n");
  740. printf("3.Stergere dupa nume\n");
  741. printf("4.afisare salariu mai mic decat 30\n");
  742. printf("5.elimina toti oamenii cu un salariu mai mic decat:");
  743. printf("da-ti optiunea:\n");
  744. scanf("%d", &opt);
  745. switch (opt)
  746. {
  747. case 1:
  748. radacina = citirefisier(radacina);
  749. break;
  750. case 2:
  751. afisare(radacina);
  752. break;
  753. case 3:
  754. printf("Dati numele care urmeaza sa fie sters:");
  755. scanf("%s", nume);
  756. radacina = elimina(radacina, nume);
  757. afisare(radacina);
  758. break;
  759. case 4:
  760. afisare_salariu30(radacina);
  761. break;
  762. case 5:
  763. printf("Dati valoarea salariului:");
  764. scanf("%d", &v);
  765. radacina=elimina2(radacina, v);
  766. afisare(radacina);
  767. break;
  768. }
  769. } while (opt != 0);
  770. _getch();
  771. }
  772.  
  773.  
  774.  
  775.  
  776. #include <stdio.h>
  777. #include <stdlib.h>
  778. #include <string.h>
  779. /*
  780. arbori binari ordonati test
  781.  
  782. Fisierul stud.txt contine nume_student , disciplina si nota
  783. a) incarcati informatiile din fisier intr-un arbore binar cu subliste(in arbore se incarca disciplina in subliste nume student si note) ordonat in fuctie de nota
  784. b) afisati arborele
  785. c) stergeti un student din evidenta ( se citeste de la tastatura)
  786.  
  787. */
  788.  
  789. typedef struct sublista
  790. {
  791. char nume_student[50];
  792. float nota;
  793. struct sublista *urm;
  794. }sublista;
  795.  
  796. typedef struct nod
  797. {
  798. char disciplina[50];
  799. struct nod *st;
  800. struct nod *dr;
  801. sublista *prim;
  802. }nod;
  803.  
  804. nod *root = NULL;
  805.  
  806. nod *supred(nod *t, nod *p);
  807.  
  808. sublista *adauga_nod_lista(sublista *l, char *student,float nota)
  809. {
  810. sublista *q1, *q2, *aux;
  811. aux = (sublista*)malloc(sizeof(sublista));
  812. strcpy(aux->nume_student,student);
  813. aux->nota = nota;
  814. for (q1 = q2 = l; q1 != NULL && aux->nota < q1->nota; q2 = q1, q1 = q1->urm);
  815. if (q1 == q2)
  816. {
  817. aux->urm = l;
  818. l = aux;
  819. }
  820. else
  821. {
  822. q2->urm = aux;
  823. aux->urm = q1;
  824. }
  825. return l;
  826. }
  827. sublista *stergere_lista(sublista *l, char *x)
  828. {
  829. sublista *q1, *q2;
  830. for (q1 = q2 = l; q1 != NULL; q2 = q1, q1 = q1->urm)
  831. if (q1 != NULL && strcmp(q1->nume_student, x) == 0) {
  832. if (q1 != q2)
  833. q2->urm = q1->urm;
  834. else
  835. l = l->urm;
  836. free(q1);
  837. return l;
  838. }
  839.  
  840. printf("Eroare:identificatorul %s nu apare in lista\n", x);
  841. return l;
  842.  
  843. }
  844.  
  845. void stergere_student(nod *t,char *x)//inordine
  846. {
  847. if (t != NULL)
  848. {
  849. stergere_student(t->st,x);
  850. //printf("\n %s ", t->disciplina);
  851. sublista *ap;
  852. ap = t->prim;
  853. if (ap)
  854. {
  855. while (ap)
  856. {
  857.  
  858. if (strcmp(ap->nume_student, x)==0)
  859. {
  860. t->prim = stergere_lista(t->prim, x);
  861. ap = t->prim;
  862. }
  863. if(ap!=NULL)
  864. ap = ap->urm;
  865. }
  866. }
  867. else
  868. {
  869. printf("Lista este goala");
  870. }
  871. stergere_student(t->dr,x);
  872. }
  873. }
  874.  
  875. nod *adauga_arbore(nod *t, char *disciplina,char *student,float nota)
  876. {
  877. if (t == NULL)
  878. {
  879. t = (nod*)malloc(sizeof(nod));
  880. strcpy(t->disciplina, disciplina);
  881. t->st = t->dr = NULL;
  882. t->prim = NULL;
  883. t->prim = adauga_nod_lista(t->prim, student, nota);
  884. }
  885. else if (strcmp(disciplina,t->disciplina)<0)
  886. {
  887. t->st = adauga_arbore(t->st, disciplina,student,nota);
  888. }
  889. else if (strcmp(disciplina, t->disciplina)>0)
  890. {
  891. t->dr = adauga_arbore(t->dr, disciplina,student,nota);
  892. }
  893. else
  894. {
  895. //printf("Nodul exista");
  896. t->prim = adauga_nod_lista(t->prim, student, nota);
  897. }
  898.  
  899. return t;
  900. }
  901.  
  902. void afisare(nod *t)//inordine
  903. {
  904. if (t != NULL)
  905. {
  906. afisare(t->st);
  907. printf("\n %s ", t->disciplina);
  908. sublista *ap;
  909. ap = t->prim;
  910. if (ap)
  911. {
  912. while (ap)
  913. {
  914. printf(" %s %f", ap->nume_student, ap->nota);
  915. ap= ap->urm;
  916. }
  917. }
  918. afisare(t->dr);
  919. }
  920. }
  921.  
  922. nod* stergere(nod *t, char *disciplina)
  923. {
  924. nod *aux;
  925. if (t == NULL)
  926. {
  927. printf("Nodul nu exista");
  928. }
  929. else if (strcmp(disciplina, t->disciplina)<0)
  930. {
  931. t->st = stergere(t->st, disciplina);
  932. }
  933. else if (strcmp(disciplina, t->disciplina)>0)
  934. {
  935. t->dr = stergere(t->dr, disciplina);
  936. }
  937. else
  938. if (t->st == NULL)
  939. {
  940. aux = t;
  941. t = t->dr;
  942. free(aux);
  943. }
  944. else if (t->dr == NULL)
  945. {
  946. aux = t;
  947. t = t->st;
  948. free(aux);
  949. }
  950. else
  951. {
  952. t->st = supred(t->st, t);
  953. }
  954. return t;
  955.  
  956. }
  957.  
  958. nod *supred(nod *t, nod *p)
  959. {
  960. nod *q, *aux;
  961. q = t;
  962. if (q->dr != NULL)
  963. {
  964. q->dr = supred(q->dr, p);
  965. }
  966. else
  967. {
  968. //p->nr = q->nr;
  969. strcpy(p->disciplina, q->disciplina);
  970. aux = q;
  971. q = q->st;
  972. free(aux);
  973. }
  974. return q;
  975.  
  976. }
  977.  
  978. int main()
  979. {
  980. char disciplina[50],student[50];
  981. float nota;
  982. int i;
  983. FILE *f;
  984. f=fopen("stud.txt", "rt");
  985. if (f)
  986. {
  987. for (i = 0; i < 10; i++)
  988. {
  989. fscanf(f,"%s %s %f.1", disciplina, student, &nota);
  990. printf("%s %s %f.1\n", disciplina, student, nota);
  991. root = adauga_arbore(root, disciplina, student, nota);
  992. }
  993. afisare(root);
  994. printf("\n \n");
  995. //root = stergere(root,"Matematica");
  996. stergere_student(root,"Alin_Gale");
  997. afisare(root);
  998. }else
  999.  
  1000. return 0;
  1001. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement