Advertisement
wendy890711

191018

Oct 18th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class node{
  6. public:
  7. int data;
  8. node*next;
  9. };
  10.  
  11. class queue{
  12. public:
  13. node*head;
  14. queue(){
  15. head = new node;
  16. head->next = NULL;
  17. }
  18. void enqueue(int i);
  19. void dequeue();
  20. };
  21.  
  22. void queue::enqueue(int i){
  23. node*x = new node;
  24. x->next = NULL;
  25. x->data = i;
  26.  
  27. if (head->next == NULL)
  28. {
  29. head->next = x;
  30. return;
  31. }
  32. node*p = new node;
  33. p->next = head;
  34. while (p->next != NULL){
  35. p = p->next;
  36. }
  37. p->next = x;
  38. }
  39.  
  40. void queue::dequeue(){
  41. node*current = head, *pre = NULL;
  42. if (current->next != NULL){
  43. current = current->next;
  44. cout << current->data << endl;
  45. pre = current->next;
  46. current->next = NULL;
  47. head->next = pre;
  48. delete current;
  49. }
  50. else{
  51. cout << "Empty" << endl;
  52. }
  53. }
  54.  
  55. int main()
  56. {
  57. queue s1;
  58. for (int i = 0; i < 11; i++)
  59. s1.enqueue(i);
  60. for (int i = 0; i < 12; i++)
  61. s1.dequeue();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement