Advertisement
tiberiup

ASD-31.10.17

Oct 31st, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. typedef struct celula_lista
  8. {
  9. int k;
  10. struct celula_lista *next;
  11. }
  12. LIST;
  13.  
  14. typedef struct coada
  15. {
  16. LIST *front,*rear;
  17. }
  18. QUEUE;
  19.  
  20. struct lista
  21. {
  22. int k;
  23. struct lista *next,*prev;
  24. };
  25.  
  26. typedef struct stiva
  27. {
  28. int k;
  29. struct stiva *next;
  30. }
  31. STIVA;
  32.  
  33. LIST *cons_lsi(int k,LIST *l);
  34. LIST *init_l(LIST *l);
  35. void tipar(LIST *l);
  36. int maxim(LIST *l);
  37. void aparitii_x(LIST *l);
  38. void citire_matrice(int M[10][10],int *m,int *n);
  39. LIST *liniarizare_matrice(LIST *l,int M[10][10],int m,int n);
  40. LIST *suma_liste(LIST *l1,LIST *l2);
  41. LIST *multime_lista(LIST *l);
  42. void ordonare_lista(LIST *l);
  43. LIST *inversare_lista(LIST *l);
  44. LIST *interclasare_liste(LIST *l1,LIST *l2);
  45. LIST *stergere_element_lista(LIST *l,int poz);
  46. LIST *inserare_element_lista(LIST *l,int poz,int x);
  47. LIST *stergere_elemente_pare_lista(LIST *l);
  48. LIST *initializare_lista_crescatoare(LIST *l);
  49. QUEUE *newq(QUEUE *q);
  50. int emptyq(QUEUE *q);
  51. QUEUE *addq(QUEUE *q,int e);
  52. QUEUE *initializare_coada_cu_header();
  53. void printq(QUEUE *q);
  54. QUEUE *remq(QUEUE *q);
  55. int frontq(QUEUE *q);
  56. int prim(int x);
  57. QUEUE *numere_prime_lista_in_coada(LIST *l,QUEUE *q);
  58. struct lista *cons_ldi(int k,struct lista *l);
  59. struct lista *init_ldi(struct lista *l);
  60. void tipar_ldi(struct lista *l);
  61. struct lista *stergere_element_ldi(struct lista *l,int poz);
  62. struct lista *inserare_element_ldi(struct lista *l,int poz,int x);
  63. STIVA *push(STIVA *vf, int x);
  64. STIVA *creare();
  65. void afisare(STIVA *vf);
  66. STIVA *pop(STIVA *vf);
  67. STIVA *mutare_prime_lista_in_stiva(LIST *l,STIVA *vf);
  68. int main()
  69. {
  70. /*
  71. struct lista *ldi;
  72. ldi=init_ldi(ldi);
  73. tipar_ldi(ldi);
  74. */
  75. LIST*l,*l1,*ls,*lm,*li;
  76. STIVA *s=NULL;
  77. /*
  78. QUEUE *q;
  79. q->front=q->rear=NULL;
  80. int M[10][10],m,n;
  81. */
  82.  
  83. l=initializare_lista_crescatoare(l);
  84. printf("\n\nlista deja introdusa este:\n\n");
  85. tipar(l);
  86.  
  87. s=mutare_prime_lista_in_stiva(l,s);
  88. afisare(s);
  89. /*
  90. q=numere_prime_lista_in_coada(l,q);
  91. printq(q);
  92. l=stergere_elemente_pare_lista(l);
  93. tipar(l);
  94. l=inversare_lista(l);
  95. printf("\nLista introdusa inversata este:\n");
  96. tipar(l);
  97. ordonare_lista(l);
  98. printf("\nLista introdusa ordonate este:\n");
  99. tipar(l);
  100. printf("\nMaximul elementelor din lista este %d.",maxim(l));
  101. aparitii_x(l);
  102. citire_matrice(M,&m,&n);
  103. l1=liniarizare_matrice(l1,M,m,n);
  104. printf("\nAceasta este liniarizarea matricei pe linii:\n");
  105. tipar(l1);
  106. ls=suma_liste(l,l1);
  107. printf("\nAceasta este suma listelor precedente:\n");
  108. tipar(ls);
  109. lm=multime_lista(lm);
  110. printf("\n\nLista multime introdusa este:\n\n");
  111. tipar(lm);
  112. li=interclasare_liste(l,lm);
  113. printf("\nAceasta este interclasarea listei initiale ordonate cu lista multime:\n");
  114. tipar(li);
  115. */
  116.  
  117.  
  118.  
  119. return 0;
  120. }
  121. LIST *cons_lsi(int k,LIST *l)
  122. { LIST *aux;
  123. aux=(LIST *)malloc(sizeof(LIST));
  124. aux->k=k;
  125. aux->next=l;
  126. return(aux);
  127. }
  128. LIST *init_l(LIST *l)
  129. {
  130. int ci,k;
  131. char c,ck,cc[5];
  132. l=NULL;
  133.  
  134. printf("Doriti sa incepeti initializarea listei [n pentru terminare]?");
  135. scanf("%c",&c);
  136. if (c!='n')
  137. k=1;
  138. else
  139. k=-1;
  140. while (k!=-1)
  141. {
  142. printf(" Introduceti o cheie : ");
  143. scanf("%s",cc);
  144. if (strcmp(cc,"n"))
  145. k=1;
  146. else
  147. k=-1;
  148. ci=atoi(cc);
  149. if(k!=-1)
  150. l=cons_lsi(ci,l);
  151. }
  152. return(l);
  153. }
  154.  
  155. void tipar(LIST *l)
  156. { LIST *p;
  157. for(p=l;p;p=p->next) printf("%d->",p->k);
  158. printf("NULL\n");
  159. }
  160.  
  161. int maxim(LIST *l)
  162. {
  163. LIST *p;
  164. int Max=l->k;
  165. for(p=l->next;p;p=p->next)
  166. if(p->k>Max)
  167. Max=p->k;
  168. return Max;
  169. }
  170.  
  171. void aparitii_x(LIST *l)
  172. {
  173. LIST *p;
  174. int x,nr=0;
  175. printf("\n\nIntroduceti numarul pe care doriti sa-l cautati in lista: ");
  176. scanf("%d",&x);
  177. for(p=l;p;p=p->next)
  178. if(p->k==x)
  179. nr++;
  180. if(!nr)
  181. printf("\nNumarul cautat nu se afla in lista.");
  182. else
  183. printf("\nNumarul cautat apare de %d ori in lista.",nr);
  184. }
  185.  
  186. void citire_matrice(int M[10][10],int *m,int *n)
  187. {
  188. int i,j;
  189. printf("\n\nIntroduceti numarul de linii, numarul de coloane\nsi apoi elementele matricei.\n");
  190. scanf("%d%d",m,n);
  191. for(i=0;i<*m;i++)
  192. for(j=0;j<*n;j++)
  193. scanf("%d",&M[i][j]);
  194. }
  195.  
  196. LIST *liniarizare_matrice(LIST *l,int M[10][10],int m,int n)
  197. {
  198. int i,j;
  199. l=NULL;
  200. for(i=m-1;i>=0;i--)
  201. for(j=n-1;j>=0;j--)
  202. l=cons_lsi(M[i][j],l);
  203. return l;
  204. }
  205.  
  206. LIST *suma_liste(LIST *l1,LIST *l2)
  207. {
  208. LIST *l=NULL,*ll=NULL,*p,*p1=l1->next,*p2=l2->next;
  209. l=cons_lsi(l1->k+l2->k,l);
  210. p=l;
  211. while(p1&&p2)
  212. {
  213. ll=cons_lsi(p1->k+p2->k,ll);
  214. p->next=ll;
  215. p=ll;
  216. p1=p1->next;
  217. p2=p2->next;
  218. }
  219. while(p1)
  220. {
  221. ll=cons_lsi(p1->k,ll);
  222. p->next=ll;
  223. p=ll;
  224. p1=p1->next;
  225. }
  226. while(p2)
  227. {
  228. ll=cons_lsi(p2->k,ll);
  229. p->next=ll;
  230. p=ll;
  231. p2=p2->next;
  232. }
  233. p->next=NULL;
  234. return l;
  235. };
  236.  
  237. LIST *multime_lista(LIST *l)
  238. {
  239. LIST *p1,*p2,*ll;
  240. int ci,k;
  241. char c,cc[5];
  242. l=NULL;
  243. printf("Doriti sa incepeti initializarea listei de tip multime[n pentru terminare]?\n");
  244. scanf("%c",&c);
  245. if (c!='n')
  246. k=1;
  247. else
  248. k=-1;
  249. while (k!=-1)
  250. {
  251. printf("Introduceti o cheie : ");
  252. scanf("%s",cc);
  253. if (strcmp(cc,"n"))
  254. k=1;
  255. else
  256. k=-1;
  257. ci=atoi(cc);
  258. if(k!=-1)
  259. {
  260. p1=NULL;
  261. for(p2=l;p2&&ci>p2->k;p2=p2->next)
  262. p1=p2;
  263. ll=cons_lsi(ci,ll);
  264. if(!p1&&!p2)
  265. {
  266. l=ll;
  267. l->next=NULL;
  268. }
  269.  
  270. if(!p1&&p2)
  271. if(ci!=p2->k)
  272. {
  273. l=ll;
  274. l->next=p2;
  275. }
  276. if(p1&&!p2)
  277. {
  278. p1->next=ll;
  279. ll->next=p2;
  280. }
  281. if(p1&&p2)
  282. if(ci!=p2->k)
  283. {
  284. p1->next=ll;
  285. ll->next=p2;
  286. }
  287.  
  288. }
  289. }
  290. return(l);
  291. };
  292. void ordonare_lista(LIST *l)
  293. {
  294. LIST *p,*q;
  295. int aux;
  296. for(p=l;p->next;p=p->next)
  297. for(q=p->next;q;q=q->next)
  298. if(p->k>q->k)
  299. {
  300. aux=p->k;
  301. p->k=q->k;
  302. q->k=aux;
  303. }
  304. };
  305.  
  306. LIST *inversare_lista(LIST *l)
  307. {
  308. LIST *p=l,*q=p->next,*r=q->next;
  309. while(q)
  310. {
  311. q->next=p;
  312. p=q;
  313. q=r;
  314. if(r)
  315. r=r->next;
  316. }
  317. l->next=NULL;
  318. l=p;
  319. return l;
  320. };
  321. LIST *interclasare_liste(LIST *l1,LIST *l2)
  322. {
  323. LIST *l=NULL,*ll=NULL,*p,*p1=l1,*p2=l2;
  324. if(p1->k<p2->k)
  325. {
  326. l=cons_lsi(p1->k,ll);
  327. p1=p1->next;
  328. }
  329. else
  330. {
  331. l=cons_lsi(p2->k,ll);
  332. p2=p2->next;
  333. }
  334. p=l;
  335. while(p1&&p2)
  336. {
  337. if(p1->k<p2->k)
  338. {
  339. ll=cons_lsi(p1->k,ll);
  340. p1=p1->next;
  341. }
  342. else
  343. {
  344. ll=cons_lsi(p2->k,ll);
  345. p2=p2->next;
  346. }
  347. p->next=ll;
  348. p=ll;
  349. }
  350. while(p1)
  351. {
  352. ll=cons_lsi(p1->k,ll);
  353. p->next=ll;
  354. p=ll;
  355. p1=p1->next;
  356. }
  357. while(p2)
  358. {
  359. ll=cons_lsi(p2->k,ll);
  360. p->next=ll;
  361. p=ll;
  362. p2=p2->next;
  363. }
  364. p->next=NULL;
  365. return l;
  366. };
  367. LIST *stergere_element_lista(LIST *l,int poz)
  368. {
  369. LIST *p=l,*q;
  370. int pozl=1;
  371. if(l->next)
  372. {
  373. if(poz==1)
  374. return l->next;
  375. }
  376. else
  377. return NULL;
  378. while(poz>pozl)
  379. {
  380. q=p;
  381. p=p->next;
  382. pozl++;
  383. if(!p&&poz>=pozl)
  384. {
  385. printf("Eroare - Lista are mai putine elemente");
  386. return l;
  387. }
  388. }
  389. q->next=p->next;
  390. free(p);
  391. return l;
  392. };
  393.  
  394. LIST *inserare_element_lista(LIST *l,int poz, int x)
  395. {
  396. LIST *p=l,*q;
  397. int pozl=1;
  398. if(poz==1)
  399. l=cons_lsi(x,l);
  400. else
  401. {
  402. while(poz>pozl)
  403. {
  404. q=p;
  405. p=p->next;
  406. pozl++;
  407. if(!p&&poz>pozl)
  408. {
  409. printf("Eroare - Elementul nu poate fi legat de lista");
  410. return l;
  411. }
  412. }
  413. p=cons_lsi(x,p);
  414. q->next=p;
  415. }
  416. return l;
  417. };
  418.  
  419. LIST *stergere_elemente_pare_lista(LIST *l)
  420. {
  421. LIST *p;
  422. int poz=1;
  423. for(p=l;p;p=p->next)
  424. if(p->k%2==0)
  425. l=stergere_element_lista(l,poz);
  426. else
  427. poz++;
  428. return l;
  429. };
  430.  
  431. LIST *initializare_lista_crescatoare(LIST *l)
  432. {
  433. LIST *p1;
  434. int ci,k,poz;
  435. char c,ck,cc[5];
  436. l=NULL;
  437. printf("Doriti sa incepeti initializarea listei crescatoare[n pentru terminare]?");
  438. scanf("%c",&c);
  439. if (c!='n')
  440. k=1;
  441. else
  442. k=-1;
  443. while (k!=-1)
  444. {
  445. printf(" Introduceti o cheie : ");
  446. scanf("%s",cc);
  447. if (strcmp(cc,"n"))
  448. k=1;
  449. else
  450. k=-1;
  451. ci=atoi(cc);
  452. poz=1;
  453. if(k!=-1)
  454. if(!l)
  455. l=inserare_element_lista(l,poz,ci);
  456. else
  457. {
  458. for(p1=l;p1&&ci>p1->k;p1=p1->next)
  459. poz++;
  460. l=inserare_element_lista(l,poz,ci);
  461. }
  462. }
  463. return(l);
  464. }
  465.  
  466. QUEUE *newq(QUEUE *q)
  467. {
  468. q=(QUEUE *)malloc(sizeof(QUEUE));
  469. q->front=q->rear=NULL;
  470. return(q);
  471. }
  472.  
  473. int emptyq(QUEUE *q)
  474. {if((q->front==q->rear)&&(q->front==NULL))
  475. return(1);
  476. else return(0);
  477. }
  478.  
  479. QUEUE *addq(QUEUE *q,int e)
  480. {LIST *aux;
  481. aux=(LIST *)malloc(sizeof(LIST));
  482. aux->k=e;
  483. aux->next=NULL;
  484. if ((q->front==NULL)&&(q->rear==NULL)) /* e primul element */
  485. q->front=q->rear=aux;
  486. else
  487. {
  488. q->rear->next=aux;
  489. q->rear=aux;
  490. }
  491. return(q);
  492. }
  493.  
  494. QUEUE *initializare_coada_cu_header()
  495. {LIST *aux;
  496. char c,c1; int k;
  497. QUEUE *q;
  498. q=newq(q); /* aux=NULL;q->front=q->rear=aux;*/
  499. printf(" DORITI SA INCEPETI INITIALIZAREA COZII ? [D/N] ");
  500. scanf("%c",&c);c1=getchar();
  501. while ((c=='d') || (c=='D'))
  502. {
  503. printf(" \n INTRODUCETI O CHEIE : "); scanf("%d",&k); c1=getchar();
  504. q=addq(q,k);
  505. printf("\n CONTINUATI ? "); scanf("%c",&c); c1=getchar();
  506. }
  507. return(q);
  508. }
  509.  
  510. void printq(QUEUE *q)
  511. { LIST *p;
  512. p=q->front;
  513. if(emptyq(q)) printf(" coada vida \n ");
  514. else
  515. { do
  516. {
  517. printf("%d ",p->k);
  518. p=p->next;
  519. }
  520. while (p!=NULL);
  521. }
  522. printf(" \n ");
  523. }
  524.  
  525. QUEUE *remq(QUEUE *q)
  526. {LIST *out;
  527. if(!emptyq(q))
  528. {out=q->front;
  529. if (q->front->next)
  530. q->front=q->front->next;
  531. else q->front=q->rear=NULL;
  532. free(out);
  533. return(q);
  534. }
  535. else
  536. {
  537. printf("\n Coada era vida !");
  538. return NULL;
  539. }
  540. }
  541.  
  542. int frontq(QUEUE *q)
  543. {if(!emptyq(q))
  544. return(q->front->k);
  545. else
  546. {printf("\n Coada este vida."); return(-1);}
  547. }
  548.  
  549. int prim(int x)
  550. {
  551. int i;
  552. if(x<2)
  553. return 0;
  554. for(i=2;i<=x/2;i++)
  555. if(x%i==0)
  556. return 0;
  557. return 1;
  558. }
  559.  
  560. QUEUE *numere_prime_lista_in_coada(LIST *l,QUEUE *q)
  561. {
  562. LIST *p;
  563. for(p=l;p;p=p->next)
  564. if(prim(p->k))
  565. {
  566. if(emptyq(q))
  567. q=newq(q);
  568. q=addq(q,p->k);
  569. }
  570. return q;
  571. }
  572.  
  573. struct lista *cons_ldi(int k,struct lista *l)
  574. { struct lista *aux;
  575. aux=(struct lista *)malloc(sizeof(struct lista));
  576. aux->k=k;
  577. aux->next=l;
  578. aux->prev=NULL;
  579. if(l) l->prev=aux;
  580. return(aux);
  581. }
  582.  
  583. struct lista *init_ldi(struct lista *l)
  584. {
  585. int ci,k;
  586. char c,ck,cc[5];
  587. l=NULL;
  588.  
  589. printf("Doriti sa incepeti initializarea listei [n pentru terminare]?");
  590. scanf("%c",&c);
  591. if (c!='n')
  592. k=1;
  593. else
  594. k=-1;
  595. while (k!=-1)
  596. {
  597. printf(" o cheie : ");
  598. scanf("%s",cc);
  599. if (strcmp(cc,"n"))
  600. k=1;
  601. else
  602. k=-1;
  603. ci=atoi(cc);
  604. if(k!=-1)
  605. l=cons_ldi(ci,l);
  606. }
  607. return(l);
  608. }
  609.  
  610. void tipar_ldi(struct lista *l)
  611. { struct lista *p;
  612. printf("NULL");
  613. for (p=l;p;p=p->next)printf("<=>%d",p->k);
  614. printf("<=>NULL\n");
  615. }
  616.  
  617. struct lista *stergere_element_ldi(struct lista *l,int poz)
  618. {
  619.  
  620. };
  621.  
  622. struct lista *inserare_element_ldi(struct lista *l,int poz,int x)
  623. {
  624.  
  625. };
  626.  
  627. STIVA *push(STIVA *vf, int x)
  628. {
  629. STIVA *aux;
  630. aux=(STIVA*)malloc(sizeof(STIVA));
  631. aux->k=x;
  632. aux->next=vf;
  633. vf=aux;
  634. return vf;
  635. }
  636.  
  637. STIVA *creare()
  638. {
  639. STIVA *vf,*aux;
  640. char c;
  641. int x;
  642.  
  643. vf=NULL;
  644. printf("Doriti sa adaugati nod in STIVA (d/n): ");
  645. c=getche();
  646. getchar();
  647. while(c=='d' || c=='D')
  648. {
  649. printf("Info: ");
  650. scanf("%i",&x);
  651. getchar();
  652. vf=push(vf,x);
  653. printf("\nDoriti sa adaugati nod in STIVA (d/n): ");
  654. c=getche();
  655. getchar();
  656. }
  657. return vf;
  658. }
  659. void afisare(STIVA *vf)
  660. {
  661. STIVA *p;
  662.  
  663. for(p=vf;p;p=p->next)
  664. printf("%i->",p->k);
  665. printf("NULL");
  666. }
  667.  
  668. STIVA *pop(STIVA *vf)
  669. {
  670. STIVA *aux;
  671. aux=vf;
  672. vf=vf->next;
  673. free(aux);
  674. return vf;
  675. }
  676.  
  677. STIVA *mutare_prime_lista_in_stiva(LIST *l,STIVA *vf)
  678. {
  679. LIST *p=l;
  680. int poz=1;
  681. while(p)
  682. {
  683. if(prim(p->k))
  684. {
  685. vf=push(vf,p->k);
  686. p=p->next;
  687. l=stergere_element_lista(l,poz);
  688. }
  689. else
  690. {
  691. p=p->next;
  692. poz++;
  693. }
  694. }
  695. return vf;
  696. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement