fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5. int data;
  6. Node* pNext;
  7. };
  8.  
  9. struct MyQueue {
  10. Node* pHead = nullptr;
  11. Node* pTail = nullptr;
  12.  
  13. void enqueue(Node* pNew);
  14. Node* dequeue();
  15. };
  16.  
  17. int main() {
  18.  
  19. // Declaration
  20. MyQueue mQueue;
  21.  
  22. // Enqueue operation
  23. {
  24. Node* mData = new Node;
  25. mData->data = 100000;
  26. mData->pNext = nullptr;
  27. mQueue.enqueue(mData);
  28. }
  29.  
  30. // Dequeue operation
  31. {
  32. Node* mData = mQueue.dequeue();
  33. cout<<mData->data;
  34. }
  35.  
  36. return 0;
  37. }
  38.  
  39. void MyQueue::enqueue(Node* pNew) {
  40. if(!pHead) {
  41. pHead = pNew;
  42. pTail = pNew;
  43. pHead->pNext = nullptr;
  44. } else {
  45. pTail->pNext = pNew;
  46. pTail = pTail->pNext;
  47. }
  48. }
  49.  
  50. Node* MyQueue::dequeue() {
  51. if(!pHead) return nullptr;
  52. Node* tmp = pHead;
  53. pHead = pHead->pNext;
  54. if(!pHead) pTail = nullptr;
  55. return tmp;
  56. }
Success #stdin #stdout 0.01s 5544KB
stdin
Standard input is empty
stdout
100000