#include "Node.h"
// Note that separate compilation of templates is supported in VC++
template <typename T>
void Node<T>::headInsert(Node<T>*& head, T theData) {
Node<T>* newHead = new Node<T>(theData, NULL, head);
head->setPreviousLink(newHead);
head = newHead;
}
This is the code I currently have for a class called Movies which is a linked list of Movie objects (it was originally an array but we are modding it to be a LL)
// Movies.cpp
#include "Movie.h" // include Movie class definition
#include "Movies.h" // include Movies class definition
#include "Node.h"
#include <fstream>
usingnamespace std;
Movies::Movies(string fn){loadMovies(fn);}
int Movies::getMovieCount() const {return movieCnt;}
int Movies::getMovieHash(string mc) const {
int hashVal = 0;
for( int i = 0; i < mc.length(); i++ )
hashVal = HASH_KEY * hashVal + mc[i];
hashVal = abs(hashVal) % TABLE_SIZE;
return hashVal;
}
const Movie * Movies::getMovie(string mc) const {
if(mc.length()==0)
return NULL; // not found
elsereturn &(movies[getMovieHash(mc)]);
}
Movies::~Movies() {delete[] movies; movies = NULL;}
void Movies::loadMovies(string fn) {
ifstream iS(fn); // technically should be c_str
string s;
getline(iS, s); // skip heading
getline(iS, s);
movieCnt = 0;
movies = new Movie[TABLE_SIZE];
Movie* n = new Movie(s);
Node<Movie>* root = new Node<Movie>(n,NULL,NULL);
//while(!iS.eof()) {
//Movie* m = new Movie(s);
//movies[getMovieHash(m->getTitle())] = *m;
//movieCnt++;
//getline(iS, s);
//}
iS.close();
}
My question is on line 37 of the code above, I'm getting an error that says: "No instance of constructor "Node<T>::Node[with T=Movie]" matches the argument list.
I don't really understand the error and how I could fix it.
Also, how would I go about creating the LL so that it is in ascending sequence by title ignoring cases. Here is the code for the Movie class:
The error is because you're passing a Movie* instead of Movie, as the first argument.
To get the sorted result, you could either sort the full list (1), or insert nodes in the right positions as you populate it (2). In both cases you'll have to write operator <. To ignore case when comparing two strings, convert both of them to lover(upper) case.
(1) You'll have to find yourself a sorting algorithm. Note that sorting a LL is not the same as sorting an array. Quick Sort is simple for lists. Remove the first element "pivot", then separate the rest of the list into two lists : "left" with elements that compare less than "pivot and "right with elements that don't. Then Sort "left" and "right" using recursion. Lastly concatenate left+pivot+right.
(2) This is similar to (1), but a bit simpler. You're given a sorted (possibly empty) "list" and an "element". To insert "element" at the right spot, again, divide "list" into "left" and "right" as in (1). This time it is easier, since "list" is already sorted, so "left" will be its first half and "right" will be the second half. You only need to find the breaking point. Finally concatenate left+element+right. No recursion this time.
// Movies.cpp
#include "Movie.h" // include Movie class definition
#include "Movies.h" // include Movies class definition
#include "Node.h"
#include <fstream>
usingnamespace std;
Movies::Movies(string fn){loadMovies(fn);}
int Movies::getMovieCount() const {return movieCnt;}
int Movies::getMovieHash(string mc) const {
int hashVal = 0;
for( int i = 0; i < mc.length(); i++ )
hashVal = HASH_KEY * hashVal + mc[i];
hashVal = abs(hashVal) % TABLE_SIZE;
return hashVal;
}
const Movie * Movies::getMovie(string mc) const {
Node<Movie*>* here = new Node<Movie*>();
if(mc.length()==0)
return NULL; // not found
else{
mc = myToLower(mc);
while (here->getNextLink() != NULL){
if (myToLower(here->getData()->getTitle()) == mc)
return here->getData();
elsereturn NULL;
here = here->getNextLink();
}
}
}
Movies::~Movies() {delete[] movies; movies = NULL;}
void Movies::loadMovies(string fn) {
ifstream iS(fn); // technically should be c_str
string s;
getline(iS, s); // skip heading
getline(iS, s);
movieCnt = 0;
movies = new Movie[TABLE_SIZE];
Movie* n = new Movie(s);
Node<Movie*>* root = new Node<Movie*>(n,NULL,NULL);
while(!iS.eof()) {
Movie* m = new Movie(s);
Node<Movie*>* node = new Node<Movie*>(m,root,NULL);
node->getPreviousLink()->setNextLink(node);
movieCnt++;
getline(iS, s);
}
iS.close();
}
string Movies::myToLower(string s) const {
int n = s.length();
string t(s);
for(int i=0;i<n;i++)
t[i] = tolower(s[i]);
return t;
}
void Movies::Sort(){
Node<Movie*>* firsts = new Node<Movie*>();
Node<Movie*>* seconds = new Node<Movie*>();
Node<Movie*>* temp = new Node<Movie*>();
}
I finished the loadMovies method and I'm currently modded the getMovie method to traverse through the linked list and grab the movie they serach for for as well as working on the Sort method.
I guess my new question is regarding the getMovie method. So I created a dummy node called here, how do I get that to point to the head node?