fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node {
  5. int value;
  6. node *next;
  7. };
  8.  
  9. int main() {
  10. node *head = new node;
  11. head->next = NULL;
  12. node *last = NULL;
  13.  
  14. int choice, n;
  15. cout << "Enter 1 to enqueue, 2 to dequeue, 0 to exit: ";
  16. while(1) {
  17. cin >> choice;
  18. if(choice == 1) {
  19. cout << "Enter the num: ";
  20. cin >> n;
  21. node *p = new node;
  22. p->value = n;
  23. p->next = NULL;
  24. if(last != NULL) {
  25. last->next = p;
  26. last = p;
  27. }
  28. else {
  29. head->next = p;
  30. last = p;
  31. }
  32. }
  33. else if(choice == 2) {
  34. if(head->next == NULL) {
  35. cout << "List is empty!" << endl;
  36. }
  37. else {
  38. cout << "Dequeued num: " << head->next->value << endl;
  39. head->next = head->next->next;
  40. if(head->next == NULL) {
  41. last = NULL;
  42. }
  43. }
  44. }
  45. else {
  46. break;
  47. }
  48. }
  49. return 0;
  50. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Enter 1 to enqueue, 2 to dequeue, 0 to exit: