One element multiple polynomial of the data structure chain list

Source: Internet
Author: User

Get as many pointers as possible to avoid using the head node and

/*

1th: Initialization and construction of the list are put together

And the return value with the linklist type

2nd: The summation of the polynomial is essentially the intersection of the linked list

The intersection needs to be noted (the index is large and small on the new linked list, the pointer moves back if the table is empty, directly

The rest of the chain to the new linked list)


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
using namespace Std;
typedef struct N
{
int left;
int right;
}node;

typedef struct NODE1
{
Node point;
struct Node1 *next;
}lnode,*linklist;


linklist makeup (void)//Pay attention to this place
{
int n;
Lnode *p,a,b,c,*s,*l;

L= (linklist) malloc (sizeof (Lnode));
if (l==null)
{
printf ("Allocation failed");
Exit (-1);
}
l->next=null;
s=l;//Note that this will turn into a head node pointing around a circle of nodes
printf ("Please enter the number of nodes (the number of items in the polynomial):");
scanf ("%d", &n);

for (int i=0;i<n;i++)
{
p= (linklist) malloc (sizeof (Lnode));

if (p==null)
{
printf ("Allocation failed");
Exit (-1);
}

printf ("Please enter the coefficients and indices of the linked list separately:");
scanf ("%d%d", &p->point.left,&p->point.right);//I am the big sand than the scanf take the address to forget;
s->next=p;
p->next=null;
S=p;
}

return l;

}


void display (Lnode *l)
{
int i;
Lnode *p;
P=l;
while (P->next!=null)
{
p=p->next;

printf ("---|%d_%d|--", p->point.left,p->point.right);


}
cout<<endl;
}

linklist Add (linklist a,linklist B)
{
Linklist c,p,d,e,s;
C= (linklist) malloc (sizeof (Lnode));
if (c==null)
{
printf ("Allocation failed");
Exit (-1);
}
P=c; d=a->next; e=b->next;
while (D!=null&&e!=null)
{
if (D->point.right > E->point.right)
{
p->next=e;
P=e;
e=e->next;
}
else if (D->point.right < e->point.right)
{
p->next=d;
P=d;
d=d->next;
}

Else
{
D->point.left=d->point.left + e->point.left;
e=e->next;
}

if (d==null)
{
p->next=e;
}
Else
{
p->next=s;
}
}

return C;
}

int Len (linklist l)
{
Linklist p;
p=l->next;
int count=0;
while (P!=null)
{
count++;
p=p->next;
}
return count;
}

int sort (linklist l)
{
Linklist p,q;
Node temp;
int i,j;
For (I=0,p=l->next;i<len (L) -1;i++,p=p->next)//Remember not to write P->count because p->count==1
{
For (J=i+1,q=p->next;j<len (l); j++,q=q->next)

if (P->point.right > Q->point.right)
{
temp=q->point;
q->point=p->point;
p->point=temp;
}
}
return 1;
}

int main ()
{
int n,k;
Lnode p, r,l;
Lnode *a,*b,*c;
A= (linklist) malloc (sizeof (Lnode));
b= (linklist) malloc (sizeof (Lnode));
C= (linklist) malloc (sizeof (Lnode));

printf ("List one: \ n");
A=makeup ();
Sort (a);
Display (a);
cout<<endl;

printf ("linked list II: \ n");
B=makeup ();
Display (b);


C=add (A, b);
printf ("The result of a unary multi-type is: \ n");
Display (c);
return 0;
}

One element multiple polynomial of the data structure chain list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.