Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define ROZMIAR 5
  6.  
  7. struct fifo_node
  8. {
  9. char data;
  10. struct fifo_node *next;
  11. };
  12.  
  13. struct fifo_pointers
  14. {
  15. struct fifo_node *head, *tail;
  16. }fifo;
  17.  
  18. void enqueue(struct fifo_pointers *fifo, char data) ///dodawanie elementu do kolejki ( na koniec - ogon)
  19. {
  20. struct fifo_node *new_node = (struct fifo_node *)malloc(sizeof(struct fifo_node));
  21. if(new_node)
  22. {
  23. new_node -> data = data;
  24. new_node -> next = NULL;
  25. if(fifo -> head == NULL)
  26. fifo->head=fifo->tail=new_node;
  27. else {
  28. fifo->tail->next = new_node ;
  29. fifo->tail = new_node;
  30. }
  31. }
  32. else
  33. printf("Nowy element nie zostal utworzony");
  34.  
  35. }
  36.  
  37. int dequeue(struct fifo_pointers *fifo) ///usuwanie elementu z kolejki ( z poczatku - glowa)
  38. {
  39. if(fifo->head)
  40. {
  41. struct fifo_node *tmp = fifo->head->next;
  42. char data = fifo->head->data;
  43. free(fifo->head);
  44. fifo->head = tmp;
  45. if(tmp == NULL)
  46. fifo->tail= NULL;
  47. return data;
  48.  
  49. }
  50. return -1;
  51. }
  52.  
  53. int main ()
  54. {
  55. char znak;
  56. printf("Wpisz zdanie/slowo do kolejki: \t");
  57. do
  58. {
  59. scanf("%c", &znak);
  60. enqueue(&fifo,znak);
  61.  
  62. }while(znak != '\n');
  63.  
  64. printf("Elementy w kolejce: \t");
  65. while(fifo.head)
  66. printf(" %c", dequeue(&fifo));
  67. puts(" ");
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement