Advertisement
anhlocpr

queue

May 6th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class node{
  6. public:
  7. int value;
  8. node *next;
  9.  
  10. node(int value){
  11. this->value = value;
  12. this->next = NULL;
  13. }
  14. };
  15.  
  16. class queue{
  17. public:
  18. node *head;
  19. node *tail;
  20. int count;
  21.  
  22. queue(){
  23. this->head = NULL;
  24. this->tail = NULL;
  25. this->count = 0;
  26. }
  27.  
  28. bool is_empty(){
  29. return !this->count;
  30. }
  31.  
  32. int push(node *n){
  33. if(this->tail == NULL)
  34. this->head = n;
  35. else
  36. this->tail->next = n;
  37. this->tail = n;
  38. this->count +=1;
  39. return 0;
  40. }
  41.  
  42. node * pop(){
  43. node *n = this->head;
  44. this->head = n->next;
  45. this->count -=1;
  46. if(this->head == NULL)
  47. this->tail = NULL;
  48. return n;
  49. }
  50. };
  51.  
  52. int main(int argc, char const *argv[])
  53. {
  54. queue *q = new queue();
  55. node *n1 = new node(3);
  56. q->push(n1);
  57. node *n2 = q->pop();
  58. cout<<n2->value;
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement