Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. ifstream fin("cuvinte.in");
  8.  
  9. int n;
  10.  
  11. struct nod
  12. {
  13. char cuvant[25];
  14. nod *leg;
  15. } *prim;
  16.  
  17. void ad_dupa(nod *&prim, nod *q, char x[25])
  18. {
  19. nod *r = new nod;
  20. strcpy(r -> cuvant, x);
  21. r -> leg = q -> leg;
  22. q -> leg = r;
  23. }
  24.  
  25. void ad_sf(nod *&prim, char cuv[25])
  26. {
  27. nod *p = new nod;
  28. nod *q;
  29. p -> leg = NULL;
  30. strcpy(p -> cuvant, cuv);
  31. if (prim == NULL)
  32. {
  33. prim = p;
  34. }
  35. else
  36. {
  37. q = prim;
  38. while (q -> leg != NULL)
  39. {
  40. q = q -> leg;
  41. }
  42. q -> leg = p;
  43. }
  44. }
  45.  
  46. void creare_lista(nod *&prim)
  47. {
  48. nod *p;
  49. char c[25];
  50. int ok;
  51. fin >> n;
  52. for (int i = 1; i <= n; ++i)
  53. {
  54. fin >> c;
  55. ok = 1;
  56. p = prim;
  57. while (p)
  58. {
  59. if (strcmp(p -> cuvant, c) == 0)
  60. {
  61. ok = 0;
  62. }
  63. p = p -> leg;
  64. }
  65. if (ok == 1)
  66. ad_sf(prim, c);
  67. }
  68. }
  69.  
  70. void afisare(nod *prim)
  71. {
  72. nod *p;
  73. for (p = prim; p != NULL; p = p -> leg)
  74. {
  75. cout << p -> cuvant << ' ';
  76. }
  77. cout << endl;
  78. }
  79.  
  80. void ordonare(nod *&prim)
  81. {
  82. nod *q, *p;
  83. for (p = prim; p -> leg != NULL; p = p -> leg)
  84. {
  85. for (q = p -> leg; q; q = q -> leg)
  86. {
  87. if (strlen(q -> cuvant) < strlen(p -> cuvant))
  88. {
  89. swap(q -> cuvant, p -> cuvant);
  90. }
  91. }
  92. }
  93. }
  94.  
  95. void rezolvare(nod *prim)
  96. {
  97. int maxim = 0;
  98. nod *p = prim;
  99. while (p)
  100. {
  101. if (strlen(p -> cuvant) > maxim)
  102. {
  103. maxim = strlen(p -> cuvant);
  104. }
  105. p = p -> leg;
  106. }
  107. p = prim;
  108. while (p)
  109. {
  110. if (strlen(p -> cuvant) == maxim)
  111. {
  112. cout << p -> cuvant << ' ';
  113. }
  114. p = p -> leg;
  115. }
  116. }
  117.  
  118. void rzv(nod *&prim)
  119. {
  120. char C[25] = "mylist";
  121. nod *p = prim;
  122. while (p)
  123. {
  124. if (strlen(p -> cuvant) % 2 == 0)
  125. {
  126. ad_dupa(prim, p, C);
  127. p = p -> leg;
  128. }
  129. p = p -> leg;
  130. }
  131. }
  132.  
  133. int main()
  134. {
  135. creare_lista(prim);
  136. cout << "Cerinta a) ";
  137. afisare(prim);
  138. cout << "Cerinta b) ";
  139. ordonare(prim);
  140. afisare(prim);
  141. cout << "Cerinta c) ";
  142. rezolvare(prim);
  143. cout << endl;
  144. cout << "Cerinta d) ";
  145. rzv(prim);
  146. afisare(prim);
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement