Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Lab 9 - Question 1
- Uriel Diaz
- This program creates a linked list with a few
- nodes and then displays them.
- */
- #include <iostream>
- using namespace std;
- struct nodeType
- {
- int data;
- nodeType *link;
- };
- int main()
- {
- nodeType *head, *current, *newNode;
- head = NULL;
- newNode = new nodeType;
- // First Node
- newNode->data = 4;
- newNode->link = NULL;
- head = newNode; /* Chains the new node to head*/
- current = head;
- cout << "First node data: " << current->data;
- // Second Node
- newNode = new nodeType;
- newNode->data = 9;
- newNode->link = NULL;
- head->link = newNode;
- current = current->link; /* Makes *current point to the next node*/
- cout << "\nSecond node data: " << current->data;
- // Third Node
- newNode = new nodeType;
- newNode->data = 1;
- newNode->link = NULL;
- head->link->link = newNode;
- current = current->link;
- cout << "\nThird node data: " << current->data;
- // Fourth Node
- newNode = new nodeType;
- newNode->data = 3;
- newNode->link = NULL;
- head->link->link->link = newNode;
- current = current->link;
- cout << "\nFourth node data: " << current->data;
- // Resets the current pointer back to the first node
- current = head;
- cout << "\n\nAll nodes: ";
- while (current != NULL)
- {
- cout << current->data << " ";
- current = current->link;
- }
- cout << "\n\n";
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement