Linked List, Search function non-operable

Hey all, I'm trying to get this Linked List Program for an assignment. I've finally gotten the program to run but the search function isn't working correctly. I can't seem to see what i'm doing wrong. Was hoping you guys could help me figure that out. Thank you in Advance.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  #include<iostream>
#include<string>
using namespace std;

class AlbumNode
{
	
	int year;
	string artist;
	string genre;
	string label;
	string name;

	AlbumNode *next; //pointer is added here

	AlbumNode() : name("Album Name"), artist("Artist"), genre("Genre"), year(0), label("Artist Label"), next(0) {}// these are the constructors
	AlbumNode(int y, string t, string g, string l, string n) : year(y), artist(t), genre(g), label(l), name(n), next(0) {}// these are the constructors
	friend class AlbumListNode;
	friend void operator<<(ostream& os, const class AlbumListNode&);//friends have been added so that private members can be accessed

};

class AlbumListNode
{
	AlbumNode *root;
	void DoAdd(int y, string t, string g, string l, string n, AlbumNode*L)
	{
		L->next = new AlbumNode(y, t, g, l, n);
		cout << n << " has been successfully added to your collection" << endl;

	}
	void Print(ostream& os) const
	{return DoPrint(os, root);}
	void DoPrint(ostream& os, AlbumNode *L) const
	{if (L->next)DoPrint(os, L->next);
		os << L->name << " ";}
public:
	AlbumListNode() :root(0) {}
	AlbumListNode(const AlbumListNode& list)
	{
		root = 0;
	}
	void Add(int y, string t, string g, string l, string n)
	{
		if (!root)
		{
			root = new AlbumNode(y, t, g, l, n);
			cout << n << " has been successfully added to your collection"<< endl;
		}
		else
		{
			return DoAdd(y, t, g, l, n, root);
		}

	}
	friend void operator<<(ostream& os, const class AlbumListNode&);
	bool search(string s)
	{
		AlbumNode *current = root;
		while (current)
		{

		}
		if (current->name==s)
			return true;
		else
		{
			cout << "unavailable to find Album" << endl;
			current = current->next;
			return false;
		}
	};
	

};

void operator<<(ostream& os, const AlbumListNode& list)
{
	os << "["; if (list.root) list.Print(os); os << "]";
}

int main()
{
	AlbumListNode list;
	list.Add(1969, "The Beatles", "Rock and Roll", "Apple Records", "Abbey Road");
	list.Add(1991, "Nirvana", "Rock and Roll", "DGC Records", "Nevermind");
	list.Add(1971, "Led Zepplin", "Rock and Roll", "Atlantic Records", "Led Zeplin IV");
	list.Add(1986, "Bon Jovi", "Rock and Roll", "Mercury Records", "Slippery When Wet");
	list.Add(2000, "Linkin Park", "Rock and Roll", "Warner Bros Records","Hybrid Theory");

	string search;
	cout << "List name of album you'd like to search for?:  ";
	cin >> search;
	list.search(search);
	return 0;


}
1
2
3
4
5
		AlbumNode *current = root;
		while (current)
		{

		}


When will that while loop end?
Solved it with this code.. it's not perfect at all (check if no char in parameter and... and...), but it works on my machine... change your values and test... sorry, did my own version :) ..in my opinion i would not decide for a list as container, missing the find function...

instead of list, you could use set- or map-container... it delivers you "count" and "find" function on board... maybe someone other knows a better way..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool AlbumNodeList::search(string text)
{
    string searchtext = text;
    cout << "searchtext is: " << searchtext << endl;

    for(list<AlbumNode *>::iterator iter = list_of_nodes.begin(); iter != list_of_nodes.end(); ++iter){

        if((**iter).getName() == searchtext){
            cout << "found" << endl;
            return true;
        }
       cout << "searching..." << endl;
    }
    return false;
}
Last edited on
Topic archived. No new replies allowed.