A polygon example
This provides an example of a self-referential pointer.

Essentially self-referential pointers allow programmers to construct graphs. This example stores a polygon as in this schematic diagram:


Source Code: (main.c)
#include <stdio.h>
#include <math.h>

typedef struct {
  double xy;  
}
point;


/* here we declare a structure in a new way.
   When declared in this way, we must refer to objects of this type as
   of type "struct vertex2" */
 
struct vertex2 /* STAR */ {
  point p;
  struct vertex2 *next;    /* a self-referential pointer */
};

/* Self-referential pointers can be thought of as giving directed edges between two nodes */

/* to save us from writing "struct vertex2" each time, we define an alias */
typedef struct vertex2 vertex;  /* now "vertex" is equivalent to "struct vertex2" */

/* A polygon is a cyclic list of vertices.
   We will store pointers to two adjacent vertices */

typedef struct {
  vertex *first,*last;
}
polygon;

/* This function takes a pointer to an uninitialized polygon and a number
   and constructs a regular n-gon inscibed in the unit circle*/

void constructRegularPolygon(p,n)
     polygon *p
     int n;      
{
  vertex *ptr;
  int i;

  p->first=(vertex *)malloc(sizeof(vertex)); /* create the first vertex */
  p->first->p.x=1;
  p->first->p.y=0;

  ptr=p->first;   /* ptr points to the same thing that v does */

  for (i=1i<ni++){
    ptr->next=(vertex *)malloc(sizeof(vertex)); /* Construct the next vertex */

    ptr=ptr->next;  /* ptr now points to the new vertex */
    
    ptr->p.x=cos(2*M_PI*i/n);
    ptr->p.y=sin(2*M_PI*i/n);
  }

  p->last=ptr;         /* store the last vertex */
  p->last->next=p->first/* close the loop */
}

/* Delete the first vertex of the polygon. The first vertex becomes the
   second vertex. */

void deleteFirstVertex(p)
     polygon *p;
{
  vertex *ptr = p->first->next/* store the location of the second vertex */
  free(p->first);               /* free the first vertex */
  p->last->next=ptr;            
  p->first=ptr;
}

/* free all the memory used by the polygon */
void freePolygon(p)
     polygon *p/* pointer to a polygon */
{
  while (p->first != p->last) { /* while there are at least two vertices 
                                   in the polyon */

    deleteFirstVertex(p);
  } 
  /* Now there is only one vertex left. */ 
  free(p->first);   
}

void printPolygon(p)
     polygon p;
{
  vertex *v = p.first/* v points to the first vertex */
  printf("(%8.8lf, %8.8lf)"v->p.xv->p.y);
  v=v->next;
  while (v!=p.first){
    printf("->(%8.8lf, %8.8lf)"v->p.xv->p.y);
    v=v->next;         /* next vertex */
  }
  printf("\n");
}

/* cycle the pair of vertices the polygon structure points to */
void cycle(p)
     polygon *p;
{
  p->last=p->first;
  p->first=p->first->next;
}

/* Insert a new vertex in the polygon between the last and first vertex. 
   This new vertex becomes the last vertex. */

void insertVertex(pxy)
     polygon *p;
     double x,y;   
{
  vertex *v=(vertex *)malloc(sizeof(vertex)); /* construct the new vertex */
  v->p.x=x;
  v->p.x=y;
  
  p->last->next=v;    /* Set the old last vertex to point to the newly constructed vertex */
  v->next=p->first;   /* The new vertex now points to the first vertex */
  p->last=v;          /* Update the last vertex */
}

int main(){
  polygon p;
  /* construct a square */
  constructRegularPolygon(&p,4);
  printPolygon(p);

  cycle(&p);
  printPolygon(p);
  
  deleteFirstVertex(&p);
  printPolygon(p);

  insertVertex(&p1.01.0);
  printPolygon(p);

  freePolygon(&p);

  return 0;
}

Output:
(1.00000000, 0.00000000)->(0.00000000, 1.00000000)->(-1.00000000, 0.00000000)->(-0.00000000, -1.00000000)
(0.00000000, 1.00000000)->(-1.00000000, 0.00000000)->(-0.00000000, -1.00000000)->(1.00000000, 0.00000000)
(-1.00000000, 0.00000000)->(-0.00000000, -1.00000000)->(1.00000000, 0.00000000)
(-1.00000000, 0.00000000)->(-0.00000000, -1.00000000)->(1.00000000, 0.00000000)->(1.00000000, 1.00000000)

To compile:
gcc -lm main.c -o main


HOME >>>>> PROGRAMMING >>>>> POINTERS