fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node { int value; node *next; };
  5. struct queue { node *head,*tail; };
  6.  
  7. void add(queue &Q,int value)
  8. {
  9. node *tmp=new node;
  10. tmp->value=value;
  11. tmp->next=0;
  12. Q.tail=(Q.tail?Q.tail->next:Q.head)=tmp;
  13. }
  14.  
  15. void show(const queue &Q)
  16. {
  17. for(node *tmp=Q.head;tmp;tmp=tmp->next) cout<<tmp->value<<" -> ";
  18. cout<<endl;
  19. }
  20.  
  21. bool del(queue &Q,int &value)
  22. {
  23. if(!Q.head) return false;
  24. node *tmp=Q.head;
  25. Q.head=tmp->next;
  26. if(!Q.head) Q.tail=0;
  27. value=tmp->value;
  28. delete tmp;
  29. return true;
  30. }
  31.  
  32. int main()
  33. {
  34. queue Q={0,0};
  35. add(Q,10);
  36. add(Q,15);
  37. add(Q,20);
  38. add(Q,25);
  39. add(Q,30);
  40. show(Q);
  41. int n;
  42. del(Q,n); cout<<n<<endl;
  43. del(Q,n); cout<<n<<endl;
  44. show(Q);
  45. return 0;
  46. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
10 -> 15 -> 20 -> 25 -> 30 -> 
10
15
20 -> 25 -> 30 ->