SHARE
TWEET

IZLAZNI KOLOKVIJ - 26.01.2018. (A)

aisp Jun 12th, 2019 (edited) 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 1. zadatak
  2. int* stack = (int*)malloc(10 * sizeof(int));
  3. int size = 10;
  4. int sp = -1;
  5.  
  6. bool isempty() {
  7.     return (sp == -1);
  8. }
  9.  
  10. bool isfull() {
  11.     return (sp == size - 1);
  12. }
  13.  
  14. void push(int x) {
  15.     if (isfull()) {
  16.         printf("Error!");
  17.     }
  18.     else {
  19.         sp++;
  20.         stack[sp] = x;
  21.     }
  22. }
  23.  
  24. void pop() {
  25.     if (isempty()) {
  26.         printf("Error!");
  27.     }
  28.     else {
  29.         printf("%d", stack[sp]);
  30.         sp--;
  31.     }
  32. }
  33.  
  34. void peek() {
  35.     if (!isempty()) {
  36.         printf("%d", stack[sp]);
  37.     }
  38. }
  39.  
  40. void clear() {
  41.     while (!isempty()) {
  42.         pop();
  43.     }
  44. }
  45.  
  46. // 2. zadatak
  47. struct cvor {
  48.     int x;
  49.     cvor *next;
  50. };
  51.  
  52. void provjeri_sortiranost(struct cvor* p) {
  53.     int count = 1;
  54.     int brojac = 0;
  55.     int usporedba = 0;
  56.  
  57.     while (p != NULL) {
  58.         if (brojac == 0) {
  59.             usporedba = p->x;
  60.             p = p->next;
  61.         }
  62.         else {
  63.             if (p->x<usporedba) {
  64.                 std::cout << "Nije sortiran" << std::endl;
  65.                 break;
  66.             }
  67.             usporedba = p->x;
  68.             p = p->next;
  69.         }
  70.         brojac++;
  71.     }
  72.     std::cout << "\n";
  73. }
  74.  
  75. // 3. zadatak
  76. int Faktorijele(int fac){
  77.     if (fac == 1) {
  78.         return 1;
  79.     }
  80.     return fac*Faktorijele(fac - 1);
  81. }
  82.  
  83. // 4. zadatak
  84. struct cvor {
  85.     int x;
  86.     struct cvor *left, *right;
  87. };
  88.  
  89. void ubaci(struct cvor *r, struct cvor *p) {
  90.     if ((r->right == NULL) && (p->x > r->x)) {
  91.         r->right = p;
  92.     }
  93.     else if ((r->right != NULL) && (p->x > r->x)) {
  94.         ubaci(r->right, p);
  95.     }
  96.     if ((r->left == NULL) && (p->x < r->x)) {
  97.         r->left = p;
  98.     }
  99.     else if ((r->left != NULL) && (p->x < r->x)) {
  100.         ubaci(r->left, p);
  101.     }
  102. }
  103.  
  104. void inOrder(struct cvor* root){
  105.     if (root == NULL){
  106.         return;
  107.     }
  108.     else{
  109.         inOrder(root->left);
  110.         printf("%d", root->x);
  111.         inOrder(root->right);
  112.     }
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top