#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
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*.