Advertisement
eniodordan

[AISP] ŠALABAHTER

Jan 28th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. // STACK
  2.  
  3. int* stack = (int*)malloc(10 * sizeof(int));
  4. int size = 10;
  5. int sp = -1;
  6.  
  7. bool isempty() {
  8.     return (sp == -1);
  9. }
  10.  
  11. bool isfull() {
  12.     return (sp == size - 1);
  13. }
  14.  
  15. void push(int x) {
  16.     if (isfull()) {
  17.         printf("Error!");
  18.     }
  19.     else {
  20.         sp++;
  21.         stack[sp] = x;
  22.     }
  23. }
  24.  
  25. void pop() {
  26.     if (isempty()) {
  27.         printf("Error!");
  28.     }
  29.     else {
  30.         printf("%d", stack[sp]);
  31.         sp--;
  32.     }
  33. }
  34.  
  35. void peek() {
  36.     if (!isempty()) {
  37.         printf("%d", stack[sp]);
  38.     }
  39. }
  40.  
  41. void clear() {
  42.     while (!isempty()) {
  43.         pop();
  44.     }
  45. }
  46.  
  47. // LINKED LIST
  48.  
  49. struct cvor {
  50.     int x;
  51.     cvor *next;
  52. };
  53.  
  54. void dodajnakraj(struct cvor* p, int x) {
  55.     struct cvor* novi;
  56.  
  57.     while (p->next != NULL) {
  58.         p = p->next;
  59.     }
  60.  
  61.     novi->x = x;
  62.     p->next = novi;
  63.     novi->next = NULL;
  64. }
  65.  
  66. void dodajnapocetak(struct cvor* p, int x) {
  67.     struct cvor* novi;
  68.  
  69.     novi->x = x;
  70.     novi->next = p;
  71.     p = novi;
  72. }
  73.  
  74. void dodajnapoziciju(struct cvor* p, int n, int x) {
  75.     struct cvor* prethodni = NULL;
  76.     struct cvor* novi;
  77.  
  78.     if (n == 1) {
  79.         novi->x = x;
  80.         novi->next = p;
  81.         p = novi;
  82.     }
  83.     else {
  84.         for (int i = 2; i < n; i++) {
  85.             prethodni = p;
  86.             p = p->next;
  87.         }
  88.  
  89.         novi->x = x;
  90.         prethodni->next = novi;
  91.         novi->next = p;
  92.         // p = novi;
  93.     }
  94. }
  95.  
  96. void izbrisikraj(struct cvor* p) {
  97.     struct cvor* prethodni = NULL;
  98.  
  99.     while (p->next != NULL) {
  100.         prethodni = p;
  101.         p = p->next;
  102.     }
  103.    
  104.     prethodni->next = NULL;
  105.     free(p);
  106. }
  107.  
  108. void izbacipoziciju(struct cvor* p, int n) {
  109.     struct cvor* prethodni = NULL;
  110.  
  111.     for (int i = 1; i < n; i++) {
  112.         prethodni = p;
  113.         p = p->next;
  114.     }
  115.  
  116.     prethodni->next = NULL;
  117.     free(p);
  118. }
  119.  
  120. void trazenjesredine(struct cvor* p) {
  121.     struct cvor* slow = p;
  122.     struct cvor* fast = p;
  123.     int n = 0;
  124.  
  125.     if (p != NULL) {
  126.         while (fast != NULL && slow != NULL) {
  127.             fast = fast->next->next;
  128.             slow = slow->next;
  129.         }
  130.     }
  131.  
  132.     printf("%d", slow->x);
  133. }
  134.  
  135. void ispis(struct cvor* p) {
  136.     while (p != NULL) {
  137.         printf("%d", p->next);
  138.         p = p->next;
  139.     }
  140. }
  141.  
  142. void sortiraj(struct cvor* p) {
  143.     struct cvor* temp1;
  144.     struct cvor* temp2;
  145.     int temp = 0;
  146.  
  147.     for (temp1 = p; temp1 != NULL; temp1 = temp1->next) {
  148.         for (temp2 = temp1->next; temp2 != NULL; temp2 = temp2->next) {
  149.             if (temp1->x > temp2->x) {
  150.                 temp = temp1->x;
  151.                 temp1->x = temp2->x;
  152.                 temp2->x = temp;
  153.             }
  154.         }
  155.     }
  156. }
  157.  
  158. void provjeri_sortiranost(struct cvor* p) {
  159.     int count = 1;
  160.     int brojac = 0;
  161.     int usporedba = 0;
  162.  
  163.     while (p != NULL) {
  164.         if (brojac == 0) {
  165.             usporedba = p->x;
  166.             p = p->next;
  167.         }
  168.         else {
  169.             if (p->x<usporedba) {
  170.                 std::cout << "Nije sortiran" << std::endl;
  171.                 break;
  172.             }
  173.             usporedba = p->x;
  174.             p = p->next;
  175.         }
  176.         brojac++;
  177.     }
  178.     std::cout << "\n";
  179. }
  180.  
  181. // FIRST IN, FIRST OUT (FIFO) - LINKED LIST
  182. struct Node{
  183.     int data;
  184.     struct Node* next;
  185. };
  186.  
  187. struct Node* front = NULL;
  188. struct Node* rear = NULL;
  189.  
  190. void Enqueue(int x) {
  191.     struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
  192.     temp->data = x;
  193.     temp->next = NULL;
  194.     if (front == NULL && rear == NULL){
  195.         front = rear = temp;
  196.         return;
  197.     }
  198.     rear->next = temp;
  199.     rear = temp;
  200. }
  201.  
  202. void Dequeue() {
  203.     struct Node* temp = front;
  204.     if (front == NULL) {
  205.         printf("Queue is Empty\n");
  206.         return;
  207.     }
  208.     if (front == rear) {
  209.         front = rear = NULL;
  210.     }
  211.     else {
  212.         front = front->next;
  213.     }
  214.     free(temp);
  215. }
  216.  
  217. bool IsEmpty() {
  218.     return front == NULL && rear == NULL;
  219. }
  220.  
  221. bool IsFull() {
  222.     return rear->next != NULL;
  223. }
  224.  
  225. // REKURZIJE
  226.  
  227. int fact(int n) {
  228.     if (n == 1) {
  229.         return 1;
  230.     }
  231.  
  232.     return n*fact(n - 1);
  233. }
  234.  
  235. int fibonacci(int n) {
  236.     if (n == 0) {
  237.         return 1;
  238.     }
  239.     if (n == 1) {
  240.         return 1;
  241.     }
  242.  
  243.     return fibonacci(n - 1) + fibonacci(n - 2);
  244. }
  245.  
  246. int countDigits(int num)
  247. {
  248.     static int count = 0;
  249.     if (num > 0)
  250.     {
  251.         count++;
  252.         countDigits(num / 10);
  253.     }
  254.     else
  255.     {
  256.         return count;
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement