aisp

PISMENI ISPIT - 29.01.2019.

Aug 26th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. // 1. zadatak
  2. struct Node
  3. {
  4.     int data;
  5.     struct Node* next;
  6. };
  7.  
  8. struct Node* createList() {
  9.     int arr[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
  10.     struct Node* p = (struct Node*)malloc(sizeof(struct Node));
  11.  
  12.     for (int i = 0; i < 10; i++) {
  13.         p->data = arr[i];
  14.         p->next = (struct Node*)malloc(sizeof(struct Node));
  15.         p = p->next;
  16.     }
  17.     p->next = NULL;
  18.  
  19.     return p;
  20. }
  21.  
  22. void addNode(struct Node* p, int x) {
  23.     while (p->next != NULL) {
  24.         if (x > p->data) {
  25.             struct Node* novi = (struct Node*)malloc(sizeof(struct Node));
  26.             novi->data = x;
  27.             novi->next = p->next;
  28.             p->next = novi;
  29.             return;
  30.         }
  31.         p = p->next;
  32.     }
  33.  
  34.     struct Node* novi = (struct Node*)malloc(sizeof(struct Node));
  35.     novi->data = x;
  36.     novi->next = NULL;
  37.     p->next = novi;
  38. }
  39.  
  40. // 2. zadatak
  41. /**/
  42.  
  43. // 3. zadatak
  44. struct cvor {
  45.     int x;
  46.     struct cvor *left, *right;
  47. };
  48.  
  49. int findMin(struct cvor* root)
  50. {
  51.     if (root == NULL) { return -1; }
  52.  
  53.     int res = root->x;
  54.     int lres = findMin(root->left);
  55.     int rres = findMin(root->right);
  56.     if (lres < res) { res = lres; }
  57.     if (rres < res) { res = rres; }
  58.  
  59.     return res;
  60. }
  61.  
  62. int addBT(struct cvor* root)
  63. {
  64.     if (root == NULL) {
  65.         return 0;
  66.     }
  67.  
  68.     return (root->key + addBT(root->left) + addBT(root->right));
  69. }
Advertisement
Add Comment
Please, Sign In to add comment