Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct nodeType{
- int data;
- nodeType *link;
- };
- int main()
- {
- nodeType *head, *current, *newNode;
- head = NULL;
- newNode = new nodeType;
- //set data value of node pointed by newNode to value 4.
- cout << "Visual for myself to follow. " << endl;
- newNode->data = 4;
- cout << newNode->data << endl;
- //set link to point to NULL
- newNode->link = NULL;
- cout << newNode->link << endl;
- //point the pointer head and current to the node pointed by newNode
- head = newNode;
- current = newNode; // end of 1st node.
- //statement to print out value of 4 using the current pointer.
- cout << current->data << endl;
- //creating the 2nd node
- newNode = new nodeType;
- cout << "\n\nThis is the 2nd node. " << endl;
- newNode->data = 9;
- cout << newNode->data << endl;
- newNode->link = NULL;
- cout << newNode->link << endl;
- head->link = newNode;
- current->link = newNode;
- current = current->link;
- cout << current->data << endl; // end of 2nd node
- //3rd node
- newNode = new nodeType;
- cout << "\n\nThis is the 3rd node. " << endl;
- newNode->data = 1;
- cout << newNode->data << endl;
- newNode->link = NULL;
- cout << newNode->link << endl;
- current->link = newNode;
- current = current->link;
- head->link->link = newNode;
- cout << head->link->link->data << endl;
- //end of the 3rd node.
- //4th node
- newNode = new nodeType;
- cout << "\n\nThis is the 4th node. " << endl;
- newNode->data = 3;
- cout << newNode->data << endl;
- newNode->link = NULL;
- cout << newNode->link << endl;
- current->link = newNode;
- current = current->link;
- cout << current->data << endl;
- //reseting the current pointer
- current = head;
- cout << "\n\nThis will be a loop to traverse through my link-list. " << endl;
- while (current!= NULL)
- {
- cout << current->data << " ";
- current = current->link;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement