#include<stdio.h>
/* Representation of polynomials */

/* Should normally go into a header file */
typedef union {
  int cv;
  char var;
} val_t;

typedef struct polynom_struct {
  struct polynom_struct *left;
  struct polynom_struct *right;
  struct polynom_struct *up;
  struct polynom_struct *down;
  val_t val;
  int exp;
} poly_t;

poly_t *create_root();
poly_t *create_poly();

int main() {
  /* Create the polynomial 3 + x^2 + x*y - 3*x*z^3 with 
   * z as root character. */
  val_t val;
  val.var = 'z';
  poly_t *root = create_root(val);
  
  poly_t *z_zero = create_poly(NULL, NULL, root, NULL, 0, 'x');
  z_zero->down = create_poly(NULL, NULL, z_zero, NULL, 0, 0); 
  z_zero->down->right = create_poly(z_zero->down, z_zero->down, 
				    z_zero, NULL, 2, 1);
  z_zero->down->left = z_zero->down->right;
  root->down = z_zero;

  poly_t *z_one = create_poly(z_zero, NULL, root, NULL, 1, 'y');
  z_one->down = create_poly(NULL, NULL, z_one, NULL, 0, 0);
  z_one->down->right = create_poly(z_one->down, z_one->down, z_one, 
				   NULL, 1, 'x');
  z_one->down->left = z_one->down->right;
  z_zero->right = z_one;

  poly_t *z_three = create_poly(z_one, z_zero, root, NULL, 3, 'x');
  z_three->down = create_poly(NULL, NULL, z_three, NULL, 0, 1);
  z_three->down->right = create_poly(z_three->down, z_three->down, z_three, 
				     NULL, 1, -3);
  z_three->down->left = z_three->down->right;
  z_one->right = z_three;

  printf("Data structure created\n");
  return 0;
}

poly_t *create_poly(poly_t *left, poly_t *right,  poly_t *up,
		    poly_t *down, int exp, val_t val) {
  /* Allocate a new polynomial and fill the structure with values */
  poly_t *poly = (poly_t*)malloc(sizeof(poly_t));
  poly->left = left;
  poly->right = right;
  poly->up = up;
  poly->down = down;
  poly->val = val;
  poly->exp = exp;

  return poly;
}

poly_t *create_root (val_t val) {
  /* Create the root element of a polynomial */
  poly_t *poly = (poly_t*)malloc(sizeof(poly_t));
  poly->up = NULL;
  poly->left = poly;
  poly->right = poly;
  
  return poly;
}

void print_val (poly_t *poly) {
  /* Print the value of a polynomial */

  return;
}
