/* autor: Krzysztof 'frimer' Zmijewski
 *
 * prog: " kolejka.c " - Program zapisuje/zdejmuje liczby do kolejki z uzyciem tablicy
 *			tzw. kolejka FIFO
 * 
 * Kompilator: gcc 4.3.3
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */

#include <stdio.h>
#include <ctype.h>

const int rozmiarKolejki = 5;  	/* rozmiar kolejki */
int liczbaElementow = 0; 		/* poczatkowa ilosc elemetow */

/* wstawienie elementu do kolejki */
int enqueue(int kolejka[], int nowy) {  
	
	if(liczbaElementow < rozmiarKolejki) {	
		kolejka[liczbaElementow] = nowy;		/* dodaj do kolejki */
		liczbaElementow++;
	
	
	} else if (full(kolejka)==0) {	/* kolejka jest juz pelna */
			printf("przepelnienie kolejki!!! \n");
	} 
}

/* pobranie elementu z kolejki */
int dequeue(int kolejka[]) {
	
	if(liczbaElementow != 0) {		/* zdejmij z kolejki */
		kolejka[liczbaElementow+1] =0;  /* zdjety element zerujemy */
		liczbaElementow--;
	} else if (empty(kolejka)==1) {	/* kolejka jest juz pusta */
			printf("kolejka jest pusta!!! \n");
	}
}

/* sprawdzamy czy kolejka jest pusta */
int empty(int kolejka[]) {

	if(liczbaElementow == 0) {  /* jesli elementy na stosie sa rowne 0 to: */
		return 1; /* pusty */
	} else {	
		return 0; /* pelny */
	}
}

/* sprawdzamy czy kolejka jest pelna */
int full(int kolejka[]) {

	if(liczbaElementow == 0) { /* jesli caly stos jest pelny to: */
		return 1; /* pusty */
	} else {	
		return 0; /* pelny */
	}
}

/* wyswietlamy kolejke */
int wyswietl(int kolejka[]) {
int j;

	for(j=0; j<liczbaElementow; j++) { /* wyswietlamy kazdy element tablicy */
		printf("Elementy znajdujace sie w kolejce: %d ", kolejka[j]);
	}
	puts("\n");
return;
}

int main(void) {
int err, i, ch, kolejka[rozmiarKolejki];
char opcja;

system("clear");	/* instrukcja systemowa czyszczaca ekran */

	while(1) {	
		printf("\n\t\tMENU:\n");
		printf("'=' - by dodac element do kolejki\n");
		printf("'-' - by zdjac element do kolejki\n");
		printf("'w' - by wyswietla kolejkie\n");
		printf("'q' - wyjscie z programu\n\nPodaj opcje: ");
		
			scanf("%s", &opcja);	
		switch(opcja) {
	case '=':	/* opcja dodawania elementu */
		printf("podaj liczbe: ");
			err=scanf("%d", &ch);
		if (err==1) {	/* warunek sprawdza czy scanf zwrocil wartosc 1 czyli ze pobral liczbe */
			enqueue(kolejka, ch);
			break;
		} else if (err==0) {	/* jezeli scanf pobral znak nastapi warunkowe zakonczenie programu */
				printf("Wpisano znak... Nastapilo wyjscie z programu !!!\n\n");
			return 0;	
		}
	case '-':		/* opcja zdejmowania elementu */
		dequeue(kolejka);
			break;
	case 'w':	/* opcja wyswietlania kolejki */
		wyswietl(kolejka);
			break;
	case 'q':	/* opcja wyjscia z programu */
		printf("\n\tBye...\n\n");
		return 0;
		}
	}

	for(i=0; i<rozmiarKolejki; i++) /* tworzymy kolejkie o rozmiarze "rozmiarKolejki" o stalym rozmiarze */
		kolejka[i] =0;	/* zerujemy kazdy element tablicy (kolejki) */
	
return 0;
}

