fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class node{
  5. public:
  6. int data;
  7. node *next;
  8.  
  9. node(int data)
  10. {
  11. this->data=data;
  12. next=NULL;
  13. }
  14. };
  15.  
  16. node * takeinput1(){
  17. int data;
  18. cin>>data;
  19.  
  20. node* head=NULL;
  21.  
  22. while(data!=-1)
  23. {
  24. node *n=new node(data);//dynammically creating node so that mem doesnt deallocate
  25. if(head==NULL){
  26. head=n;
  27. }
  28.  
  29. else{
  30. node *temp=head;
  31. while(temp->next!=NULL)
  32. {
  33. temp=temp->next;
  34. }
  35.  
  36. temp->next=n;
  37. }
  38.  
  39. cin>>data;
  40.  
  41.  
  42. }
  43.  
  44. }
  45.  
  46.  
  47. void print(node *head){
  48.  
  49. node *temp=head;
  50.  
  51. while(temp!=NULL)
  52. {
  53. cout<<endl;
  54. cout<<temp->data;
  55. temp=temp->next;
  56. }
  57. }
  58.  
  59. int main() {
  60.  
  61. node* head=takeinput1();
  62.  
  63.  
  64. cout<<"\n Printing linked list using print function";
  65. print(head);
  66.  
  67. return 0;
  68. }
Success #stdin #stdout 0s 5504KB
stdin
Standard input is empty
stdout
 Printing linked list using print function
1