Advertisement
Guest User

main.cpp

a guest
Oct 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct nodeType{
  5. int data;
  6. nodeType *link;
  7.  
  8. };
  9.  
  10. int main()
  11. {
  12. nodeType *head, *current, *newNode;
  13. head = NULL;
  14. newNode = new nodeType;
  15.  
  16. //set data value of node pointed by newNode to value 4.
  17. cout << "Visual for myself to follow. " << endl;
  18. newNode->data = 4;
  19. cout << newNode->data << endl;
  20.  
  21. //set link to point to NULL
  22. newNode->link = NULL;
  23. cout << newNode->link << endl;
  24.  
  25. //point the pointer head and current to the node pointed by newNode
  26. head = newNode;
  27. current = newNode; // end of 1st node.
  28.  
  29. //statement to print out value of 4 using the current pointer.
  30. cout << current->data << endl;
  31.  
  32. //creating the 2nd node
  33. newNode = new nodeType;
  34. cout << "\n\nThis is the 2nd node. " << endl;
  35. newNode->data = 9;
  36. cout << newNode->data << endl;
  37.  
  38. newNode->link = NULL;
  39. cout << newNode->link << endl;
  40.  
  41. head->link = newNode;
  42.  
  43. current->link = newNode;
  44. current = current->link;
  45. cout << current->data << endl; // end of 2nd node
  46.  
  47. //3rd node
  48. newNode = new nodeType;
  49. cout << "\n\nThis is the 3rd node. " << endl;
  50. newNode->data = 1;
  51. cout << newNode->data << endl;
  52. newNode->link = NULL;
  53. cout << newNode->link << endl;
  54. current->link = newNode;
  55. current = current->link;
  56.  
  57. head->link->link = newNode;
  58.  
  59. cout << head->link->link->data << endl;
  60. //end of the 3rd node.
  61.  
  62. //4th node
  63. newNode = new nodeType;
  64. cout << "\n\nThis is the 4th node. " << endl;
  65. newNode->data = 3;
  66. cout << newNode->data << endl;
  67. newNode->link = NULL;
  68. cout << newNode->link << endl;
  69. current->link = newNode;
  70. current = current->link;
  71.  
  72. cout << current->data << endl;
  73.  
  74. //reseting the current pointer
  75.  
  76. current = head;
  77. cout << "\n\nThis will be a loop to traverse through my link-list. " << endl;
  78. while (current!= NULL)
  79. {
  80. cout << current->data << " ";
  81. current = current->link;
  82. }
  83.  
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement