Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include <ctype.h>
  6.  
  7. typedef struct stud
  8. {
  9. char nume[20];
  10. int varsta;
  11. struct stud* next;
  12. }Lstud;
  13. Lstud *prim, *ultim, *p;
  14.  
  15.  
  16. Lstud *cautare(char cheie[20])
  17. {
  18. p=prim;
  19. while(p!=0){
  20. if(strcmp(p->nume,cheie)==0)
  21. return p;
  22. else
  23. p=p->next;
  24. }
  25. return 0;
  26. }
  27. // la final
  28. void addElement(char n[],int v){
  29. p=(Lstud*)malloc(sizeof(Lstud));
  30. strcpy(p->nume,n);
  31. p->varsta=v;
  32. p->next=0;
  33.  
  34. if(ultim==0)
  35. prim=p;
  36. else
  37. ultim->next=p;
  38. ultim=p;
  39. }
  40. void creare(){
  41. FILE *f;
  42. int v;
  43. char str[30],*tk;
  44. const char sep[2]=",";
  45. f=fopen("date.txt","r");
  46. while(!feof(f))
  47. {
  48. fgets(str,30,f);
  49. tk=strtok(str,sep);
  50. v=atoi(strtok(NULL,sep));
  51. addElement(tk,v);
  52.  
  53. }
  54.  
  55.  
  56. }
  57. //la inceput
  58.  
  59. void add_prim(){
  60. char n[20];
  61. int v;
  62. printf("\nDati elementele nodului");
  63. scanf("%s %d",n,&v);
  64.  
  65. p=(Lstud*)malloc(sizeof(Lstud));
  66. strcpy(p->nume,n);
  67. p->varsta=v;
  68.  
  69. if(prim==0)
  70. ultim=p;
  71. else
  72. p->next=prim;
  73. prim=p;
  74. }
  75.  
  76. void add_cheie(){
  77. char n[20];
  78. int v;
  79. Lstud *temp,*temp2,*q;
  80. p=prim;
  81. printf("\nDati cheia");
  82. scanf("%s",n);
  83. temp=cautare(n);
  84. if(temp==0)
  85. {
  86. printf("Elementul nu a fost gasit");
  87. return ;
  88. }
  89.  
  90. else {
  91. while (p->nume != temp->nume)
  92. {
  93. q=p;
  94. p=p->next;
  95. }
  96. printf("\nDati elementele nodului");
  97. scanf("%s %d",n,&v);
  98.  
  99. temp2=(Lstud*)malloc(sizeof(Lstud));
  100. strcpy(temp2->nume,n);
  101. temp2->varsta=v;
  102. temp2->next=temp;
  103. q->next=temp2;
  104. }
  105.  
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. int main()
  117. {
  118. prim=0;
  119. ultim=0;
  120. int opt = -1;
  121. while(opt!=4)
  122. {
  123. printf("\n 1.Citire_date \n 2.Salvare \n 3.Afisare \n \n 4.Exit");
  124. printf("\nDati optiunea:\n");
  125. scanf("%d",&opt);
  126.  
  127. switch(opt)
  128. {
  129. case 1:creare(); break;
  130. case 2:add_prim();break;
  131. case 3: add_cheie();break;
  132. case 4: return 0;
  133. default: printf("\n Optiune gresita"); break;
  134. }}
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement