Tobiahao

Kolokwium_07

Apr 26th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. /*
  2. Wypożyczalnia samochodów id i data w kolejce
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #define CAR_NAME_SIZE 20
  9.  
  10. struct queue_node{
  11.     unsigned int id;
  12.     char make[CAR_NAME_SIZE];
  13.     struct queue_node *next;
  14. };
  15.  
  16. struct queue_pointers{
  17.     struct queue_node *head, *tail;
  18. } fifo;
  19.  
  20. void enqueue(struct queue_pointers *fifo, unsigned int id, char *make)
  21. {
  22.     struct queue_node *new_node = (struct queue_node*)malloc(sizeof(struct queue_node));
  23.     if(new_node){
  24.         new_node->id = id;
  25.         strncpy(new_node->make, make, CAR_NAME_SIZE);
  26.         new_node->next = NULL;
  27.         if(fifo->head == NULL)
  28.             fifo->head = fifo->tail = new_node;
  29.         else{
  30.             fifo->tail->next = new_node;
  31.             fifo->tail = new_node;
  32.         }
  33.     }
  34. }
  35.  
  36. int dequeue(struct queue_pointers *fifo)
  37. {
  38.     if(fifo->head){
  39.         struct queue_node *tmp = fifo->head->next;
  40.         int tmp_id = fifo->head->id;
  41.         free(fifo->head);
  42.         fifo->head = tmp;
  43.         if(tmp == NULL)
  44.             fifo->tail = NULL;
  45.         return tmp_id;
  46.     }
  47.     return -1;
  48. }
  49.  
  50. void print_queue(struct queue_pointers fifo)
  51. {
  52.     while(fifo.head){
  53.         printf("%u: %s\n", fifo.head->id, fifo.head->make);
  54.         fifo.head = fifo.head->next;
  55.     }
  56.     printf("\n");
  57. }
  58.  
  59. int main(void)
  60. {
  61.     puts("Lista dostepnych samochodow:");
  62.     enqueue(&fifo, 1, "Samochod 1");
  63.     enqueue(&fifo, 2, "Samochod 2");
  64.     enqueue(&fifo, 3, "Samochod 3");
  65.     print_queue(fifo);
  66.     puts("Wypozyczam pierwszy samochod:");
  67.     dequeue(&fifo);
  68.     print_queue(fifo);
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment