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;
      
}
}
gvijay1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
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
0
ZoppoCommented:
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
0
ZoppoCommented:
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 ...
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

graham_kCommented:
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.
0
ramshakalCommented:
Hi GVijay,

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

In main declare head_actor=NULL,
and also head=NULL,
Then modify the /***Code to add a node **/ as shown
                      void add_node_actor (string Texts[], int count)
                      {

                      act temp;
                      temp=new actor;

                      temp->actor_name=Texts[1];

                       /**** Remove this
                      head=new movie;
                      head=NULL;
                      *Remove this************/

    /* modify next_movie and next_actor pointer as below **/
                      temp->next_movie=NULL;
                      temp->next_actor = NULL;

/* Assign head_actor pointer to first node if you are adding this first time **/
                         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=head; **/
                             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;
                           }
                           /**Remove*head=new_movie; ***/
                      }
                      }

Ok Try with this. Later on you can remove head variable but do it yourself. in case of any prob it me know.

---
Ram
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gvijay1Author Commented:
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;
      
}
}
0
ramshakalCommented:
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.
0
gvijay1Author Commented:
Thanks for the advise...I managed to get the linked list to work....
0
graham_kCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.