Classes and Linked Lists

I'm writing a program that creates a linear linked list from information in a txt file. I won't write my actual code nor the actual homework assignment, but I'll create a similar scenario.

We have a file called "movies.txt" and it contains this information:
Ironman:2008
Thor:2011
Avengers:2012
Guardians of the Galaxy:2014

I create two classes: a class (Movie) that will manage the information of ONE movie, and a class (MovieList) that will manage the linear linked list of movies.

I also have a struct called "node" containing Movie data member and a pointer to the next node.

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
class Movie
{
    private:
        char * title;  //dynamically allocated
        int year;
    public:
        Movie();
        ~Movie();
        int createMovie(char * aTitle, int aYear)
        int readMovie(char * title, int & year);
        int copyMovie(Movie&);
}

struct node
{
    Movie aMovie;
    node * next;
}

class MovieList
{
    private:
        node * head;
    public:
        MovieList();
        ~MovieList();
        int insertMove(Movie & addMovie);
}

int Movie::readMovie(char * title, int & year)
{
    ifstream f;
    
    f.open("movies.txt");
    if(f){
        f.get(title, 50, ':');
        f.ignore(50, ':');  //ignore delimeter
        while(!f.eof()){
            f >> year;
            f.ignore(50, '\n');

        //read again in case more data
        f.get(title, 50, ':');
        f.ignore(50, ':');
        }
    }
    return 1;


int main()
{
    //temporary variables
    char title[100];
    int year;
  
    return 0;
}
}


I've done more coding, but this seems to be the wall. I can successfully read information from a file, but HOW do I then put what I've read into the Movie class's data members? I want to build a linked list of Movie.

OR how do I read directly into a node?

Any suggestions? I've been stuck on this for a few days.
Last edited on
MovieList should provide you with a mechanism to either copy an existing Movie into a new element in the list, and/or it should provide a mechanism to allow you to access a Movie which is already in the list so you may modify it. Code which uses MovieList should have no idea that your node class even exists.
Okay. I've been trying to figure that out. Well, in Movie I have the function int copyMovie(Movie&); where I attempt to copy an existing Movie and then call it in MovieList when I want to build. However, my homework code gives me a segmentation fault when I try to run it.

Example:
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
int Movie::copyMovie(Movie & source)
{
    //to avoid memory leak
    if(title)
        delete title;

    title = NULL;

    title = new char[strlen(source.title)+1];
    strcpy(title, source.title);

    year = source.year;
}


int MovieList::insertMovie(Movie & addMovie)
{
    if(!head)
    {
        head = new node;
        head->next = NULL;
        head->aMovie.copyMovie(addMovie);
    }
    return 1;
}


The purpose of the copyMovie function (what I want it to do) is copy the data that was read and then insert it within a node/list. Obviously, it's not working but I don't know how to proceed from there. I don't think I'm copying anything...
Last edited on
You should implement a copy assignment operator instead of that copyMovie function. (Google for a few minutes to get an idea).
Topic archived. No new replies allowed.