8 Replies - 2558 Views - Last Post: 23 April 2012 - 05:32 AM Rate Topic: -----

#1 RileyFrank  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-April 12

Searching A Linked List

Posted 20 April 2012 - 12:48 PM

Hi guys first time poster here.
I've been tasked with making a program thats searches 400+ movies (linked together using a linked list) by title, genre, year, rating, lead actor, etc.

There is a catch though, we are only allowed ONE search function to preform the searches through the linked list. Also, within that search function, we are only allowed one while loop ( which i assume in my case will be something like..

While (moviePtr != NULL)

Obviously their will be many different instances if its an actor search, or a genre search. In the case of actor, genre, rating, year, subgenre, and supporting actor, it should output every single instance of it was found. (for example, if KEvin bacon was in x-men and the notebook, it should output both not just one of them (to the output file not the sceen)).

Ive found myself completely stumped by these restrictions that we were given.
How will my search function handle different data types? (year and rating have to be declared as ints). How will it know what exactly im searching for? If im searching for actor, i dont want it to search for title as well.

Any suggestions on how to get started and get going are very appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Searching A Linked List

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: Searching A Linked List

Posted 20 April 2012 - 01:14 PM

What language are you using to complete this project?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 RileyFrank  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-April 12

Re: Searching A Linked List

Posted 20 April 2012 - 01:19 PM

View Postjimblumberg, on 20 April 2012 - 01:14 PM, said:

What language are you using to complete this project?

Jim

C++ on eclipse indigo.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: Searching A Linked List

Posted 20 April 2012 - 01:24 PM

Since this is C++ you can use function overloading, or templates to allow you to search for each different type. You should probably start by getting your program to work searching for one of your fields. Once you get the program correct for this one item it should be fairly easy to allow searching on any field.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Searching A Linked List

Posted 20 April 2012 - 01:25 PM

Right, you need a generic way to write this function. If it's not evident to you how to do this, you have to make the solution apparent.

Let's say the restriction to have only one function wasn't there. You can write multiple search functions. Why don't you write a function to search by actor, and then a function to search by genre?
Was This Post Helpful? 0
  • +
  • -

#6 RileyFrank  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-April 12

Re: Searching A Linked List

Posted 20 April 2012 - 01:32 PM

View Postjimblumberg, on 20 April 2012 - 01:24 PM, said:

Since this is C++ you can use function overloading, or templates to allow you to search for each different type. You should probably start by getting your program to work searching for one of your fields. Once you get the program correct for this one item it should be fairly easy to allow searching on any field.

Jim



Hi jim thanks for the reply. Ive never used function overloading, aren't we technically using more then one function (even though it has the same name?)

Olers, are you suggestion i should just write seperate functions for each search and figure out a way to combine them?

Thanks for the quick replys guys!
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Searching A Linked List

Posted 20 April 2012 - 01:36 PM

When there's a problem you can't solve, you have to break it down into simpler problems that you can solve.
Was This Post Helpful? 1
  • +
  • -

#8 RileyFrank  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-April 12

Re: Searching A Linked List

Posted 22 April 2012 - 06:35 PM

Hi All, Just wanted to let you all know i took your advice. I have 3 different search functions. One for the numerical values (the year and the rating), 1 for the genre and actors, and lastly one for the title.

Here is the code for all three of them.
First off, the genre, and actors search
int ItemSearch (MovieNode* head,string itemSearched, ofstream& outFile)
{
	int instances;
	MovieNode* moviePtr;
        moviePtr = head;
	instances = 0;






		while (moviePtr !=NULL)
		{
			if (moviePtr-> genre == itemSearched || moviePtr ->subGenre == itemSearched)
			{

				instances = instances +1;
                                // outputting to the file
				OutList(moviePtr,outFile,"Genre",itemSearched,instances);
				moviePtr = moviePtr -> next;
			}
			else if (moviePtr->leadActor == itemSearched || moviePtr->supportActor == itemSearched)
			{

				instances = instances +1;
                                //outputting to the file
				OutList(moviePtr,outFile,"Actor",itemSearched,instances);
				moviePtr = moviePtr -> next;
			}
			else
			{
				moviePtr = moviePtr ->next;
			}


		}


now the code for the title search.
void TitleSearched(MovieNode*head,
					string titleSearched,
					ofstream& outFile)
{
	MovieNode* moviePtr;
	bool found;

	moviePtr = head;
	found = false;

	while (moviePtr !=NULL & !found)
	{
		if (moviePtr-> title == titleSearched)
		{
			found = true;
		}
		else
		{
			moviePtr = moviePtr -> next;
		}

	}
	if (found)
	{
		cout << endl << titleSearched << " has been found!\n";
		TitleOutput (moviePtr,outFile);
	}
	else
	{
		cout << endl << titleSearched << " was not found.\n";
	}
}


Lastly the rating and year search.
int NumSearched(MovieNode* head, int numSearched)
{

	int instances;
	MovieNode* moviePtr;

	ofstream outFile;


		moviePtr = head;
		instances = 0;
		while (moviePtr !=NULL)
		{
			if (moviePtr-> year == numSearched)
			{

				instances = instances +1;
				NumOutList(moviePtr,outFile,"year",numSearched,instances);
				moviePtr = moviePtr -> next;
			}
			else if (moviePtr->rating == numSearched)
			{

				instances = instances +1;
				NumOutList(moviePtr,outFile,"rating",numSearched,instances);
				moviePtr = moviePtr -> next;
			}
			else
			{
				moviePtr = moviePtr ->next;
			}


		}
	return instances;
}


I wanted to remind you guys what my task was.
1. Combine these three search functions into one
2.have only ONE while loop in the search
3. only one return in any given function (however, id assume this would be a void function when combined)

My main issue i beileve is my ints and strings. I am not allowed to declare rating or year as strings. And just the format of the code in combing all three in general is giving me a head ache :sad2:
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: Searching A Linked List

Posted 23 April 2012 - 05:32 AM

First in all three functions the actual search should be almost identical except the search term. I would avoid doing anything except the search in these functions. I also don't see why you are creating another instance of MovieNode in each of these functions, just use the parameter you passed into the function. Also you should possibly be returning the "node" where you found the match, or a nullptr if you don't find the item.

Also:

Quote

There is a catch though, we are only allowed ONE search function to preform the searches through the linked list. Also, within that search function, we are only allowed one while loop ( which i assume in my case will be something like..

Does this forbid the use of a loop in your calling function when dealing with the items that may have multiple instances? If you just perform the "search" in your search function and let your calling function handle the differences you might be better off. Also have you tested each of these functions as written? Do they actually find and print the expected values? For example does your TitleSearch() function actually find and print the correct output?

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1