Erstellen Sie ein Polynom mit eine verknüpfte Liste in C

Erstelle ich ein Polynom verwenden Sie eine verknüpfte Liste in C, und ich bin ein problem. Kann mir bitte jemand helfen mit meinem code? In der create Funktion, die ich gerade erstellt einen Knoten, und ich möchte legen Sie die Knoten in der richtigen position in die Funktion insert, und dann möchte ich das Polynom p1 zurückgegeben werden.

Ich bin auch nicht in der Lage zu verstehen, wie die return - Anweisung, die zur Arbeit gehen. Bitte sagen Sie mir, was die Fehler im code sind mit meinem Ansatz.

struct node
{
    int cof;
    int exp;
    struct node *link;
};
struct node * create(struct node *q)
 {
  int i,n;
  printf("enter the number of nodes");
  scanf("%d",&n);
  struct node *ptr=(struct node *)malloc (sizeof(struct node));
  for(i=0;i<n;i++)
  {
                  printf("entre the coefficient and exponent respectivly");
                  scanf("%d%d",&ptr->cof,&ptr->exp);
                  ptr->link=NULL;
                  q=insert(ptr,q);
  }
  return q;
 }
struct node * insert(struct node *ptr,struct node *p)
 {
  struct node *temp,*b;
  if(p==NULL)
  p=ptr;
  else
  {
      if((p->exp)<(ptr->exp))
      {
                              ptr->link=p;
                              p=ptr;
      }
      else
      {
          temp=p;
          while((temp!=NULL)||((temp->link->exp)<(ptr->exp)))
          temp=temp->link;
          b=temp->link;
          temp->link=ptr;
          ptr->link=b;
      }
  }
  return p;
  }
void display(struct node *ptr)
 {
   struct node *temp;
  temp=ptr;
  while(temp!=NULL)
  {
                   printf("%d x ^ %d + ",temp->cof,temp->exp);
                   temp=temp->link;
  }
 }

 int main()
  {
  printf("enter the first polynomial");
  struct node *p1=NULL,*p2=NULL; 
  p1=(struct node *)malloc(sizeof(struct node));
  p2=(struct node *)malloc(sizeof(struct node));

  p1=create(p1);

  printf("entr secon dpolynimial");
  create(p2);

  display(p1);
  display(p2);

  getch();
  return 0;
 }
  • Frage 1: wie viele Knoten, die Sie einfügen möchten? Question2: wie viele wollen Sie reservieren ?
InformationsquelleAutor user2883824 | 2013-11-16
Schreibe einen Kommentar