Guest User

Untitled

a guest
Aug 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Single Linked List
  4. //
  5. // Created by Harshit Jindal on 20/08/18.
  6. // Copyright © 2018 Harshit Jindal. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. class Node
  13. {
  14. public:
  15. int data;
  16. Node *next;
  17. };
  18.  
  19. void traverseList(Node *n)
  20. {
  21. while (n != NULL)
  22. {
  23. cout << n -> data << endl;
  24. n = n->next;
  25. }
  26. }
  27.  
  28. int main(int argc, const char * argv[]) {
  29.  
  30. Node* head = NULL;
  31. Node* second = NULL;
  32. Node* third = NULL;
  33.  
  34. head = (Node*)malloc(sizeof(Node));
  35. second = (Node*)malloc(sizeof(Node));
  36. third = (Node*)malloc(sizeof(Node));
  37.  
  38.  
  39. head->data = 50; //assign data in first node
  40. head->next = second; // Link first node with
  41.  
  42. second->data = 2;
  43. second->next = third;
  44.  
  45. third->data = 23;
  46. third->next = NULL;
  47.  
  48.  
  49. traverseList(head);
  50.  
  51. return 0;
  52.  
  53.  
  54. }
Add Comment
Please, Sign In to add comment