Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define max 5
- typedef struct red{int niz[max]; int f; int r;}RED;
- int isFull(RED* bafer)
- {
- return (bafer->r+1)%max== bafer->f;
- }
- int isEmpty(RED*bafer)
- {
- return bafer->f==-1;
- }
- int dodaj(RED*bafer , int podatak)
- {
- if (isFull(bafer)) return 0; //printf("Bafer je pun");
- if (bafer->f==-1) bafer->f=0; //bafer->f++;
- bafer->r=(bafer->r+1)%max;
- bafer->niz[bafer->r]=podatak;
- return 1;
- }
- int brisi(RED*bafer, int *pom) //skidanje sa vrha
- {
- if (isEmpty(bafer)) return 0;
- *pom=bafer->niz[bafer->f];
- if (bafer->f == bafer->r)
- bafer->f=bafer->r=-1;
- else bafer->f=(bafer->f+1)%max;
- return 1;
- }
- int pisi(RED *bafer) {
- if (isEmpty(bafer))
- return 0;
- int i = bafer->f, j=0;
- if (bafer->f <= bafer->r)
- {
- while (i <= bafer->r)
- printf("%d. : %d\n ", ++j, bafer->niz[i++]);
- }
- else
- {
- while (i <= max - 1)
- printf("%d. : %d\n ", ++j, bafer->niz[i++]);
- i = 0;
- while (i <= bafer->r)
- printf("%d. :%d\n ", ++j, bafer->niz[i++]);
- }
- return 1;
- }
- //
- typedef struct cvor{ int podatak; struct cvor* sledeci;}CVOR;
- void push(CVOR** tos, int podatak)
- {
- CVOR* novi=(CVOR*)malloc(sizeof(CVOR));
- novi->podatak=podatak;
- novi->sledeci=*tos;
- *tos=novi;
- }
- int pop(CVOR **tos, int *broj) {
- if (*tos == 0) return 0;
- CVOR *p = *tos;
- *broj = p->podatak;
- *tos = p->sledeci;
- free(p);
- return 1;
- }
- void pisi2(CVOR** tos)
- {
- int broj;
- while(pop(tos,&broj))
- printf("%d\n", broj);
- }
- int main()
- {
- RED bafer; bafer.f = bafer.r = -1;
- char c;
- int broj;
- CVOR* tos=0;
- do {
- printf("===================================================\n");
- printf("Dodavanje[D], brisanje [B], prikaz [P], kraj [0]? ");
- scanf("\n%c", &c);
- if (c == 'D') {
- printf(" Unesite broj: ");
- scanf("%d", &broj);
- if (dodaj(&bafer, broj) == 0) printf(" Bafer je pun!\n");
- else push(&tos,broj);
- }
- else if (c == 'B') {
- if (brisi(&bafer, &broj)){
- printf(" Obrisan je broj: %d\n", broj);
- }
- else printf(" Bafer je prazan!\n");
- }
- else if (c == 'P') {
- printf(" Sadrzaj bafera: ");
- if (pisi(&bafer) == 0)
- printf("\n Bafer je prazan!\n");
- else printf("\n");
- }
- else if (c != '0') printf(" Nepoznata opcija - '%c'.\n", c);
- } while (c != '0');
- printf("\nSadrzaj je:\n");
- while(pop(&tos,&broj))
- printf("%d ", broj);
- printf("KRAJ!\n"); return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement