Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. /* Programul creeaza o lista dublu inlantuita */
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <malloc.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. struct lista
  10. {int k;
  11. struct lista *next,*prev;
  12. };
  13.  
  14. struct lista *cons_ldi(int k,struct lista *l)
  15. { struct lista *aux;
  16. aux=(struct lista *)malloc(sizeof(struct lista));
  17. aux->k=k;
  18. aux->next=l;
  19. aux->prev=NULL;
  20. if(l) l->prev=aux;
  21. return(aux);
  22. }
  23.  
  24. struct lista *init_ldi(struct lista *l)
  25. {
  26. int ci,k;
  27. char c,ck,cc[5];
  28. l=NULL;
  29.  
  30. printf("Doriti sa incepeti initializarea listei [n pentru terminare]?");
  31. scanf("%c",&c);
  32. if (c!='n')
  33. k=1;
  34. else
  35. k=-1;
  36. while (k!=-1)
  37. {
  38. printf(" o cheie : ");
  39. scanf("%s",cc);
  40. if (strcmp(cc,"n"))
  41. k=1;
  42. else
  43. k=-1;
  44. ci=atoi(cc);
  45. if(k!=-1)
  46. l=cons_ldi(ci,l);
  47. }
  48. return(l);
  49. }
  50.  
  51. void tipar_ldi(struct lista *l)
  52. { struct lista *p;
  53. printf("NULL");
  54. for (p=l;p;p=p->next)printf("<=>%d",p->k);
  55. printf("<=>NULL\n");
  56. }
  57.  
  58. struct lista *inserare(struct lista *l, int poz, int inf){
  59. struct lista *aux;
  60. struct lista *p;
  61.  
  62. aux=(struct lista *)malloc(sizeof(struct lista));
  63. aux->k=inf;
  64. if(poz==1){
  65. aux->prev=NULL;
  66. aux->next=l;
  67. if(l!=NULL)
  68. l->prev=aux;
  69. l=aux;
  70. }
  71. else{
  72. p=l;
  73. for(int i=1;i<=poz-2&&p!=NULL;i++)
  74. p=p->next;
  75.  
  76. aux->next=p->next;
  77. if(p->next!=NULL)
  78. p->next->prev=aux;
  79. p->next=aux;
  80. aux->prev=p;
  81. }
  82. return l;
  83.  
  84.  
  85.  
  86.  
  87. };
  88.  
  89. int main()
  90. {
  91. struct lista *l;
  92. int k;
  93.  
  94. l=init_ldi(l);
  95. printf(" \n \n LISTA INITIALA : ");
  96. tipar_ldi(l);
  97. l=inserare(l,3,3333);
  98. printf(" \n \n LISTA DUPA INSERTIE : ");
  99. tipar_ldi(l);
  100.  
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement