fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int data;
  5. Node* next;
  6. };
  7. Node* head = 0;
  8. void InsertFirst(int item)
  9. {
  10. Node* nn = new Node();
  11. (*nn).data = item;
  12.  
  13. nn->next = head;
  14.  
  15. head = nn;
  16. }
  17. void InsertLast(int item)
  18. {
  19. if(head == 0) {
  20. InsertFirst(item);
  21. return;
  22. }
  23.  
  24. Node* nn = new Node;
  25. nn->data = item;
  26. nn->next = 0;
  27.  
  28. Node* ptr = head;
  29. while(ptr->next != 0)
  30. {
  31. ptr = ptr->next;
  32. }
  33.  
  34. ptr->next = nn;
  35. }
  36.  
  37. void Display()
  38. {
  39. Node* ptr = head;
  40.  
  41. while(ptr != 0)
  42. {
  43. cout << ptr->data << " ";
  44. ptr = ptr->next;
  45. }
  46. cout << endl;
  47. }
  48. int main(){
  49.  
  50. // InsertFirst(5);
  51. // Display();
  52. // InsertFirst(15);
  53. // InsertFirst(50);
  54. // InsertFirst(21);
  55.  
  56. InsertLast(123);
  57. InsertLast(22);
  58. InsertLast(3);
  59.  
  60. Display();
  61. return 0;
  62. }
  63.  
  64.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
123 22 3