jaredec18

Untitled

Sep 22nd, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. rlinked.h:
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. // Structure for Node in linked list
  7. struct Node {
  8. int value = 0; // variable for Data
  9. Node* next = NULL; // pointer to the next node
  10. };
  11.  
  12. // Function for inserting a node to a linked list
  13. // in the last by recursion.
  14. void insertRecur(Node** head, int value);
  15.  
  16. rlinked.cpp:
  17.  
  18. #include <iostream>
  19. #include "rlinked.h"
  20.  
  21. using namespace std;
  22.  
  23. // Function
  24. // Name: insertRecur
  25. // Parameters:
  26. // Node** head - pointer to the head pointer of
  27. // the Linked list
  28. //
  29. //int value - data for the new node
  30. //
  31. // Return: Void
  32. //
  33. // the function will traverse the linked list
  34. // till the last node and create a new node with
  35. // the given data and sttach it at last.
  36. //
  37. void insertRecur(Node** head, int value)
  38. {
  39. // check whether the head node is created
  40. // if not, create a new node and attach it.
  41. if((*head) == NULL)
  42. {
  43. (*head) = new Node();
  44. (*head)->value = value;
  45. (*head)->next = NULL;
  46. return;
  47. }
  48.  
  49. // check if the next node is null
  50. if((*head)->next == NULL)
  51. {
  52. //if it is null, create a new node.
  53. Node* temp = new Node();
  54. temp->value = value;
  55.  
  56. // assign the value to the new node
  57. temp->next = NULL;
  58.  
  59. // attaching the new node to the tail
  60. // of the list
  61. (*head)->next = temp;
  62. return;
  63. }
  64. else
  65. {
  66. // if the next node is not null
  67. // call the function recursively
  68. // passing the next pointer
  69. // and value.
  70. insertRecur(&((*head)->next),value);
  71. }
  72. }
  73.  
  74. main.cpp:
  75.  
  76. #include <iostream>
  77. #include "rlinked.h"
  78.  
  79. using namespace std;
  80.  
  81. int main()
  82. {
  83. // ls is the head node of the LinkedList
  84. // Initializing the head node, ls to NULL
  85. Node* ls = NULL;
  86.  
  87. //Inserting values to the Linked List
  88. insertRecur(&ls,1);
  89. insertRecur(&ls,2);
  90. insertRecur(&ls,3);
  91. insertRecur(&ls,4);
  92. insertRecur(&ls,5);
  93. insertRecur(&ls,-2);
  94.  
  95. // current is a node pointer
  96. // It will point to the head node, ls
  97. Node* current = ls;
  98.  
  99. // current will be an alias to the head pointer, ls
  100. // changing the value of current will not affect the
  101. // head pointer
  102.  
  103. cout<<"Linked List: "<<endl;
  104.  
  105. // Traversing through Linked list
  106. // to print the Linked List
  107. while(current != NULL)
  108. {
  109. cout<<current->value<<" ";
  110. current = current->next;
  111. }
  112.  
  113. cout<<endl;
  114.  
  115. return 1;
  116. }
  117.  
  118. Output:
  119.  
  120. $ g++ rlinked.h rlinked.cpp main.cpp
  121.  
  122. $ ./a.exe
  123.  
  124. Linked List:
  125. 1 2 3 4 5 -2
Advertisement
Add Comment
Please, Sign In to add comment