Advertisement
aisp

IZLAZNI KOLOKVIJ - 16.02.2018.

Jun 12th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. // 1. zadatak
  2. int* stack = (int*)malloc(10 * sizeof(int));
  3. int size = 10;
  4. int sp = -1;
  5.  
  6. bool IsEmpty() { return (sp == -1); }
  7.  
  8. bool IsFull() { return (sp == size - 1); }
  9.  
  10. void Push(int x) {
  11.     if (IsFull()) {
  12.         size += 10;
  13.         stack = (int*)malloc(size * sizeof(int));
  14.     }
  15.     sp++;
  16.     stack[sp] = x;
  17. }
  18.  
  19. int Pop() {
  20.     if (IsEmpty()) {
  21.         printf("Stack is empty!");
  22.         return -1;
  23.     }
  24.     int x = stack[sp];
  25.     sp--;
  26.    
  27.     return x;
  28. }
  29.  
  30. int main(void) {
  31.     char arr[] = { '1', '15', '*', '3', '+', '1', '4', '*', '*' };
  32.     for (int i = 0; i < 9; i++)  {
  33.         if (arr[i] == '+') {
  34.             Push(Pop() + Pop());
  35.         }
  36.         else if (arr[i] == '*') {
  37.             Push(Pop() * Pop());
  38.         }
  39.         else {
  40.             Push(arr[i]);
  41.         }
  42.     }
  43.     return 0;
  44. }
  45.  
  46. // 2. zadatak
  47. struct Node
  48. {
  49.     int data;
  50.     struct Node* next;
  51. };
  52.  
  53. void newList(struct Node *p1, struct Node *p2, int n) {
  54.     int count = n;
  55.     struct Node* p1_new = (struct Node*)malloc(sizeof(struct Node));
  56.     struct Node* p2_new = (struct Node*)malloc(sizeof(struct Node));
  57.  
  58.     while (count-- > 0) {
  59.         p1_new->data = p1->data;
  60.         p2_new->data = p2->data;
  61.         p1 = p1->next;
  62.         p2 = p2->next;
  63.         p1_new->next = (struct Node*)malloc(sizeof(struct Node));
  64.         p1_new = p1_new->next;
  65.         p2_new->next = (struct Node*)malloc(sizeof(struct Node));
  66.         p2_new = p1_new->next;
  67.     }
  68.  
  69.     while (p1->next != NULL && p2->next != NULL) {
  70.         p1_new->data = p2->data;
  71.         p2_new->data = p1->data;
  72.         p1 = p1->next;
  73.         p2 = p2->next;
  74.         p1_new->next = (struct Node*)malloc(sizeof(struct Node));
  75.         p1_new = p1_new->next;
  76.         p2_new->next = (struct Node*)malloc(sizeof(struct Node));
  77.         p2_new = p1_new->next;
  78.     }
  79.  
  80.     p->next = NULL;
  81.  
  82.     for (int i = 0; i < n; i++) {
  83.         p1 = p1->next;
  84.         p2 = p2->next;
  85.     }
  86.  
  87.     struct Node* temp = p1;
  88.     p1->next = p2->next;
  89.     p2->next = temp->next;
  90.     free(temp);
  91. }
  92.  
  93. // 3. zadatak
  94. struct Node{
  95.     int data;
  96.     struct Node* next;
  97. };
  98.  
  99. struct Node* front = NULL;
  100. struct Node* rear = NULL;
  101.  
  102. void Enqueue(int x) {
  103.     struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
  104.     temp->data = x;
  105.     temp->next = NULL;
  106.     if (front == NULL && rear == NULL){
  107.         front = rear = temp;
  108.         return;
  109.     }
  110.     rear->next = temp;
  111.     rear = temp;
  112. }
  113.  
  114. void Dequeue() {
  115.     struct Node* temp = front;
  116.     if (front == NULL) {
  117.         printf("Queue is Empty\n");
  118.         return;
  119.     }
  120.     if (front == rear) {
  121.         front = rear = NULL;
  122.     }
  123.     else {
  124.         front = front->next;
  125.     }
  126.     free(temp);
  127. }
  128.  
  129. bool IsEmpty() {
  130.     return front == NULL && rear == NULL;
  131. }
  132.  
  133. bool IsFull() {
  134.     return rear->next != NULL;
  135. }
  136.  
  137. // 4. zadatak
  138. struct cvor {
  139.     int x;
  140.     struct cvor *left, *right;
  141. };
  142.  
  143. void ubaci(struct cvor *r, struct cvor *p) {
  144.     if ((r->right == NULL) && (p->x > r->x)) {
  145.         r->right = p;
  146.     }
  147.     else if ((r->right != NULL) && (p->x > r->x)) {
  148.         ubaci(r->right, p);
  149.     }
  150.     if ((r->left == NULL) && (p->x < r->x)) {
  151.         r->left = p;
  152.     }
  153.     else if ((r->left != NULL) && (p->x < r->x)) {
  154.         ubaci(r->left, p);
  155.     }
  156. }
  157.  
  158. void preorderIterative(Node *root)
  159. {
  160.     push(root);
  161.  
  162.     while (!empty())
  163.     {
  164.         Node *curr = pop();
  165.         cout << curr->data << " ";
  166.  
  167.         if (curr->right) { push(curr->right); }
  168.         if (curr->left) { push(curr->left); }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement