Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define ROZMIAR 5
- struct fifo_node
- {
- int data;
- struct fifo_node *next;
- };
- struct fifo_pointers
- {
- struct fifo_node *head, *tail;
- }fifo;
- void enqueue(struct fifo_pointers *fifo, int data) ///dodawanie elementu do kolejki ( na koniec - ogon)
- {
- struct fifo_node *new_node = (struct fifo_node *)malloc(sizeof(struct fifo_node));
- if(new_node)
- {
- new_node -> data = data;
- new_node -> next = NULL;
- if(fifo -> head == NULL)
- fifo->head=fifo->tail=new_node;
- else {
- fifo->tail->next = new_node ;
- fifo->tail = new_node;
- }
- }
- else
- printf("Nowy element nie zostal utworzony");
- }
- int dequeue(struct fifo_pointers *fifo) ///usuwanie elementu z kolejki ( z poczatku - glowa)
- {
- if(fifo->head)
- {
- struct fifo_node *tmp = fifo->head->next;
- int data = fifo->head->data;
- free(fifo->head);
- fifo->head = tmp;
- if(tmp == NULL)
- fifo->tail= NULL;
- return data;
- }
- return -1;
- }
- int show(struct fifo_pointers fifo)
- {
- if(fifo.head)
- {
- printf(" %d ", fifo.head->data);
- fifo.head = fifo.head->next;
- show(fifo);
- }
- else
- return 0;
- }
- int main ()
- {
- int i;
- for(i=1;i<=5;i++)
- enqueue(&fifo,i);
- printf(" Elementy w kolejka(rekurencja): \t");
- show( fifo);
- printf("\n Elementy usuniete: \t");
- while(fifo.head)
- printf(" %d ", dequeue(&fifo));
- puts(" ");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement