Advertisement
gha890826

期末考預告-1

Jun 10th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class node
  6. {
  7. public:
  8.     int num;
  9.     node *next=NULL;
  10. };
  11.  
  12. class queue
  13. {
  14. public:
  15.     queue(){ cout << "***head= " << head << endl; }
  16.     void push(int);
  17.     void pop();
  18. private:
  19.     node *head = new(node);
  20. };
  21.  
  22. void queue::push(int n)
  23. {
  24.     node *pos=head;
  25.     while (pos->next!=NULL)
  26.     {
  27.         pos = pos->next;
  28.     }
  29.     pos->next = new(node);
  30.     pos->next->num = n;
  31.     cout << "***push " << n << " to " << pos->next << endl;
  32. }
  33.  
  34. void queue::pop()
  35. {
  36.     if (head->next == NULL)
  37.     {
  38.         cout << "empty queue!\n";
  39.         return;
  40.     }
  41.     node *del=head->next;
  42.     head->next = head->next->next;
  43.     cout << "pop " << del->num <<" frome "<< del << endl;
  44.     delete(del);
  45. }
  46.  
  47.  
  48. void main()
  49. {
  50.     cout << "new qu1\n";
  51.     queue qu1;
  52.     cout << "start push\n";
  53.     qu1.push(1);
  54.     qu1.push(2);
  55.     qu1.push(3);
  56.     qu1.push(4);
  57.     cout << "start pop\n";
  58.     qu1.pop();
  59.     qu1.pop();
  60.     qu1.pop();
  61.     qu1.pop();
  62.     qu1.pop();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement