Like Tree1Likes

Help with my linked list

This is a discussion on Help with my linked list within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; struct nodeType { int info; nodeType * link; }; void main(void) { nodeType * ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    Help with my linked list

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct nodeType
    {
    	int info;
    	nodeType * link;
    };
    
    void main(void)
    {
    	nodeType * first, * last, *newNode, * current ,*previous;
    	first = last = newNode = NULL;
    
    	//int num;
    
    	first = NULL;
    	last = NULL;
    	cout << "Building a linked list: " << endl;
    
    	newNode = new nodeType;
    	newNode -> info = 5;
    	newNode -> link = NULL;
    	cout << "My first node is " <<newNode -> info << endl;
    
    	first = last = newNode;
    
    	newNode = new nodeType;
    	newNode -> info = 15;
    	newNode -> link = NULL;
    	cout << "My second node is " <<newNode -> info << endl;
    	last->link = newNode;
    	last = newNode;
    
    	newNode = new nodeType;
    	newNode -> info = 10;
    	newNode -> link = NULL;
    	cout << "My third node is " <<newNode -> info << endl;
    	last->link = newNode;
    	last = newNode;
    
    	newNode = new nodeType;
    	newNode -> info = 22;
    	newNode -> link = NULL;
    	cout << "My fourth node is " <<newNode -> info << endl;
    	last->link = newNode;
    	last = newNode;
    	
    	cout << "End of building a linked list: " << endl;
    
    
    	cout << "Transvering " << endl;
    	current = first;
    	
    	cout << "Transvering through the first node" << endl;
    	cout << current-> info << endl;
    
    	cout << "Transvering thorugh the second node" << endl;
    	current = current->link ;
    	cout << current -> info << endl;
    
    	cout << "Transvering through the third node" << endl;
    	current = current-> link;
    	cout << current -> info << endl;
    
    	cout << "Transvering through the fourth node" << endl;
    	current = current-> link;
    	cout << current -> info << endl;
    	
    	cout << "Transvering through the last node" << endl;
    	if (current = last)
    	{
    		cout << "No transvering allowed: " << endl;
    	}
    	else
    	{
    		cout << current -> info << endl;
    	}
    	cout << endl;
    	
    	cout << "Transvering back to the first node: " << endl;
    	current = first;
    	cout << current -> info << endl;
    
    
    	//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;
    
    	current = first;
    	cout << current -> info << endl;
    	current = current ->link->link;
    	cout << current -> info << endl;
    	current = current ->link->link->link;
    	cout << current -> info << endl;
    	
    	system ("pause");
    }
    Is my linked above correct, i got problem deleting nodes , any1 got any idead how should I do it. Anyway I not sure if i miss anything on the linked list i forgot to add.Some1 help me. Thank you. I am trying to delete the third nodes.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    #1
    Code:
    void main(void)
    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.
    iMalc likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Nice help you had given me, thank you. I thought that only class can create the constructor. Not sure that can use constructor in struct.
    Code:
    delete current;
    After deleting the current from the list, can I use the current for pointer again for transving and to print output using the current to link the info or I need to create a new pointer? And what is the cout << ? ; i had to print to ensure the node had deleted. I try it on my code using current ->info but it crash. What is the output I had to print using cout << and print all of the nodes to ensure the nodes has been deleted. And i only want to delete only the third nodes. I do not want to touch the second nodes.
    Last edited by evildotaing; 11-25-2011 at 08:46 AM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    class members default to private, struct members default to public, that about all the difference there is between them.
    Even if you weren't going to use a constructor, you could still write a function to do it and use that
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by evildotaing View Post
    Code:
    delete current;
    After deleting the current from the list, can I use the current for pointer again for transving and to print output using the current to link the info or I need to create a new pointer? And what is the cout << ? ; i had to print to ensure the node had deleted. I try it on my code using current ->info but it crash. What is the output I had to print using cout << and print all of the nodes to ensure the nodes has been deleted. And i only want to delete only the third nodes. I do not want to touch the second nodes.
    "delete current" deletes what current is pointing to. So then "current" points to nothing, as far as you're concerned. If you assign a valid pointer to current, you can carry on using it -- you don't need to actually declare a new pointer.
    Code:
        current = first;
        cout << current -> info << endl;
        current = current ->link->link;
        cout << current -> info << endl;
        current = current ->link->link->link;
        cout << current -> info << endl;
    So at this point the list is
    Code:
    5  -->  15  --> 22
    ^
    |
    current
    
    
        current = current ->link->link;
    
    5  -->  15  --> 22
                    ^
                    |
                current
    
    (current is meant to point to 22, hopefully it displays ok)
    
        current = current ->link->link->link;
    *bang*! What is current->link? It is NULL. What is NULL->link? A crash!

    It does help to draw pictures for this kind of thing. I'd write each number in a box and draw and arrow out of the box to the next number to indicate the link. Then draw arrows to represent "current", "first", "previous" etc. Just do it a step at a time.

    To print all the nodes, you could do:
    Code:
    cout << first->info << endl;
    cout << first->link->info  << endl;
    cout << first->link->link->info  << endl;
    However much better would be to write a function to print all of the nodes, no matter how many there are. You could do this with a loop checking for NULL, or comparing against "last".
    Last edited by smokeyangel; 11-25-2011 at 05:23 PM.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct nodeType
    {
    	int info;
    	nodeType * link;
    };
    
    int main()
    {
    	nodeType * first, * last, *newNode, * current ,*previous;
    	first = last = newNode = NULL;
    
    	//int num;
    
    	first = NULL;
    	last = NULL;
    	cout << "Building a linked list: " << endl;
    
    	newNode = new nodeType;                                  //create a first node
    	newNode -> info = 5;
    	newNode -> link = NULL;
    	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;
    	newNode -> link = NULL;
    	cout << "My second node is " <<newNode -> info << endl;  //print the second node = 15
    	last->link = newNode;
    	last = newNode;
    
    	newNode = new nodeType;                                  //create the third node
    	newNode -> info = 10;
    	newNode -> link = NULL;
    	cout << "My third node is " <<newNode -> info << endl;   //print the third node =10
    	last->link = newNode;
    	last = newNode;
    
    	newNode = new nodeType;                                 //create a fourth node
    	newNode -> info = 22;
    	newNode -> link = NULL;
    	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
    
    
    	cout << "Transvering " << endl;                         //start to transverse
    	current = first;                                        //set current pointer to the first
    	
    	cout << "Transvering through the first node" << endl;   
    	cout << current-> info << endl;                        //5
    
    	cout << "Transvering thorugh the second node" << endl;
    	current = first;
    	current = current->link ;                              //set current pointer to the second node
    	cout << current -> info << endl;                       //15
    
    	cout << "Transvering through the third node" << endl;
    	current = first;
    	current = current-> link->link;                        //set current pointer to third node
    	cout << current -> info << endl;                       //10
    
    	cout << "Transvering through the fourth node" << endl;
    	current = first;
    	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
    	if (current == last)
    	{
    		cout << "No transvering allowed: " << endl;        //error would be givn
    	}
    	else
    	{
    		cout << current -> info << endl;                    //would not print
    	}
    	cout << endl;
    	
    	cout << "Transvering back to the first node: " << endl; //back to the first node
    	current = first;
    	cout << current -> info << endl;
    
    
    	//Deleting third node
    	
    	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
    	current = first;
    	cout << current -> info << endl;
    	current = current ->link->link;
    	cout << current -> info << endl;
    	current = current ->link->link->link;
    	cout << current -> info << endl;
    	system ("pause");
    	return 0;
    }
    This is my latest code. When I try to compile, I received this error "Unhandled exception at 0x00414f0f in assginment 1 das.exe: 0xC0000005: Access violation reading location 0x00000004." causing my program to crash. Did i miss anything in my code and what is exactly wrong. Or is my eletion nodes has some problem or I did not properly create the linked list? And i received this message "error C2018: unknown character '0x60'" if I used the first pointer cout << first->info causing the error.

    The problem is now I am using my cout << current->link to set and cout << current->info to print output just like what i had done while I am doing transvering at my code where there are no error yet. But, after deleting the nodes, then the progrm crash while doing the same thing.
    Last edited by evildotaing; 11-25-2011 at 08:24 PM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    That means you dereferenced a NULL pointer. With the debugger attached it will show you exactly which line the problem is on.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    I dun understand what you saying

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct nodeType
    {
    	int info;
    	nodeType * link;
    };
    
    int main()
    {
    	nodeType * first, * last, *newNode, * current ,*previous;
    	first = last = newNode = NULL;
    
    	//int num;
    
    	first = NULL;
    	last = NULL;
    	cout << "Building a linked list: " << endl;
    
    	newNode = new nodeType;                                  //create a first node
    	newNode -> info = 5;
    	newNode -> link = NULL;
    	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;
    	newNode -> link = NULL;
    	cout << "My second node is " <<newNode -> info << endl;  //print the second node = 15
    	last->link = newNode;
    	last = newNode;
    
    	newNode = new nodeType;                                  //create the third node
    	newNode -> info = 10;
    	newNode -> link = NULL;
    	cout << "My third node is " <<newNode -> info << endl;   //print the third node =10
    	last->link = newNode;
    	last = newNode;
    
    	newNode = new nodeType;                                 //create a fourth node
    	newNode -> info = 22;
    	newNode -> link = NULL;
    	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
    
    
    	cout << "Transvering " << endl;                         //start to transverse
    	current = first;                                        //set current pointer to the first
    	
    	cout << "Transvering through the first node" << endl;   
    	cout << current-> info << endl;                        //5
    
    	cout << "Transvering thorugh the second node" << endl;
    	current = first;
    	current = current->link ;                              //set current pointer to the second node
    	cout << current -> info << endl;                       //15
    
    	cout << "Transvering through the third node" << endl;
    	current = first;
    	current = current-> link->link;                        //set current pointer to third node
    	cout << current -> info << endl;                       //10
    
    	cout << "Transvering through the fourth node" << endl;
    	current = first;
    	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
    	if (current == last)
    	{
    		cout << "No transvering allowed: " << endl;        //error would be givn
    	}
    	else
    	{
    		cout << current -> info << endl;                    //would not print
    	}
    	cout << endl;
    	
    	cout << "Transvering back to the first node: " << endl; //back to the first node
    	current = first;
    	cout << current -> info << endl;
    
    
    	//Deleting third node
    	
    	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
    	current = first;
        cout << current -> info << endl;    //print the first numbers
        current = current ->link->link;
        cout << current -> info << endl;     //print second numbers
        current = current ->link->link->link;
        cout << current -> info << endl;     //print fourth nnumbers
    	system ("pause");
    	return 0;
    }
    My latest code, still not working. What is wrong with it

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    still got problem, anyone help me pls?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by evildotaing View Post
    I dun understand what you saying
    In the meantime while waiting for an answer, I might suggest you learn about Visual Studio's debugger. Essentially, that is what iMalc is trying to tell you. Use a debugger.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Look at these lines VERY closely and remember that your list only has three numbers in it at this point in your code:
    Code:
    current = first;
    cout << current -> info << endl;    //print the first numbers
    current = current ->link->link;
    cout << current -> info << endl;     //print second numbers
    current = current ->link->link->link;
    cout << current -> info << endl;     //print fourth nnumbers
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    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;
    }
    Solve deleting the node. Now I am going to add a node between the first and third node. Do i need to use all of the pointer to do it. I try it on my code but could not work.

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    any1 got any idea how to insert it. Do i need to use previous pointer again just like what I had done for deletion.

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Seriously, i dun know what is the problem on my code. I managed to pint 5 , 33 which 33 si the new node number and if I tried to print the next number in linked list, it crash.
    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
    	
    	current = first;                                        //set current pointer to the first node
    	
    	cout << "Transvering through the first node :" ;   
    	cout << current-> info << endl;                        //5
    
    	cout << "Transvering thorugh the second node :" ;
    	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 :" ;
    	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 :" ;
    	if (current != last)                                        //Prove that the current node is not in last pointer eyt
    	{
    		current = first;									   //set current back to first node
    		current = current-> link->link->link;                  //set current pointer to fourth node
    		cout << current -> info << endl;                       //22
    	}
    	else
    	{
    		cout << "Should not print this " << endl;           //never print
    	}
    		
    	
    	
    	cout << "Transvering through the last node" << endl;   //trying to transverse after the fourth node, accessing after from the fourth node
    	if (current != last)
    	{
    		cout << current -> info << endl;                    //would not print and go through this else statement since it is not possible
    	}
    	else
    	{
    		cout << endl;
    		cout << "No transvering allowed after the fourth node: " << endl;        //error would be given since there are only 4 nodes
    	}
    	cout << endl;
    	
    	
    	cout << "Transvering back to the first node: "; //back to the first node
    	
    	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 old 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->link = current->link;
    	
    	//current = first;
    	//cout << current -> info << endl;
    	
    	//current = first;
    	//current = current -> link;
       // cout << current -> info << endl;
    
    	//current = first;
    	//current = current -> link->link;
        //cout << current -> info << endl;
        
        
    	system ("pause");
    	return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying a linked list into another linked list
    By paranoidgnu in forum C Programming
    Replies: 7
    Last Post: 07-19-2011, 06:21 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 05:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21