Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------------------------
- //Pretrazivanje
- void sekvencijalno(int *polje, int a, int n)
- {
- int pronadeno = 0;
- for (int i = 0; i < n; i++)
- {
- if (polje[i] == a)
- {
- printf("Pronadeno");
- pronadeno = 1;
- }
- }
- if (pronadeno == 0)
- {
- printf("Broj nije pronaden");
- }
- }
- void binarno(int* polje, int a, int n)
- {
- int dg, gg, s, f;
- dg = 0;
- gg = n - 1;
- s = (dg + gg) / 2;
- f = 0;
- while (dg <= gg) {
- if (polje[s] == a)
- {
- printf("Pronađen");
- f = 1; break;
- }
- else if (polje[s] < a)
- {
- dg = s + 1;
- }
- else if (polje[s] < a)
- {
- gg = s - 1;
- }
- s = (dg + gg) / 2;
- }
- if (f == 0) {
- printf("Nije pronaden");
- }
- }
- // Sortiranja
- void zamjena(int *a, int *b)
- {
- int t = *a;
- *a = *b;
- *b = t;
- }
- void bubblesort(int *polje, int n)
- {
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n - i - 1; j++) {
- if (polje[j] > polje[j + 1])
- zamjena(&polje[j], &polje[j + 1]);
- }
- }
- }
- void selectionsort(int *polje, int n)
- {
- int i, j, minimum;
- for (i = 0; i < n; i++) {
- minimum = i;
- for (j = i + 1; j < n; j++) {
- if (polje[j] < polje[minimum]) {
- minimum = j;
- }
- }
- zamjena(&polje[i], &polje[minimum]);
- }
- }
- ---------------------------------------------------------------------------------
- //Povezani popisi
- struct element {
- int x;
- struct element *next;
- };
- struct element* lista(int n)
- {
- struct element *p = NULL;
- struct element *glava = NULL;
- for (int i = 0; i < n; i++)
- {
- if (i == 0)
- {
- glava = ((struct element*)malloc(sizeof(struct element)));
- p = glava;
- }
- else
- {
- p->next = ((struct element*)malloc(sizeof(struct element)));
- p = p->next;
- }
- scanf_s("%d", &p->x);
- }
- p->next = NULL;
- return glava;
- }
- void ispis(struct element* glava)
- {
- struct element *p = glava;
- while (p != NULL)
- {
- printf("%d", p->x);
- p = p->next;
- }
- }
- void ubacinakraj(struct element *glava, int n)
- {
- struct element* trenutni = glava;
- struct element* novi = NULL;
- while (trenutni->next != NULL)
- {
- trenutni = trenutni->next;
- }
- novi = ((struct element*)malloc(sizeof(struct element)));
- novi->x = n;
- novi->next = NULL;
- trenutni->next = novi;
- }
- void ubacinapoziciju(struct element*glava, int n, int poz)
- {
- struct element* novi = NULL;
- struct element* lijevi = NULL;
- struct element* desni = glava;
- for (int i = 1; i < poz; i++)
- {
- lijevi = desni;
- desni = desni->next;
- }
- novi = ((struct element*)malloc(sizeof(struct element)));
- novi->x = n;
- lijevi->next = novi;
- lijevi = novi;
- lijevi->next = desni;
- }
- void izbacizadnji(struct element*glava)
- {
- struct element *p = glava;
- struct element *prethodni = NULL;
- while (p->next != NULL)
- {
- prethodni = p;
- p = p->next;
- }
- prethodni->next = NULL;
- free(p);
- }
- void izbacipoziciju(struct element*glava, int poz)
- {
- struct element *p = glava;
- struct element *prethodni = NULL;
- for (int i = 1; i < poz; i++)
- {
- prethodni = p;
- p = p->next;
- }
- prethodni->next = p->next;
- free(p);
- }
- int sort(struct element* glava)
- {
- struct element* p1 = (struct element*)malloc(sizeof(struct element));
- struct element* p2 = (struct element*)malloc(sizeof(struct element));
- int t = 0;
- for (p1 = glava; p1 != NULL; p1->next++) {
- for (p2 = p1->next; p2 != NULL; p2->next++) {
- if (p1->next > p2->next)
- {
- t = p1->next;
- p1->next = p2->next;
- p2->next = t;
- }
- }
- }
- }
- void provjerasorta(struct element*glava)
- {
- int br = 0;
- int usporedba = 0;
- struct element *p = glava;
- while (p != NULL) {
- if (br == 0) {
- usporedba = p->x;
- p = p->next;
- }
- else {
- if (p->x < usporedba)
- printf("nije");
- break;
- }
- usporedba = p->x;
- p = p->next;
- }
- }
- ---------------------------------------------------------------------------------
- //Stogovi
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX 5
- int stack_point = -1;
- int stog[5]= { 1,2,3,4, 5};
- int isEmpty() {
- if (stack_point == -1)
- return 1;
- else
- return 0;
- }
- int isFull() {
- if (stack_point == MAX)
- return 1;
- else
- return 0;
- }
- void push(int a)
- {
- if (isFull())
- printf("Pun stog");
- else {
- stack_point += 1;
- stog[stack_point] = a;
- }
- }
- int pop() {
- int rez = -1;
- if (isEmpty()) {
- printf("Prazan");
- }
- else {
- rez = stog[stack_point];
- stack_point--;
- }
- return rez;
- }
- void peek()
- {
- if (!isEmpty())
- printf("%d", stog[stack_point]);
- }
- ---------------------------------------------------------------------------------
- //Stabla
- struct cvor {
- int x;
- struct cvor *lijevi;
- struct cvor *desni;
- };
- void ubaci(struct cvor* novi, struct cvor* roditelj)
- {
- if ((roditelj->desni == NULL) && (novi->x > roditelj->x)) {
- roditelj->desni = novi;
- }
- else if ((roditelj->desni != NULL)) && (novi->x > roditelj->x)){
- ubaci(roditelj->desni, novi);
- }
- if ((roditelj->lijevi == NULL) && (novi->x < roditelj->x)) {
- roditelj->lijevi = novi;
- }
- else if ((roditelj->lijevi != NULL) && (novi->x < roditelj->x)) {
- ubaci(roditelj->lijevi, novi);
- }
- }
- int prd(struct cvor* korijen)
- {
- if (korijen == 0)return 1;
- else {
- printf("%d", korijen->x);
- prd(korijen->desni);
- prd(korijen->lijevi);
- }
- }
- int iod(struct cvor* korijen)
- {
- if (korijen == 0)return 1;
- else {
- iod(korijen->lijevi);
- printf("%d", korijen->x);
- iod(korijen->desni);
- }
- }
- int pod(struct cvor* korijen)
- {
- if (korijen == 0)return 1;
- else {
- pod(korijen->lijevi);
- pod(korijen->desni);
- printf("%d", korijen->x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement