Creative Commons License Foxbond's Repo

/**(c) 2015 Michał (Foxbond) Chraniuk */
#include <stdio.h>

typedef struct element {
	int lp;
	char nazwisko[20];
	struct element *prev;
	struct element *next;
}element;

int main() {
	int lp = 1;
	char opt;
	element *queue = NULL;
	element *temp = NULL;

	printf("Kolejka FIFO, nacisnij dowolny klawisz aby kontynuowac");


	do {
		system("cls");
		printf("1. Wyswietl kolejke\n2. Dodaj element\n3. Pobierz element\n0. Zakoncz\n");
		opt = getch();
		system("cls");
		if (opt == '1') {
			if (queue == NULL) {
				printf("Brak elementow\n");
			}
			else {
				while (queue->prev != NULL) {
					queue = queue->prev;
				}

				for (;;) {
					printf("%d\t%s\n", queue->lp, queue->nazwisko);
					if (queue->next == NULL) {
						break;
					}
					queue = queue->next;
				}
			}

			printf("Nacisnij dowolny klawisz aby powrocic");
			getch();
		} else if (opt == '2') {
			if (queue == NULL) {
				queue = (element*)malloc(sizeof(element));
				queue->prev = NULL;
				queue->next = NULL;
				queue->lp = lp++;
				printf("Dodawanie elementu:\nPodaj nazwisko: ");
				scanf("%s", &queue->nazwisko);
				printf("Dodano element nr %d\n", queue->lp);
			} else {
				queue->next = (element*)malloc(sizeof(element));
				if (queue->next == NULL) {
					printf("Brak wolnej pamieci, nie mozna dodac nowego elementu\n");
				} else {
					queue->next->next = NULL;
					queue->next->prev = queue;
					queue = queue->next;
					queue->lp = lp++;
					printf("Dodawanie elementu:\nPodaj nazwisko: ");
					scanf("%s", &queue->nazwisko);
					printf("Dodano element nr %d\n", queue->lp);
				}
			}
			

			printf("Nacisnij dowolny klawisz aby powrocic");
			getch();
		} else if (opt == '3') {
			if (queue == NULL) {
				printf("Brak elementow w kolejce\n");
			} else {
				while (queue->prev != NULL) {
					queue = queue->prev;
				}
				printf("Pobrany element: %d. %s\n", queue->lp, queue->nazwisko);

				if (queue->next == NULL) {
					queue = NULL;
				}
				else {
					queue->next->prev = NULL;
					queue = queue->next;
				}
			}

			printf("Nacisnij dowolny klawisz aby powrocic");
			getch();
		}

		


	} while (opt != '0');


	return 0;
}

> Back