Code:
#include <iostream>
using namespace std;
struct nodeType
{
int info;
nodeType * link;
};
int main()
{
nodeType * first, * last, *newNode, * current ,*previous;
first = last = newNode = NULL;
cout << "Building a linked list: " << endl;
newNode = new nodeType; //create a first node
newNode -> info = 5; //first node = 5;
newNode -> link = NULL; //make the first link to NULL first
cout << "My first node is " <<newNode -> info << endl; //print the first node = 5
first = last = newNode; //ensure pointer first and last is in first node
newNode = new nodeType; //create a second node
newNode -> info = 15; //second node = 15;
newNode -> link = NULL; //make the second link to NULL first
cout << "My second node is " <<newNode -> info << endl; //print the second node = 15
last->link = newNode; //link to the second node from the first node
last = newNode; //set last point to the second node
newNode = new nodeType; //create the third node
newNode -> info = 10; //third node = 10;
newNode -> link = NULL; //make the third link to NULL first
cout << "My third node is " <<newNode -> info << endl; //print the third node =1 0
last->link = newNode; //link to the third node from the second node
last = newNode;
newNode = new nodeType; //create a fourth node
newNode -> info = 22; //fourth node = 22;
newNode -> link = NULL; //make the fourth node link to NULL first
cout << "My fourth node is " <<newNode -> info << endl; //print the fourth node
last->link = newNode;
last = newNode;
cout << "End of building a linked list: " << endl; //end of building a linked list
//Start of Transvering
cout << "Transvering " << endl; //start to transverse
current = first; //set current pointer to the first node
cout << "Transvering through the first node" << endl;
cout << current-> info << endl; //5
cout << "Transvering thorugh the second node" << endl;
current = first; //set current back to first node
current = current->link ; //set current pointer to the second node
cout << current -> info << endl; //15
cout << "Transvering through the third node" << endl;
current = first; //set current back to first node
current = current-> link->link; //set current pointer to third node
cout << current -> info << endl; //10
cout << "Transvering through the fourth node" << endl;
current = first; //set current back to first node
current = current-> link->link->link; //set current pointer to fourth node
cout << current -> info << endl; //22
cout << "Transvering through the last node" << endl; //trying to transverse after the fourth node, accessing after from the fourth node
if (current == last)
{
cout << endl;
cout << "No transvering allowed in fourth node: " << endl; //error would be given since there are only 4 nodes
}
else
{
cout << current -> info << endl; //would not print and go through this else statement since it is not possible
}
cout << endl;
cout << "Transvering back to the first node: " << endl; //back to the first node
cout << endl;
current = first; //set current back to the first node
cout << current -> info << endl; //print , 5
//Deleting third node
previous = first->link; // Previous points to second node
current = previous->link; // Current points to third node
previous->link = current->link; // 2nd node link pointer to link to the 4th node
delete current; // Delete the third node pointing
//Print after deleting using transverse
current = first; //set the current back to the first node
cout <<"New first node is " << current -> info << endl; //print the first node, 5
current = first; //set current back to the first node
current = current ->link; //set current pointing to the second node
cout << "new Second node is " << current -> info << endl; //print the second node, , 15
current = first; //set current pointing back to the first node
current = current ->link->link; //set current pointing to the fourth node since third node has been deleted
cout << "New Third node is " << current -> info << endl; //print 22 instead 0f 10
cout << "Deletion of the third node is successfully." << endl;
//Insertion new node between first node and third node
//newNode = new nodeType; //create a new node
//newNode -> info = 33; //first node = 33;
//newNode -> link = NULL; //make the first link to NULL first
//cout << "My new node is " <<newNode -> info << endl; //print the first node = 33
//previous = first;
//current = current->link;
//previous -> link = newNode;
//newNode = current;
//cout << current -> info << endl;
//cout << current -> info << endl;
//cout << current -> info << endl;
system ("pause");
return 0;
}
I managed to build a single linked list but completely unsure how to implement in double linked list. Is the pointer that I wrote enough for transverse. Maybe i add another current pointer to transverse but do I need other point just to transverse.