Search code examples
c++ooplinked-listnodessingly-linked-list

Function moving list elements to the end of the list not working multiple times


I need to make a function that moves the nth element in a singly linked list to the end of the list. I created some code that does that but it only works once if I try to do it again it moves the selected element to the end but the one that was moved previously gets deleted/dissapears. My theory is that it doesnt actually change the tail reference. so im stuck right now!

void move(int n)
    {
        if (head == NULL || head->next == NULL)
        {
            return;
        }
        node *first = head;
        node *temp =new node;

        for (int i = 1; i < n-1; i++)
        {
            first=first->next;
        }
        temp = first->next;
        first->next=first->next->next;
        temp->next = NULL;

        tail->next = temp;
        tail=temp;
    }

my input: 1 2 3 4 5

after moving the 3rd element for the first time:

1 2 4 5 3

after moving the 3rd element(4) for the 2nd time:

1 2 5 4

but it should be

1 2 5 3 4


Solution

  • I checked your code with my own implementation. Your function move() is working fine. However, you should not be using 'new' in your 8th line of code as highlighted by @molbdnilo and @PaulMakenzie. But it is not responsible for this problem. There is a problem with some other part of your code.

    #include<iostream>
    using namespace std;
    
    class List
    {
        struct Node
        {
            int number;
            Node* next;
        };  
    
        Node* head;
        Node* tail;
    
        public:
        List()
        {
            head = NULL;
            tail = NULL;
        }
            void insert(int num)
        {
            Node* temp = new Node();
            temp->number = num;
            temp->next = NULL;
    
            if (head == NULL)
            {
                head = temp;
                tail = temp;
            }
            else
            {
                Node* point = head;
                while (point->next != NULL)
                    point = point->next;
    
                point->next = temp;
                tail = point->next;
            }
        }
        void display()
        {
            Node* point = head;
            while (point != NULL)
            {
                cout << point->number << " ";
                point = point->next;
            }
        }
    
        void move(int n)
        {
            if (head == NULL || head->next == NULL)
            {
                return;
            }
            Node *first = head;
            Node *temp;
    
            for (int i = 1; i < n-1; i++)
            {
                first=first->next;
            }
            temp = first->next;
            first->next=first->next->next;
            temp->next = NULL;
    
            tail->next = temp;
            tail=temp;
        }
    };
    
    int main()
    {
        List a;
        a.insert(1);
        a.insert(2);
        a.insert(3);
        a.insert(4);
        a.insert(5);
    
        a.move(3);
        a.move(3);
    
        a.display();
    
    }