i am trying to move the node in the first item from the list and add it to the back of the list

1->2->3 becames 2->3->1

instead i get 3 1 2

#include <iostream>

using namespace std;
struct nodeType
{
    int info;
    nodeType *link;
};

void createList(nodeType*& first, nodeType*& last);

void printList(nodeType*& first);

void moveFront2Back(nodeType*& first, nodeType*& last);


int main()
{
 
    nodeType *first, *last;
    int num;
    
    createList(first, last);
    printList(first);
    moveFront2Back(first,last);


    

    system("PAUSE");
    return 0;
}
void createList(nodeType*& first, nodeType*& last)
{
    int number;
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
    
    cout<<"Enter an integer (-999 to stop): ";
    cin>>number;
    cout<<endl;
    
     while (number != -999)
     {
         newNode = new nodeType; // create new node
         newNode->info = number;
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
         cout<<"Enter an integer (-999 to stop): ";
         cin>>number;
         cout<<endl;
     } // end of while-loop
        
} // end of build list function

void printList(nodeType*& first)
{

    cout<<"Inside printList...printing linked list...\n"<<endl;
        nodeType *current;
        current = first;    
        while (current != NULL)
        {
          cout << current->info<<endl;
          current = current->link;
        }
}   
void moveFront2Back(nodeType*& first, nodeType*& last)
{
     nodeType* current;
     if(first!=NULL)
     {
        if(first->link!=NULL)
        {
          current=first;
          while (current->link!=last)
          current=current->link;
          current->link=NULL;
          last->link=first;
          first=last;
          last=current;
          }
          }
       printList(first);
          }

Well !! I went through your code, it does have a problem. I feel its more simple and i don't find the necessity to run through a loop since you have the pointers for the first and the last one. What can be done is

You have

1->2->3
Store 1 in a temp.
assign 3->1 .
//nullify temp's link.
1->2 is now NULL.
//now assign temp to last.

Hope you get it.

This is my revised code

void moveFront2Back(nodeType*& first, nodeType*& last)
{
     nodeType* current;
     nodeType* temp;
     if(first!=NULL)
     {
        if(first->link!=NULL)
        {
         temp=first;
         last->link=first;
         temp->link=NULL;
         temp=last;
          }
          }
       printList(first);
          }

I just get 1 now

Input :1,2,3
output 1

I said

//now assign temp to last.

You did

temp=last;

You've done it the other way around. Write down an example in a sheet of paper and follow each step. That will help you understand what actually happens.

commented: Pencil and paper are the way to go! +6

Thank you so much
I wrote it down and it made so much sense
Thanks!!!!!!!!!!!!!!!!!!!

void moveFront2Back(nodeType*& first, nodeType*& last)
{
     nodeType* current;
     nodeType* temp;
     if(first!=NULL)
     {
        if(first->link!=NULL)
        {
         temp=first;
         first=first->link;
         last->link=temp;
         temp->link=NULL;
         last=temp;
        
          }
          }
       printList(first);
          }

Thanks man

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.