fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5. int value;
  6. Node* next;
  7. };
  8.  
  9. Node* head = NULL;
  10.  
  11. void insert_element2(int x)
  12. {
  13. Node* temp1 = new Node();
  14. temp1->value = x;
  15. temp1->next = NULL;
  16.  
  17. if (head == NULL)
  18. head = temp1;
  19. else {
  20. Node* temp2 = head;
  21. while(temp2->next != NULL) {
  22. temp2 = temp2->next;
  23. }
  24. temp2->next = temp1;
  25. }
  26. }
  27.  
  28. void print_list(Node* head)
  29. {
  30. Node* temp = head;
  31.  
  32. while (temp != NULL) {
  33. cout << temp->value << ' ';
  34. temp = temp->next;
  35. }
  36.  
  37. cout << endl;
  38. }
  39.  
  40. void mid_list(Node* head)
  41. {
  42. Node* temp1 = head;
  43. Node* temp2 = head;
  44.  
  45. while (temp1->next != NULL) {
  46. temp1 = temp1->next->next;
  47. temp2 = temp2->next;
  48. }
  49.  
  50. cout << temp2->value;
  51. }
  52.  
  53. int main()
  54. {
  55. insert_element2(1);
  56. insert_element2(2);
  57. insert_element2(3);
  58.  
  59. print_list(head);
  60. mid_list(head);
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0s 4540KB
stdin
Standard input is empty
stdout
1 2 3 
2