how to move front node to last node in linked list.

So I`m trying to move my front two nodes to the back of the list. I don`t understand what I am doing wrong here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<string>

struct Node{
    std::string data;
    Node *link;
    Node *next;
};

class Lilist{
    public:
        Lilist(){head = NULL;}
        void add(std::string item);
        bool search(std::string target);
        void move_front_to_back();
        void show(); 
    private:
        Node *head;
};

void Lilist::add(std::string item){
    Node * tmp;
    if(head == NULL){
        head = new Node;
        head -> data = item;
        head -> link = NULL;
    }
    else{
        for(tmp = head; tmp->link != NULL; tmp = tmp -> link)
            ;  // this loop simply advances the pointer to last node in
                //the list
        tmp ->link = new Node;
        tmp = tmp->link;
        tmp->data = item;
        tmp->link = NULL;
    }
}

bool Lilist::search(std::string target)
{
      for (Node *cursor = head; cursor != NULL; cursor = cursor->link){
        if (cursor->data == target)
            return cursor;
    }
  
}
void Lilist::move_front_to_back()
{
  if( head && head->next)
  {
    Node* newHead = head->next;
    Node* cur = newHead;

    while( cur->next ) cur = cur->next;
    
    head->next = NULL;
    cur->next = head;

    head = newHead;
  }
}  
void Lilist::show()
{
    for(Node *tmp = head; tmp != NULL; tmp = tmp->link)
        std::cout<<tmp->data<< "  ";
	std::cout <<std::endl;
}


main.c
#include <iostream>
#include <string>
#include "list1.h"
using namespace std;

int main(){
    Lilist L1, L2;
    string target;
    L1.add("Charlie");
    L1.add("Lisa"); 
    L1.add("Drew");
    L1.add("Derrick");
    L1.add("AJ");
    L1.add("Bojian");
    cout<<"Now showing list One:\n";
    L1.show();

    cout<<"Enter a name to search: ";
    cin>>target;
    if(L1.search(target) != NULL)
            cout<<"That name is stored at address: "<<L1.search(target) <<endl;
     else
 	cout<<"That name is not in the list.\n";
    L1.move_front_to_back();
     L1.move_front_to_back();
     L1.show();
     
return 0;
}

}

The output I am getting:

Now showing list one:
Charlie Lisa Drew Derrick AJ Bojian
Enter a name to search: Charlie
That name is stored at address: 1
Charlie Lisa Drew Derrick AJ Bojian

The output I am supposed to get:

Now showing list one:
Charlie Lisa Drew Derrick AJ Bojian
Enter a name to search: Charlie
That name is stored at address: 1
Drew Derrick AJ Bojian Charlie Lisa
Last edited on
Well, you inexplicably have a "link" and "next" which you seem to use arbitrarily. Presumably you should get rid of "link" and rename any "link" identifiers in the program to "next".

And your "search" function says it's returning a bool, but it actually returns a Node*.
Topic archived. No new replies allowed.