Jump to content

help with Linked List

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
M0SS_99

M0SS_99

    Newbie

  • Members
  • Pip
  • 4 posts
Hi all, I have to implement a function List *foo(List *pointslist) that gets a list of n Point instances and returns a list of instances of Triangle that contains all the possible triangles achievable with the n points, so here I have three structures:

typedef struct {

double x;

double y;

} Point;


typedef struct {

Point *p1, *p2, *p3;

double area; 

} Triangle;


struct node {

void* info;

struct node* next;

};

typedef struct node List;
What i need to understand is how to build the List* pointlist since every point is not just a single value but has x and y, so here's what i tried to do:
void buildListOfPoints(List **headRef, void* data)

{

	List* newNode = malloc(sizeof(List));

	

	newNode->info = data;

	newNode->next = *headRef;

	*headRef = newNode;	

}


int main()

{

	Point p;						// struct Point variable	

	

	List *head = NULL;


	buildListOfPoints(&head, ???  );  // I want to add the point to the list but how do i do that?

	

	while(head != NULL) {

		printf("%.2lf", head->info);

		head = head->next;

	}

	printf("\n");

		

	return 0;

}

My problem is with the call to the buildListOfPoints() function, i don't understand how to pass a point value which should be a p.x and p.y for example.. any help? Thanks

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
buildListOfPoints(&head, &p);

The problem is you probably want to have buildListOfPoints collect the information for several points, and just pass it head.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
M0SS_99

M0SS_99

    Newbie

  • Members
  • Pip
  • 4 posts

WingedPanther said:

buildListOfPoints(&head, &p);

The problem is you probably want to have buildListOfPoints collect the information for several points, and just pass it head.

Yes I want it to collect the information for several points, plus if I pass a pointer to Point* with buildListOfPoints(&head, &p) i'm not passing any value, instead i want to give the list several points, how can I do that? Thanks for the help anyway ;)

#4
M0SS_99

M0SS_99

    Newbie

  • Members
  • Pip
  • 4 posts
That's how i managed to solve it:
int main()
{
	Point p;						// struct Point variable	
	
	p.x=5.0;
	p.y=6.0;

	List *head = NULL;
	buildListOfPoints(&head, &p);
	
	while(head != NULL) {
		Point* pp=(Point*)(head->info);	
		printf("X:%.2f Y:%.2f", pp->x, pp->y);	
		head = head->next;
	}
	printf("\n");
		
	return 0;
}
Bye!