linked list and fonction in a structure

Hello,

I begin and I've a little problem : I've made a code to create a linked list included a name and a number for each case, and then some sorting and deleting functions.

Then, I must to put the "Affiche" and "insere_en_tete" functions into the "Personne" structure.

No problems for "Affiche" function because it's a void but "insere_en_tete" function makes me some problems. I've tried some things but it failed.

If anyone can help me, it would be very kind.

This is my programme :


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
typedef struct Personne{
	int Id;
	string nom;
	void Affiche ();
	struct Personne *next;
}liste;

liste *insere_en_tete (liste *lis){
	liste *c;
	c=new liste;
	if (c!=NULL){
		Personne tmp;
		int static j=0;
		j++;
		cout<<"\nId_"<<j<<" : ";
		cin>>tmp.Id;
		c->Id=tmp.Id;
		cout<<"\nNom_"<<j<<" : ";
		cin>>tmp.nom;
		c->nom=tmp.nom;
		c->next=lis;
		lis=c;
	}
	return lis;
}		
void liste::Affiche (){
	cout<<setw(20)<<Id<<setw(20)<<nom<<endl<<endl;		
}


1
2
3
4
5
6
7
8
9
10
11
void main (){
int  x=3, i=1;
liste *debut;
debut=NULL;
while (i<=x){
	debut=insere_en_tete(debut);
	i++;
 }				
for (liste *parcours_a=debut; parcours_a!=NULL; parcours_a=parcours_a->next)
        parcours_a->Affiche();
}



Sorry for my bad English :)
closed account (Lv0f92yv)
What you're doing there doesn't look to me like a linked list... A linked list usually has two basic classes, at minimum:

A 'List' class that manages the sorting, adding, deletion, etc of the objects it contains
A 'Node' class that contains the data and other components that make up your data (a unique ID, maybe).

The List class would contain pointers to the head and tail of the list, which would be updated as items are added/removed. This is what allows you to 'traverse' the list.

So a linked list of "Personne" would behave kind of like this:

1
2
3
LinkedList list* = new LinkedList();
list->add( "jerry" );
list->add( "chris" );

...
and it would be in the 'list' class that you can create 'Node' objects to contain "Personne" objects with the name passed as a parameter to add().

Edit: The 'Node' objects would contain references to the next* and prev* (if doubly linked) objects...
Last edited on
It could be considered a bad design, but that doesn't cause any errors.
Actually, nothing is causing any errors. I compiled it and it works fine (except that the program closes immediately. see http://www.cplusplus.com/forum/beginner/1988/ )
What exactly were your problems?
I haven't already learnt classes.

This programme has no problems and it isn't complete (thera are a switch case in loop and "x" is given by the utilisator) , just the important part.

First I've made that :

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
typedef struct Personne{
	int Id;
	string nom;	
	struct Personne *next;
}liste;

liste *insere_en_tete (liste *lis){
	liste *c;
	c=new liste;
	if (c!=NULL){
		Personne tmp;
		int static j=0;
		j++;
		cout<<"\nId_"<<j<<" : ";
		cin>>tmp.Id;
		c->Id=tmp.Id;
		cout<<"\nNom_"<<j<<" : ";
		cin>>tmp.nom;
		c->nom=tmp.nom;
		c->next=lis;
		lis=c;
	}
	return lis;
}		
void Affiche (liste *lis){
                liste *p;
                p=lis;
                while (p!=NULL) {
	       cout<<setw(20)<<p->Id<<setw(20)<<p->nom<<endl<<endl;	
                       p=p->next;
               	}
}


1
2
3
4
5
6
7
8
9
10
void main (){
int  x=3, i=1;
liste *debut;
debut=NULL;
while (i<=x){
	debut=insere_en_tete(debut);
	i++;
 }				
Affiche (debut);
}


The second part of my exercise is to declare "Affiche" and "insere_en_tete" in the structure.

Affiche is okay (in the fisrt programme) but I can't do the same thing for "insere_en_tete".
You can think of a member function as of a normal function that has argument this. this is a pointer to the object that calls the function.
1
2
3
4
5
6
7
8
void Affiche (liste *lis){
                liste *p;
                p=lis;
                while (p!=NULL) {
	       cout<<setw(20)<<p->Id<<setw(20)<<p->nom<<endl<<endl;	
                       p=p->next;
               	}
}

would become
1
2
3
4
5
6
7
8
void liste::Affiche (){
    liste *p;
    p=this;
    while (p!=NULL) {
        cout<<setw(20)<<p->Id<<setw(20)<<p->nom<<endl<<endl;	
        p=p->next;
    }
}

You can do the same wit h insere_en_tete. Just replace lis with this. Note: on line 21 you assign to lis. You cannot assign to this however. Simply return c.
Sorry, I'm stupid. I've understood that my problem was impossible. It's impossible to include "insere_en_tete" into the structure. Just (how do you say the action to enter letters or numbers ?) that :

1
2
3
4
5
6
7
8
void liste::saisie(){
	int static j=0;
	j++;
	cout<<"\nId_"<<j<<" : ";
	cin>>Id;		
	cout<<"\nNom_"<<j<<" : ";
	cin>>nom;
}


So :

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
typedef struct Personne{
	int Id;
	string nom;
	void Affiche ();
	void saisie();
	struct Personne *next;
}liste;
void liste::saisie(){
	int static j=0;
	j++;
	cout<<"\nId_"<<j<<" : ";
	cin>>Id;		
	cout<<"\nNom_"<<j<<" : ";
	cin>>nom;
}
liste *insere_en_tete (liste *lis){
	liste *c;
	c=new liste;	
	if (c!=NULL){
		c->saisie();		
		c->next=lis;
		lis=c;
	}
	return lis;
}		


Thank you for your help hamsterman :)

If I write English errors, correct me. It's also important to increase my English.
Topic archived. No new replies allowed.