Advertisement
Tobiahao

Kolokwium_02

Apr 26th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. /*
  2. Dodać do kolejki kartę i jej rodzaj, np. As Serce i zrobić wyświetlanie.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9.  
  10. char *rodzaj[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Joker", "Dama", "Król", "As"};
  11. char *kolor[] = {"Kier", "Karo", "Pik", "Trefl"};
  12.  
  13. struct queue_node{
  14.     char *rodzaj_karty;
  15.     char *kolor_karty;
  16.     struct queue_node *next;
  17. };
  18.  
  19. struct queue_pointers{
  20.     struct queue_node *first, *last;
  21. } fifo;
  22.  
  23. void enqueue(struct queue_pointers *fifo, char rodzaj[], char kolor[])
  24. {
  25.     struct queue_node *new_node = (struct queue_node*)malloc(sizeof(struct queue_node));
  26.     if(new_node){
  27.         new_node->rodzaj_karty = rodzaj;
  28.         new_node->kolor_karty = kolor;
  29.         new_node->next = NULL;
  30.        
  31.         if(fifo->first == NULL)
  32.             fifo->first = fifo->last = new_node;
  33.         else{
  34.             fifo->last->next = new_node;
  35.             fifo->last = new_node;
  36.         }
  37.     }
  38. }
  39.  
  40. void print_queue(struct queue_pointers fifo)
  41. {
  42.     while(fifo.first){
  43.         printf("%s %s\n", fifo.first->rodzaj_karty, fifo.first->kolor_karty);
  44.         fifo.first = fifo.first->next;
  45.     }
  46.     printf("\n");
  47. }
  48.  
  49. int main(void)
  50. {
  51.     srand(time(NULL));
  52.    
  53.     // Dodanie przykladowych kart
  54.     for(int i = 0; i < 10; i++)
  55.         enqueue(&fifo, rodzaj[rand() % 13], kolor[rand() % 4]);
  56.    
  57.     print_queue(fifo);
  58.    
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement