Thread: How to make the following program work?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    14

    How to make the following program work?

    How to make the following program work? I think there are some place of the following program use the address NULL. But I don't know how to fix the problem. Thank you.

    Code:
    #include <stdlib.h>
    
    #include <stdio.h>
    
    #include <string.h>
    
    #include <malloc.h>
    
    #include <math.h>
    
     
    
    typedef struct node /*a node denotes a P_{i,k}^{l}*/
    
    {
    
     int l,i,k;
    
    } node;
    
     
    
     
    
    typedef struct monomial_node /* a monomial denotes a monomial P_{i,k}^{l} \cdots P_{i',k'}^{l'} */
    
    {
    
     node y;
    
     int coeff;         /* coeff is s(m_r) */
    
     struct monomial_node *next; 
    
     struct monomial_node *head;
    
     struct monomial_node *tail; 
    
    } monomial_node, *monomial;
    
     
    
     
    
     
    
    typedef struct polynomial_node /* a polynomial denotes a sum of some monomials */
    
    {
    
     monomial m;
    
     struct polynomial_node *next; 
    
     struct polynomial_node *head;
    
     struct polynomial_node *tail;
    
    } polynomial_node, *polynomial;
    
     
    
     
    
    monomial insert(node a, monomial b)          /* insert a factor a to a monomial b such that the first subscripts i's in the new monomial is in increasing order */
    
    {
    
     monomial p;     
    
     monomial_node *s;
     
     monomial_node *p1;
     
     
     p=b;
     p1=p->head->next;
    
     while((p1->y).i >= a.i)          /* find the position */
    
     {
    
      p1=p1->next;
    
      if(p1==p->tail)
    
      {
    
        break;
    
      }
    
     }
    
     if(p1==p->tail)
     {
       printf("tail\n");
     }
    
     s=(monomial)malloc(sizeof(monomial_node));        /* create new node */
    
     s->y=a;
    
     s->next=p1->next;     /* insert the node a */
    
     p1->next=s;
    
     return p;
    
    }
    
     
    
     
    
    monomial create_monomial(int number) /* create a monomial */
    
    {
    
       monomial m,s;
    
       monomial p;
    
       int i;
    
     
       m=(monomial)malloc(sizeof(monomial_node));
       m->head=m->tail=(monomial)malloc(sizeof(monomial_node));
    
       if(m->head==NULL)
    
       {
    
         return NULL;
    
       }
    
     
       m->head->next=m->tail;
    
       m->tail->next=NULL;
    
       p=(monomial)malloc(sizeof(monomial_node));
    
       m->head->next=p;
    
       p->next=m->tail;   
    
    
       printf("Please input the parameters of the heighest weight monomial(for example,  2, 2, 3 denotes the monomial Y^{2}_{2,3}). The first subscripts i of Y's should be in increase order.");
    
       scanf("%d", &((p->y).l)); 
       scanf("%d", &((p->y).i));
       scanf("%d", &((p->y).k));
    
      if(number > 1)
      {
       for(i=0;i<number-1;i++)
    
       {
    
        s=(monomial)malloc(sizeof(monomial_node));
    
        scanf("%d", &((s->y).l)); 
        scanf("%d", &((s->y).i));
        scanf("%d", &((s->y).k));
        
    
        
       
        m=insert(s->y,m);
              
    
       }
       
      }
    
     
    
       return m;
    
     
    
    }
    
    
    
    polynomial create_polynomial(monomial n) /* create a polynomial with only one monomial n */
    
    {
    
        polynomial m,p;
        
        m=(polynomial)malloc(sizeof(polynomial_node));
        p=(polynomial)malloc(sizeof(polynomial_node));
        m->head=(polynomial)malloc(sizeof(polynomial_node));
        m->tail=(polynomial)malloc(sizeof(polynomial_node));
    
        m->head->next=p;
        p->m=n;
        p->next=m->tail;
        m->tail->next=NULL;
     
     
    
        return m;
    
     
    
    }
    
    
    
     
    
    void print_monomial(monomial m)
    
    {
    
        monomial p;
        p=(monomial)malloc(sizeof(monomial_node));
    
        p=m->head->next;
    
        if(p==m->tail)
    
        {
    
                 printf("No element. \n");
    
        }
    
        while(p!=m->tail)
    
        {  
    
           printf("Y^{%d}_{%d,%d}",(p->y).l,(p->y).i,(p->y).k); 
    
           p=p->next;  
    
        }
    
    }
    
     
    
    void print_polynomial(polynomial n)
    
    {
    
        polynomial p;
    
        int i=0;
    
        p=n->head->next;
    
        if(p==n->tail)
    
        {
    
                 printf("No element. \n");
    
        }
    
        while(p!=n->tail)
    
        {  
    
           if(i !=0)
    
           {
    
              printf("+");     
    
           }
    
           i++;
    
           print_monomial(p->m);
    
           p=p->next;  
    
        }
    
        printf("\n");
    
    }
    
    
    int length_monomial(monomial a) 
    {
      int len=0;
      monomial_node *b;
      
      b=a->head->next;
      while(b != a->tail)
      {
        len++;
        b=b->next;
      }  
      return len;
    }
    
    
    int length_polynomial(polynomial a) 
    {
      int len=0;
      polynomial_node *b;
      
      b=a->head->next;
      while(b != a->tail)
      {
        len++;
        b=b->next;
      }  
      return len;
    }
    
    
    
     
    int compare_nodes(node a, node b)
    {
      int result;
      if(a.l==b.l && a.i==b.i && a.k==b.k)
      {
        result=1;
      }
      else 
      {
        result=0;
      }
      return result;
    }
    
    
    
    monomial delete_node(node b, monomial a)        
    {
    
     monomial p;    
     monomial_node *p1; 
     
     
     p=a;
     p1=p->head->next;
     p=p->head;
    
     while(p1 != a->tail)
     {
       if(compare_nodes(p1->y, b)==1)
       {
          p->next = p1->next;
          break;
       }
       p=p->next;
       p1=p1->next;
     }
    
     return p;
    
    }
    
    
    int main(int argc, char *argv[])
    
    {
    
     
    
     node p;
    
     
    
     polynomial FM; 
    
     polynomial new_monomials; 
    
     polynomial phi[100][1000];  
    
     
    
     monomial s;
    
     monomial m; 
    
     monomial mm[1000]; 
    
     
    
     int type=1;         
    
     int ra=2;             
    
     int l;
    
     int i,j;
    
     int number;
    
     int a;
    
     int ccc[100][100];
    
     
    
     
    
     
    
     
    
      printf("Please input the number of factors.\n");
    
      scanf("%d", &number);  
    
      
    
      m=create_monomial(number);  
    
     
    
      FM=create_polynomial(m);  
    
      
    
     
    
      print_monomial(m);
    
     
      printf("\n");
    
      print_polynomial(FM);
    
      printf("\n len(m)=%d", length_monomial(m));
      printf("\n len(FM)=%d", length_polynomial(FM));
    
    
      node y,z,w;
      
      y.l=1;y.i=2;y.k=3;
    
      z.l=1;z.i=2;z.k=3;
      w.l=2;w.i=2;w.k=3;
    
      printf("\n y=z? %d\n", compare_nodes(y,z));
       printf("\n y=w? %d\n", compare_nodes(y,w));
      
    
    
      printf("\n delete y in m:\n");
      
      print_monomial(delete_node(y, m));
     
    
     
    
     
    
     return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why did you open up a new thread when you already have one? And have you bothered to take up Salem's suggestions in this post?

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Posts to other thread, please.

    Closed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make this work =(
    By readytogo in forum C Programming
    Replies: 1
    Last Post: 10-28-2010, 09:59 AM
  2. How can I make this program work? Problem with arrays
    By babe20042004 in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2009, 07:14 PM
  3. how can i make this work?
    By Darkozuma in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2008, 08:59 PM
  4. Can't make this work
    By BlahBlahBlah in forum C++ Programming
    Replies: 11
    Last Post: 11-28-2002, 03:45 AM
  5. Replies: 2
    Last Post: 03-25-2002, 05:49 AM