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);
          }

Dani AI

Generated

Brief summary: the goal was to move the head node to the tail (1->2->3 becomes 2->3->1). had the right high‑level idea: with both first and last available there is no need to walk the list. The bug came from the traversal + wrong assignment order in the original routine, which ended up making the former tail the new head and leaving the old head chained after it.

Why the original produced "3 1 2": starting at first = 1 the loop stopped with current at node 2; current->link = NULL cut the list into 1->2->NULL; last->link = first made 3->1; then setting first = last (and last = current) made the printed sequence start at 3, so printing yielded 3,1,2. The correct sequence of pointer updates must preserve the remaining list before the moved node is attached to the tail.

Practical checklist and cautions:

  • Do nothing for an empty list or a single-node list.
  • Detach the original head (store it), advance first to the next node, then attach the detached node after last.
  • Ensure the detached node’s link is set to NULL and update last to point to it.
  • Maintain the invariant last->link == NULL after every operation.
  • printList need not take first by reference; use a non‑modifying pointer parameter.
  • For modern C++, prefer nullptr (or std::list/smart pointers) in production.

Quick validation: run the operation on 0, 1, 2 and N-element lists and assert that list length is unchanged, first advances (when size>1), last is correct, and last->link==NULL. The simple paper walkthrough that suggested is often the fastest way to spot an ordering mistake.

Recommended Answers

All 4 Replies

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.