Advertisement
Guest User

Untitled

a guest
May 25th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #define SIZE 5;
  4.  
  5. using namespace std;
  6.  
  7.  
  8. struct Node
  9. {
  10. int data;
  11. Node *next; //Указатель на соседа
  12. Node *head; // Голова списка дочерних узлов
  13. };
  14.  
  15. void generate(Node*& tree, int count)
  16. {
  17. queue<Node*> crap;
  18. tree->data=rand()%100;
  19. count--;
  20. crap.push(tree);
  21. while(!crap.empty() && count)
  22. {
  23. Node *curr = crap.front(); //Родительский узел, для которого генерируются дочерние узлы
  24. crap.pop();
  25. Node *temp = new Node; // Голова списка дочерних узлов
  26. memset(temp, 0, sizeof(Node*));
  27. // int val = rand() % SIZE + 1;
  28. int val = 5;
  29. temp->data = rand()%100;
  30. curr->head=temp;
  31. crap.push(temp);
  32. val--;
  33. count--;
  34. Node *prev = temp;
  35. temp=temp->next;
  36. while(val && count)
  37. {
  38. Node *temp = new Node;
  39. memset(temp, 0, sizeof(Node));
  40. temp->data = rand()%100;
  41. crap.push(temp);
  42. val--;
  43. count--;
  44. prev->next=temp;
  45. prev = temp;
  46. temp=temp->next;
  47. }
  48.  
  49. }
  50.  
  51. }
  52.  
  53. void print(Node* tree)
  54. {
  55. cout << tree->data << endl;
  56. queue<Node*> ptr;
  57. ptr.push(tree);
  58. while(!ptr.empty())
  59. {
  60. Node *curr = ptr.front();
  61. ptr.pop();
  62. Node *temp = curr->head;
  63. while(temp)
  64. {
  65. cout << temp->data << " ";
  66. ptr.push(temp);
  67. temp=temp->next;
  68. }
  69. cout << endl;
  70.  
  71. }
  72. }
  73.  
  74. void main()
  75. {
  76. Node *kal = new Node;
  77. memset(kal, 0, sizeof(Node));
  78. generate(kal, 65);
  79. print(kal);
  80.  
  81. cout << "yay";
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement