English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

means of data structure Linked list .By deatails i mean the posision of nodes...and i want it in c language

2006-10-27 07:01:46 · 1 answers · asked by s_waterlili 1 in Computers & Internet Programming & Design

1 answers

Looks simple. You need ciruclar doubly linked list.

struct point
{
float x; //x coordinate of the point
float y;//y coordinate of the point
struct point *previous_point; //pointer to the previous point
struct point *next_point; //pointe to the next point
};

void main()
{
int n;
printf("how many points in the polygon ");
scanf("%d \n",&n);
//n is the number of points in the polygon

//now read all the points and create linked list
for(int i= 0;i<=n-1;i++)
{
float x_coord,y_coord;
printf("enter x-coordinate ");
scanf("%d \n",&x_coord);
printf("enter y-coordinate ");
scanf("%d \n",&y_coord);

point *first_point = new(point);
point *current;
if(i == 0) // i.e. first point we have to create node for first point
{
first_point->x = x_coord;
first_point->y = y_coord;
first_point->previous_point=NULL;
first_point->next_point = NULL;
current = first_point;
}
else
{
point *current_point = new(point);
current_point->x = x_coord;
current_point->y = y_coord;
current_point->previous_point = current;
current->next_point = current_point;
if(i!=n-1)
current_point->next_point = NULL;
else // last point must point to first point
{
current_point->next_point = first_point;
first_point->previous_point = current_point;
}
current = current_point;
}

}

}

I hope this will be usefull. This program is not compiled and tested, but the logic is right.

2006-10-27 07:42:30 · answer #1 · answered by manoj Ransing 3 · 0 0

fedest.com, questions and answers