Advertisement
J00ker

Untitled

May 4th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #define Nmax 1000
  3.  
  4. using namespace std;
  5.  
  6. struct Lista
  7. {
  8. int t[Nmax];
  9. int pr, ul;
  10.  
  11. void Init()
  12. {
  13. pr = 0; ul = -1;
  14. }
  15.  
  16. int Empty()
  17. {
  18. return pr > ul;
  19. }
  20.  
  21. void InsertEnd(int x)
  22. {
  23. t[++ul] = x;
  24. }
  25.  
  26. void Insert(int poz, int x)
  27. {
  28. for(int i = ul; i >= poz + pr - 1; i--)
  29. t[i+1] = t[i];
  30. t[poz + pr - 1] = x;
  31. ul++;
  32. }
  33.  
  34. void Parcurgere()
  35. {
  36. if(Empty()) cout << "Lista este vida!";
  37. else
  38. {
  39. for(int i = pr; i <= ul; i++)
  40. cout << t[i] << " ";
  41. cout << "\n";
  42. }
  43. }
  44.  
  45. void DeleteEnd()
  46. {
  47. if(!Empty()) ul--;
  48. }
  49.  
  50. void DeleteBegin()
  51. {
  52. if(!Empty()) pr++;
  53. }
  54.  
  55. void Delete(int poz)
  56. {
  57. if(Empty()) return;
  58. for(int i = poz + pr - 1; i < ul; i++)
  59. t[i] = t[i+1];
  60. ul--;
  61. }
  62.  
  63. int GetElement(int k)
  64. {
  65. return t[pr + k - 1];
  66. }
  67.  
  68. int Search(int x)
  69. {
  70. int i;
  71. for(i = pr; i <= ul; i++)
  72. if(t[i] == x) return i - pr + 1;
  73. return -1;
  74. }
  75.  
  76. int Size()
  77. {
  78. return ul - pr + 1;
  79. }
  80.  
  81. };
  82.  
  83. int main()
  84. {
  85. Lista L;
  86. L.Init();
  87.  
  88. int a[10] = {1, 4, 3, 5, 6, 10, 8, 9, 7, 2};
  89. for(int i = 0; i < 10; i++)
  90. L.InsertEnd(a[i]);
  91.  
  92. L.Parcurgere(); cout << "\n";
  93. int i, j, iaux, jaux;
  94. i = 1; j = L.Size();
  95. while(i < j)
  96. {
  97. iaux = L.GetElement(i);
  98. jaux = L.GetElement(j);
  99.  
  100. if(iaux % 2 == 0)
  101. iaux = L.GetElement(++i);
  102.  
  103. if(jaux % 2 == 1)
  104. jaux = L.GetElement(--j);
  105.  
  106. if((iaux % 2 == 1) && (jaux % 2 == 0))
  107. {
  108. L.Delete(i);
  109. L.Insert(i, jaux);
  110. i++;
  111.  
  112. L.Delete(j);
  113. L.Insert(j, iaux);
  114. j--;
  115. }
  116. }
  117. L.Parcurgere();
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement