[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

structures and pointers

Posted on 2000-01-15
9
Medium Priority
?
195 Views
Last Modified: 2012-05-07
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  
0
Comment
Question by:gvijay1
9 Comments
 
LVL 2

Expert Comment

by:_lychee_
ID: 2356760
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
 

Author Comment

by:gvijay1
ID: 2356786
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
 
LVL 2

Expert Comment

by:_lychee_
ID: 2356827
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 4

Expert Comment

by:inpras
ID: 2356864
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
 
LVL 2

Expert Comment

by:_lychee_
ID: 2356909
? i assumed that each movie can have more than one actor... that's reasonable i think?????
0
 
LVL 1

Expert Comment

by:hustch
ID: 2356918
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
 

Author Comment

by:gvijay1
ID: 2357623
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
 
LVL 9

Accepted Solution

by:
jasonclarke earned 300 total points
ID: 2359036
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
 

Author Comment

by:gvijay1
ID: 2360898
Thanks for the advise..I broke itr down and it seems to work.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question