Link to home
Start Free TrialLog in
Avatar of gvijay1
gvijay1

asked on

Problem with link list

Dear Experts,
                     I am having problems with making my link list work. Everytime I create a node and try to link it, I am only able to print out the last node entered. Below is the prototype for my nodes and the code to link them. COuld you please tell me what my fault is.

//movie structure
typedef struct movie *mov_ptr;
struct movie
{
      string movie_name;
      mov_ptr next_movie;
};

mov_ptr head;

//Defining the structure for the movie

typedef struct actor *act;
struct actor
{
      string actor_name;
      act next_actor;
      mov_ptr next_movie;
};

act head_actor;

Here each actor has a link to the various movies acted by the actor.I initially declared head_actor as null in the main.

//Code to add a node
void add_node_actor (string Texts[], int count)
{

      act temp;
      temp=new actor;
      
      temp->actor_name=Texts[1];
      
      head=new movie;
      head=NULL;
      
      temp->next_movie=head;
      temp->next_actor=head_actor;
      head_actor=temp;
      
      for (int h=2; h<count; h++)
      {
            mov_ptr new_movie;
            new_movie=new movie;
            
            new_movie->movie_name=Texts[h];
            new_movie->next_movie=head;
            head=new_movie;
      }
}

//code to return the link list's contents
void return_node()
{

while(head_actor != NULL)
{
cout<<"Actor's name is.."<<head_actor->actor_name<<endl;
      
      while(head != NULL)
      {
            cout<<"Movie Name.."<<head->movie_name<<endl;
            head=head->next_movie;
      }
      
head_actor=head_actor->next_actor;
      
}
}
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi gvijay1,

why do you do this:

------------------------
head=new movie;
head=NULL;
------------------------

this creates a new instance of type movie and after this set's head to NULL, so you loose the pointer to the new instance (which will lead to memory leaks)

even there's a problem in return_node():
when the while loop ends the head is NULL, so your whole list is anywhere in the nirwana and you don't have any pointer pointing to it (lot's of memory leaks I think).

hope that helps,

ZOPPO
PS: even the head_actor will point to NULL after return_node, same problem as with head ...

I'm not sure what you wanna do, but I think you want to have a list of actors and for each actor you want a list of movies. To do so you'll have to linked list of actors, and for each actor a linked list of movies. Your code implements one linked list of actors and only one linked list of movies, so your return_node() would (at best) print a list of actors and for each actor the complete list of movies.

ZOPPO
Even better strategy would be to hold a global linked list of movies, a global linked list of actors and for each actor a linked list of pointer pointing to movies in the global movie list. So you can avoid duplicated movie entries ...
Avatar of graham_k
graham_k

Even better strategy would be to to use the Standard Template Libarary. Then you get a debugged & tested link list class which will also probably be much more efficient than anything which you write.

Fire up your compilerr's help & Look for <vector>. look in the examples too.

Why reinvent the wheel? You may as well code in C, if you don't want to use the featrues of C++. In fact, why are you using "struct" instead of "class"? get with C++ :-)


#include <vector>
using namespace std;

class Film
{
  Film(string title, string direcector, int year);  // constructor

private:
  string theTitle;
  string theDirector;
  int     theYear;
  // etc
}

class Actor
{
public:

Actor();           // constructor

private:
  vector<Film> theFilms;
}

vector<Actor> theActors;

then you can grow your list with

Film *film1 = new film("casablanca", "whoever, 1952);
Film *film2 = new film("key largo", "whoever, 1954);

Actor *Bogart = new Actor;

Bogart->theFilms.push_back(film1);
Bogart->theFilms.push_back(film2);
theActors.push_back(Bogart);


Now, I'm at work, without a C++ compiler & typed that all in quickly, so don't expect it compile. But, it should show you what you can do with the STL. I urge you very strongly to try it out.
ASKER CERTIFIED SOLUTION
Avatar of ramshakal
ramshakal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gvijay1

ASKER

Ramshakal,
                 Thanks for your answer. I tried it out, but it does not seem to produce all the elements in the linked list. It still just prints out the first head_actor node. I will send my entire code and try to run it to see what I am talking about. The linked list is still not being formed :(

#include <iostream>
#include <string>
#include <vector>

//Defining the structure for each actor

typedef struct movie *mov_ptr;
struct movie
{
      string movie_name;
      mov_ptr next_movie;
};

mov_ptr head;

//Defining the structure for the movie

typedef struct actor *act;
struct actor
{
      string actor_name;
      act next_actor;
      mov_ptr next_movie;
};

act head_actor;


using namespace std;

void add_node (string Texts[], int count);
void return_node();
void swap_values (string& x, string& y);


main()
{
vector<string> Text;
string CurLin;
string Name;
string command;

head_actor=NULL;
head=NULL;

//Taking in input from keyboard
while (1)
{
cout << "Enter the info"<<endl;
int count=0;

Text.erase(Text.begin(), Text.end());

while(true)
{

getline(cin,CurLin);
if (CurLin.length()==0)
break;
Text.push_back(CurLin);
count++;
                             
}

//input obtained and stored in an array

cout<<"The num "<<count<<endl;
cout<< Text[0]<<endl;

for (int i=0; i<count; i++)
{
      cout<<"The elements are "<<Text[i]<<endl;
}

command=Text[0];
Name=Text[1];

cout<<"Command is "<<command<<endl;
cout<<"Person's name is "<<Name<<endl;

//Sorting the array by chronogical order

int times=0;
      
      while (times<count)
      {
      for (int j=2; j<(count-1); j++)
      {
            if (Text[j]>Text[j+1])
            {
                  swap_values (Text[j],Text[j+1]);
            }
      }
      times++;
      }

//done

//Converting to array of strings
      
string Texts[count];

for (int i=0; i<count; i++)
{
      Texts[i]=Text[i];
}
//done

//Function calls

add_node(Texts, count);
return_node();

}

return 0;

}

void swap_values (string& x, string& y)
{
string temp;

temp=x;
x=y;
y=temp;
}

void add_node (string Texts[], int count)
{
act temp;
temp=new actor;

temp->actor_name=Texts[1];                                                                                                    
temp->next_movie=NULL;
temp->next_actor = NULL;

                                     
if ( head_actor == NULL)
{
      head_actor=temp;
}

mov_ptr temp_moviePtr;

for (int h=2; h<count; h++)
{
      mov_ptr new_movie;
      new_movie=new movie;

      new_movie->movie_name=Texts[h];
        new_movie->next_movie=NULL;
      
      if (h==2)
        {
            temp_moviePtr = new_movie;
            head = new_movie;
            temp->next_movie = head;
      }
      else
      {
      temp_moviePtr->next_movie = new_movie;
      temp_moviePtr = new_movie;
      }
                                                                 
}

}

      

void return_node()
{

while(head_actor != NULL)
{
cout<<"Actor's name is.."<<endl;
cout<<head_actor->actor_name<<endl;
      
      while(head != NULL)
      {
            cout<<"Movie Name.."<<head->movie_name<<endl;
            head=head->next_movie;
      }
      
head_actor=head_actor->next_actor;
      
}
}
Hi gvijay,
     Try printing the names while adding. This way you can debug. May be you are not passing the information correctly in the add function.
Avatar of gvijay1

ASKER

Thanks for the advise...I managed to get the linked list to work....
hey, guys. I don't want the points and I don't want to rain on your parade BUT ... if you want to code C, code C.  If you want to code C++ and you want linked lists, use the STL. *DO NOT* reinvent the wheel, because your wheel won't be as good.

| don't want to start a flame war here. I sincerely want to help. Look into it. No reply necessary.