#1
It should always be int main not void main, people tend to get picky about this one here.
#2
Code:
newNode = new nodeType;
newNode -> info = 5;
newNode -> link = NULL;
....
newNode = new nodeType;
newNode -> info = 15;
newNode -> link = NULL;
...
newNode = new nodeType;
newNode -> info = 10;
newNode -> link = NULL;
...
newNode = new nodeType;
newNode -> info = 22;
newNode -> link = NULL;
Create a constructor for your nodeType struct to help out and reduce some of that code:
Code:
struct nodeType
{
int info;
nodeType * link;
nodeType(int value) : info(value), link(NULL) {}
};
The above code then becomes:
Code:
newNode = new nodeType(5);
....
newNode = new nodeType(15);
...
newNode = new nodeType(10);
...
newNode = new nodeType(22);
#3
Code:
if (current = last)
Typo? It should be == not =
#4
Code:
//Deleting third node
cout << "Deleting third node from the link list" << endl;
current = first;
previous = first;
previous ->link = previous ->link -> link;
current = previous ->link;
previous -> link = current->link;
delete current;
The lines that unravel everything here are the ones in red above. For the first one, by repointing previous->link you are also affecting current->link and first->link since they are all the same node in your list. That one line destroys your list since you've lost any pointer you had to the second node in the list - the one that contains the number 15. For the second one, you then make first's link pointer point to the last node in the list before deleting the third node (current). The end result is that you end up with a list of two elements - 5 and 22.
If you want to delete the third node, then move previous, not previous->link:
Code:
previous = first->link; // Previous points to 2nd node
current = previous->link; // Current points to 3rd node
previous->link = current->link; // 2nd node's link pointer reset to skip to the 4th node
delete current; // Delete the third node
However, that said I would create functions to both insert a node at the end of the existing list and to delete a given node.
As a side note, certain operations, such as deletion of a node, are easier when your lists are doubly linked instead of singly linked. You are also manually managing your first/last pointers, this should be the job of a list class.