Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include "queue.h"
  2.  
  3. Queue_ptr createQueue() {
  4. Queue_ptr s = (Queue_ptr)malloc(sizeof(Queue));
  5. s->front = NULL;
  6. s->back = NULL;
  7. return s;
  8. }
  9.  
  10. void enqueue(Queue_ptr s, element e) {
  11. DN_ptr n = (DN_ptr)malloc(sizeof(DLLNode));
  12. n->e = e;
  13. if (s->front == NULL) {
  14. n->next = NULL;
  15. s->front = n;
  16. s->back = n;
  17. }else{
  18. s->back->next = n;
  19. n->prev = s->back;
  20. s->back = n;
  21. }
  22. }
  23.  
  24. element dequeue(Queue_ptr s) {
  25. DN_ptr temp = s->front->next;
  26. element e = s->front->e;
  27. free(s->front);
  28. s->front = temp;
  29. s->front->next = NULL;
  30. s->front->prev = NULL;
  31. return e;
  32. }
  33.  
  34. int isEmpty(Queue_ptr s) {
  35. if (s->front == NULL)
  36. return 1;
  37. else
  38. return 0;
  39. }
  40.  
  41. element peek(Queue_ptr s) {
  42. return s->front->e;
  43. }
  44.  
  45. void display(Queue_ptr s) {
  46. DN_ptr temp = s->back;
  47. while (temp) {
  48. printf("%dn", temp->e);
  49. temp = temp->prev;
  50. }
  51. }
  52.  
  53. void destroyQueue(Queue_ptr s) {
  54. DN_ptr temp = s->front;
  55. DN_ptr next;
  56. while (temp) {
  57. next = temp->next;
  58. free(temp);
  59. temp = next;
  60. }
  61. free(s);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement