/* Queue a.k.a. FIFO http://en.wikipedia.org/wiki/Queue (c) BruXy -- http://bruxy.regnet.cz/fel/ */ #include #include /********************* * Globalni promenne * *********************/ typedef struct stack{ int data; struct stack *odkaz; } STACK; /********************* * Funkce * *********************/ STACK *create(void){ STACK *test; test = ( (STACK *) malloc(sizeof(STACK)) ); if(test==NULL){ printf("Malo pameti!\n"); return NULL; } test->odkaz=NULL; return test; } /* Vrazi cislo do zasobniku a zvetsi ho o jeden prvek */ short int qprazdna(STACK *queue_top) { if(queue_top == NULL){ printf("Fronta je prazdna!\n"); return 0; } else return 1; } STACK *push(int cislo) { STACK *queue_new; /* Vytvori novy konec */ queue_new = create(); /* Do minuleho konce zapise */ queue_new->data = cislo; return queue_new; } /* Vymaze prvni cislo ve fronte a nastavi novou adresu cela fronty */ STACK *pop(STACK *queue_top) { STACK *queue_delete; if(qprazdna(queue_top) != 0){ queue_delete = queue_top; free(queue_delete); return queue_top->odkaz; } else { return NULL; } } void print_data(STACK *queue_top){ while(queue_top != NULL) { printf(" %6d -> %p \n", queue_top->data, (void *) queue_top->odkaz); queue_top = queue_top->odkaz; } } void free_data(STACK *queue_top){ while(queue_top != NULL) { queue_top = pop(queue_top); } } /********************* * Main * *********************/ int main(){ int cislo, size; int citac = 0; STACK *s_start, *s_current, *s_temp; printf("Kolik prvku ma obsahovat fronta?: "); scanf("%d", &size); /* Ukazatel na zacatek */ s_start = NULL; for(;;) { if(citac == size){ break; } printf("Zadej cislo %d. : ", citac+1); scanf("%d", &cislo); printf("Ukladam %d na adresu %p.\n", cislo, (void *) s_current); s_temp = push(cislo); if(!citac){ s_start = s_current = s_temp; } else{ s_current->odkaz=s_temp; s_current = s_temp; } printf("Addr -> %p.\n", (void *) s_current); putchar('\n'); citac++; } /* Vypsani obsahu cele fronty */ printf("Vypis naplnenych dat, od adresy: %p\n", (void *) s_start); print_data(s_start); printf("\nKolik mam vymazat prvku?:"); scanf("%d", &cislo); citac = 0; for(;;) { if(citac == cislo) break; s_start = pop(s_start); if(s_start == NULL) break; citac++; } print_data(s_start); /* Na zaver po sobe uklidime */ free_data(s_start); return 0; }