Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <stdlib.h>
  4. #include <cmath>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. class Queue {
  10.  
  11. private:
  12. struct Node {
  13. int value;
  14. Node* next;
  15. Node* prev;
  16. };
  17.  
  18. Node *first;
  19. Node *last;
  20.  
  21. int *p_darr;
  22. int n;
  23. int dlina;
  24.  
  25.  
  26. public:
  27. Queue() {
  28. first = NULL;
  29. last = first;
  30. }
  31.  
  32. void add () {
  33. Node *temp;
  34.  
  35.  
  36. int v = rand() % 15;
  37.  
  38. if(first == NULL) {
  39. first = new Node;
  40. first->value = v;
  41. first->next = NULL;
  42. last = first;
  43. first->prev = NULL;
  44. }
  45.  
  46. else {
  47.  
  48. temp = new Node;
  49. temp->value = v;
  50. temp->next = NULL;
  51.  
  52. last->next = temp;
  53. temp->prev = last;
  54. last = temp;
  55. }
  56.  
  57. }
  58.  
  59. void fill() {
  60. srand(time(0));
  61. cout<<"Vvedite dliny ocheredi ";
  62. cin>>dlina;
  63.  
  64. for(int i=0;i<dlina;i++) {
  65. add();
  66. }
  67. }
  68.  
  69.  
  70.  
  71. void get() {
  72. for(int i= 0;i<dlina;i++){
  73. cout<<first->value<<" ";
  74. first = first->next;
  75. }
  76. }
  77.  
  78. void get2() {
  79. for(int i= 0;i<(dlina+n);i++){
  80. cout<<first->value<<" ";
  81. first = first->next;
  82. }
  83. }
  84.  
  85. void back() {
  86. Node *temp;
  87. for (int i = 0; i < (dlina - 1); i ++) {
  88. temp = last->prev;
  89. last = temp;
  90. }
  91.  
  92. first = last;
  93. while(temp->next != NULL) {
  94. temp = last->next;
  95. last = temp;
  96. }
  97.  
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104. void move() {
  105.  
  106. cout<<"Vvedite kol-vo elementov dla peremech iz 1 ocheredi vo 2: ";
  107.  
  108. cin>>n;
  109.  
  110. p_darr = new int[n];
  111.  
  112. Node *temp;
  113.  
  114. cout<<"move elements: ";
  115. for(int i = 0; i<n ; i++) {
  116. p_darr[i] = first->value;
  117. cout<<p_darr[i]<<" ";
  118. temp=first;
  119. first=first->next;
  120. delete temp;
  121. }
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128. void metamorf() {
  129. Node *temp;
  130. for (int i = 0; i<n; i ++) {
  131. temp = new Node;
  132. temp->next = NULL;
  133. temp->value = p_darr[i];
  134.  
  135.  
  136. last->next = temp;
  137. temp->prev = last;
  138. last = temp;
  139. }
  140.  
  141. }
  142.  
  143.  
  144. ~Queue() {
  145. delete [] p_darr;
  146. }
  147.  
  148. };
  149.  
  150.  
  151.  
  152. int main()
  153.  
  154. {
  155. Queue a;
  156. Queue b;
  157.  
  158.  
  159.  
  160. a.fill();
  161.  
  162. cout<<"1 ochered: ";
  163. a.get();
  164.  
  165.  
  166. cout<<endl;
  167.  
  168.  
  169. a.back();
  170.  
  171. cout<<endl<<endl;
  172.  
  173. b.fill();
  174.  
  175. cout<<"2 ochered: ";
  176.  
  177. b.get();
  178. b.back();
  179. cout<<endl<<endl;
  180.  
  181. a.move();
  182. cout<<endl<<endl;
  183.  
  184.  
  185.  
  186. return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement