Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. /*
  2. Lab 9 - Question 1
  3. Uriel Diaz
  4.  
  5. This program creates a linked list with a few
  6. nodes and then displays them.
  7. */
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. struct nodeType
  13. {
  14. int data;
  15. nodeType *link;
  16. };
  17.  
  18. int main()
  19. {
  20. nodeType *head, *current, *newNode;
  21.  
  22. head = NULL;
  23. newNode = new nodeType;
  24.  
  25. // First Node
  26. newNode->data = 4;
  27. newNode->link = NULL;
  28. head = newNode; /* Chains the new node to head*/
  29.  
  30. current = head;
  31. cout << "First node data: " << current->data;
  32.  
  33. // Second Node
  34. newNode = new nodeType;
  35. newNode->data = 9;
  36. newNode->link = NULL;
  37. head->link = newNode;
  38.  
  39. current = current->link; /* Makes *current point to the next node*/
  40. cout << "\nSecond node data: " << current->data;
  41.  
  42. // Third Node
  43. newNode = new nodeType;
  44. newNode->data = 1;
  45. newNode->link = NULL;
  46. head->link->link = newNode;
  47.  
  48. current = current->link;
  49. cout << "\nThird node data: " << current->data;
  50.  
  51. // Fourth Node
  52. newNode = new nodeType;
  53. newNode->data = 3;
  54. newNode->link = NULL;
  55. head->link->link->link = newNode;
  56.  
  57. current = current->link;
  58. cout << "\nFourth node data: " << current->data;
  59.  
  60. // Resets the current pointer back to the first node
  61. current = head;
  62. cout << "\n\nAll nodes: ";
  63. while (current != NULL)
  64. {
  65. cout << current->data << " ";
  66. current = current->link;
  67. }
  68.  
  69. cout << "\n\n";
  70. system("PAUSE");
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement