Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct node{
  4.     node *next;
  5.     int val;
  6. };
  7. struct queue{
  8.     node *push;
  9.     node *pop;
  10. };
  11. void push_queue(queue &Q, int val)
  12. {
  13.     node *p = new node;
  14.     p->val =val;
  15.     p->next=Q.push;
  16.     Q.push=p;
  17. }
  18. int pop_queue(queue &Q)
  19. {
  20.     if(Q.pop == nullptr){
  21.         while(Q.push != nullptr){
  22.             node *p = new node;
  23.             p->val=Q.push->val;
  24.             p->next=Q.pop;
  25.             Q.pop=p;
  26.             node *tmp=Q.push;
  27.             Q.push=Q.push->next;
  28.             delete tmp;
  29.         }
  30.         if(Q.pop == nullptr){cout<<"error"; return -1;}
  31.     }
  32.     int res=Q.pop->val;
  33.     node *tmp =Q.pop;
  34.     Q.pop=Q.pop->next;
  35.     delete tmp;
  36.     return res;
  37. }
  38. void init_queue(queue &Q)
  39. {
  40.     Q.push=nullptr;
  41.     Q.pop=nullptr;
  42. }
  43. int main()
  44. {
  45.     queue Q;
  46.     init_queue(Q);
  47.     push_queue(Q,2);
  48.     cout<<pop_queue(Q);
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement