Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstddef>
  3. using namespace std;
  4.  
  5. class Node
  6. {
  7. public:
  8. int data;
  9. Node *next;
  10. Node(int d){
  11. data=d;
  12. next=NULL;
  13. }
  14. };
  15.  
  16. class Solution{
  17. public:
  18.  
  19. Node* insert(Node *head,int data)
  20. {
  21. Node* newhead = new Node(data);
  22. if(head != NULL) {
  23. Node *current = head;
  24. while(current->next != NULL) {
  25. current = current->next;
  26. }
  27. current->next = newhead;
  28. return head;
  29. } else {
  30. return newhead;
  31. }
  32. }
  33.  
  34. void display(Node *head)
  35. {
  36. Node *start=head;
  37. while(start)
  38. {
  39. cout<<start->data<<" ";
  40. start=start->next;
  41. }
  42. }
  43. };
  44.  
  45. int main()
  46. {
  47. Node* head=NULL;
  48. Solution mylist;
  49. int T,data;
  50. cin>>T;
  51. while(T-->0){
  52. cin>>data;
  53. head=mylist.insert(head,data);
  54. }
  55. mylist.display(head);
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement