structures and pointers

Experts,
             I have a problem here. I need to create a link list for actor names with a link from each actor to the names of the movies that the actor acted in. What type of structures do I have to create for each of these nodes, and what code can I use to add a new node when data is entered; first to create the node for the actor name and second to create the node for the name of the movies. One idea I had is

Please let me know. Thanks  
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.

_lychee_Commented:
one idea u had is ___?

a linked list contains data and a pointer to the next item. to insert an item u allocate memory, fill in the data values and then attach it somewhere in the list. u can think about how to do it... it's quite ez...

or u could look at any algorithm/data structure books...
0
gvijay1Author Commented:
Dear Expert,
                I understand the concept quite well of how this is done. I am having problems implementing it, and have been struggling for days. That is why I need to see how you would set up the different structures, and how the code for appending values to the link list would look like.

My idea was

typedef struct Movie * link_to_next_movie
typedef struct Movie{
string movie_name;


typedef struct Actor * link_to_next_actor;
typedef struct Actor{
string name;
link_to_next_actor link;
//needs a link to the Movie node (I am not sure if how to do)

My idea is a littlle scratchy, but I hope you can follow.

0
_lychee_Commented:
correcting ur Actor code for example:

typedef struct {
    char *name;
    Actor *next;
} Actor;

now u can define an actor pointer by:
Actor *boo1;
or an Actor by:
Actor boo2;

as for ur Movie thingy, if u want different actors to share the same movie data, i think it's better to make a linked list that in turn points to movie data... i know that's quite unclear but i can quite express what's going on in my mind using only words... as for now try out the linked list of actors and get it working first... u can add the movies later...
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

inprasCommented:
I don't think U need two structures here, see for example

struct movie{
 char *strMovieName;
 char *strActorName;
 struct movie * next;
};

here U will get alll the info

in case U want a independent structure then

struct actor{
 char * atrActorName;
 int nId;
 struct actor* link;
}

and UR movie stucture has to be
struct movie{
 char *strMovieName;
 int nIdActor; //This is ID of the structure actor
 struct movie * next;
};
Hope this helps
0
_lychee_Commented:
? i assumed that each movie can have more than one actor... that's reasonable i think?????
0
hustchCommented:
You should create your lists, so that each actor cat be en any movie, and each move can feature any actor.

So each actor should have a list of movies, and each movia a list of actors.

The easyest way to do this is to use the list template from the standard library (#include <list>)
0
gvijay1Author Commented:
Dear Experts,
                     I tried your suggestions andcame up with the following. I have documented the code. The input is of the type;
a //command
Pierce Brosnan //actor name
Movie1   //Movies acted by the person
Movie2
Movie3

I need to create a link list for the actor, with a link to all the movies acted by the actor, and must be able to add actors to this linked list. This code gives one error. PLease give me hints.
#include <iostream>
#include <string>
#include <vector>

//Defining the structure for each actor

typedef struct actor *act;
struct actor
{
      string actor_name;
      act next_actor;
      act mo;
};

//Defining the structure for the movie

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


using namespace std;

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

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

//Array of the actor structures one array for each alphabet (To store by alphabet name)



//Taking in input from keyboard

cout << "Enter the info"<<endl;
int count=0;

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++;
      }

//Array sorted
      
act temp;

temp=(act)malloc(sizeof(struct actor));
temp->actor_name=Text[1];
temp->next_actor=NULL;

mov_ptr head, temp1, temp2;

head=(mov_ptr)malloc(sizeof(struct movie));
temp->mo=head;
head->movie_name=Text[2];
head->next_movie=NULL;
temp1=head;

for (int h=3; h<count; h++)
{
      temp2=(mov_ptr)malloc(sizeof(struct movie));
      temp1->next_movie=temp2;
      temp2->movie_name=Text[h];
      temp2->next_movie=NULL;
      temp1=temp2;
}




return 0;

}

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

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


0
jasonclarkeCommented:
I think you are trying to do too much in one go, break the problem down a bit, and it may seem easier:

you want: a list of actors
where each actor consists of a name and a list of movies.

you should probably separate the 'list' part from the 'data' part, i.e. you have a list of actors, not actors modified to be part of a list structure.

are you trying to implement the linked list functionality yourself - it looks as if you are - if so, please say, otherwise it would be much simpler to use standard containers:

struct Movie {
    string name;
};

struct Actor {
    string name;
    list<Movie*>  movies;
};

list<Actor>  actorList;

...and so on...

if you want to set up the list yourself (and don't want to use templates or anything C++ ish), then consider setting up a list structure that is separate from the actor data:

struct ListElement {
   ListElement(void* d) : data(d), next(0) {}
   void* data;
   ListElement* next;
};

struct Actor {
   Actor(const string& n) : name(n), movieList(0) {}
   string name;
   ListElement*  movieList;
};

struct Movie {
   Movie(const string& n) : name(n) {}
   string name;
};

ListElement* actorList = 0;

// example code to add an actor to the list
Actor* a = new Actor("name");
// Create the movie list...
for (int i=0; i<numMovies; i++)
{
    ListElement* thisElt = new ListElement(new Movie(movies[i]));
    thisElt->next = a->movieList;
    a->movieList = thisElt;
}
ListElement* thisActor = new ListElement(a);
thisActor->next = actorList;
actorList = thisActor;

         
*but* this is definitely the 'C' way to do it, there are much better ways in C++.
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:
Thanks for the advise..I broke itr down and it seems to work.
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
C++

From novice to tech pro — start learning today.