Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // STACK
- int* stack = (int*)malloc(10 * sizeof(int));
- int size = 10;
- int sp = -1;
- bool isempty() {
- return (sp == -1);
- }
- bool isfull() {
- return (sp == size - 1);
- }
- void push(int x) {
- if (isfull()) {
- printf("Error!");
- }
- else {
- sp++;
- stack[sp] = x;
- }
- }
- void pop() {
- if (isempty()) {
- printf("Error!");
- }
- else {
- printf("%d", stack[sp]);
- sp--;
- }
- }
- void peek() {
- if (!isempty()) {
- printf("%d", stack[sp]);
- }
- }
- void clear() {
- while (!isempty()) {
- pop();
- }
- }
- // LINKED LIST
- struct cvor {
- int x;
- cvor *next;
- };
- void dodajnakraj(struct cvor* p, int x) {
- struct cvor* novi;
- while (p->next != NULL) {
- p = p->next;
- }
- novi->x = x;
- p->next = novi;
- novi->next = NULL;
- }
- void dodajnapocetak(struct cvor* p, int x) {
- struct cvor* novi;
- novi->x = x;
- novi->next = p;
- p = novi;
- }
- void dodajnapoziciju(struct cvor* p, int n, int x) {
- struct cvor* prethodni = NULL;
- struct cvor* novi;
- if (n == 1) {
- novi->x = x;
- novi->next = p;
- p = novi;
- }
- else {
- for (int i = 2; i < n; i++) {
- prethodni = p;
- p = p->next;
- }
- novi->x = x;
- prethodni->next = novi;
- novi->next = p;
- // p = novi;
- }
- }
- void izbrisikraj(struct cvor* p) {
- struct cvor* prethodni = NULL;
- while (p->next != NULL) {
- prethodni = p;
- p = p->next;
- }
- prethodni->next = NULL;
- free(p);
- }
- void izbacipoziciju(struct cvor* p, int n) {
- struct cvor* prethodni = NULL;
- for (int i = 1; i < n; i++) {
- prethodni = p;
- p = p->next;
- }
- prethodni->next = NULL;
- free(p);
- }
- void trazenjesredine(struct cvor* p) {
- struct cvor* slow = p;
- struct cvor* fast = p;
- int n = 0;
- if (p != NULL) {
- while (fast != NULL && slow != NULL) {
- fast = fast->next->next;
- slow = slow->next;
- }
- }
- printf("%d", slow->x);
- }
- void ispis(struct cvor* p) {
- while (p != NULL) {
- printf("%d", p->next);
- p = p->next;
- }
- }
- void sortiraj(struct cvor* p) {
- struct cvor* temp1;
- struct cvor* temp2;
- int temp = 0;
- for (temp1 = p; temp1 != NULL; temp1 = temp1->next) {
- for (temp2 = temp1->next; temp2 != NULL; temp2 = temp2->next) {
- if (temp1->x > temp2->x) {
- temp = temp1->x;
- temp1->x = temp2->x;
- temp2->x = temp;
- }
- }
- }
- }
- void provjeri_sortiranost(struct cvor* p) {
- int count = 1;
- int brojac = 0;
- int usporedba = 0;
- while (p != NULL) {
- if (brojac == 0) {
- usporedba = p->x;
- p = p->next;
- }
- else {
- if (p->x<usporedba) {
- std::cout << "Nije sortiran" << std::endl;
- break;
- }
- usporedba = p->x;
- p = p->next;
- }
- brojac++;
- }
- std::cout << "\n";
- }
- // FIRST IN, FIRST OUT (FIFO) - LINKED LIST
- struct Node{
- int data;
- struct Node* next;
- };
- struct Node* front = NULL;
- struct Node* rear = NULL;
- void Enqueue(int x) {
- struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
- temp->data = x;
- temp->next = NULL;
- if (front == NULL && rear == NULL){
- front = rear = temp;
- return;
- }
- rear->next = temp;
- rear = temp;
- }
- void Dequeue() {
- struct Node* temp = front;
- if (front == NULL) {
- printf("Queue is Empty\n");
- return;
- }
- if (front == rear) {
- front = rear = NULL;
- }
- else {
- front = front->next;
- }
- free(temp);
- }
- bool IsEmpty() {
- return front == NULL && rear == NULL;
- }
- bool IsFull() {
- return rear->next != NULL;
- }
- // REKURZIJE
- int fact(int n) {
- if (n == 1) {
- return 1;
- }
- return n*fact(n - 1);
- }
- int fibonacci(int n) {
- if (n == 0) {
- return 1;
- }
- if (n == 1) {
- return 1;
- }
- return fibonacci(n - 1) + fibonacci(n - 2);
- }
- int countDigits(int num)
- {
- static int count = 0;
- if (num > 0)
- {
- count++;
- countDigits(num / 10);
- }
- else
- {
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement