8. programs polynomial sparse and two array in linked list
8. programs polynomial sparse and two array in linked list
#include <cstdlib>
#include <iostream>
# include<stdlib.h>
struct node
int size;
int a[10][10];
}*head,*newnode,*tail,*temp;
int n;
void insert()
cin>>n;
newnode->size=n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>newnode->a[i][j];
newnode->next=NULL;
if(head==NULL)
head=newnode;
tail=head; //tail=newnode;
else
tail->next=newnode;
tail=tail->next; // tail=newnode;
void display()
if(head==NULL)
cout<<"list is empty"<<endl;
else
cout<<"elements in list"<<endl;
temp=head;
while(temp!=NULL)
for(int i=0;i<temp->size;i++)
for(int j=0;j<temp->size;j++)
cout<<temp->a[i][j]<<" ";
cout<<endl;
}
temp=temp->next;
int main()
{ head=NULL;
insert();
display();
insert();
display();
insert();
display();
#include <cstdlib>
#include <iostream>
# include<stdlib.h>
struct node
int row;
int col;
int val;
}*head,*tail,*temp,*newnode;
newnode->row=r;
newnode->col=c;
newnode->val=input;
newnode->next=NULL;
if(head==NULL)
head=newnode;
tail=head;
else
tail->next=newnode;
tail=newnode;
void display()
if(head==NULL)
cout<<"list is empty"<<endl;
else
for(temp=head;temp!=tail->next;temp=temp->next)
cout<<temp->row<<"--"<<temp->col<<"--"<<temp->val<<" | ";
int main()
int a[3][3]={{0,0,1},{2,2,0},{9,0,0}};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(a[i][j]!=0)
insert(i,j,a[i][j]);
display();
#include <cstdlib>
#include <iostream>
# include<stdlib.h>
struct node
int coeff;
int expo;
}*head,*tail,*temp,*newnode;
newnode->coeff=c;
newnode->expo=e;
newnode->next=NULL;
if(head==NULL)
head=newnode;
tail=head;
else
tail->next=newnode;
tail=newnode;
void display()
if(head==NULL)
cout<<"list is empty"<<endl;
else
for(temp=head;temp!=tail->next;temp=temp->next)
cout<<temp->coeff<<"X^"<<temp->expo;
if(temp->next!=NULL)
cout<<"+";
else
cout<<endl;
}
}
int main()
head=NULL;
insert(6,2);
insert(3,1);
insert(9,0);
display();
# include<iostream>
# include<stdlib.h>
struct node
int coeff;
int expo;
}*head,*newnode,*temp,*tail;
newnode->coeff=c;
newnode->expo=exp;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
return head;
if(head==NULL)
cout<<"List is empty"<<endl;
else
temp=head;
while(temp!=NULL)
cout<<temp->coeff<<"X^"<<temp->expo;
temp=temp->next;
if(temp!=NULL)
cout<<" + ";
}
}
cout<<endl;
struct node* addition(struct node* poly1, struct node* poly2, struct node* poly3)
cout<<"list is empty";
else
poly3=insert(poly3,poly1->coeff+poly2->coeff,poly1->expo);
poly1=poly1->next;
poly2=poly2->next;
return poly3;
int main()
cout<<"polynomail 1"<<endl;
poly1=insert(poly1,6,2);
poly1=insert(poly1,3,1);
poly1=insert(poly1,4,0);
display(poly1);
cout<<"polynomail 2"<<endl;
poly2=insert(poly2,3,2);
poly2=insert(poly2,7,1);
poly2=insert(poly2,2,0);
display(poly2);
cout<<"polynomail 3"<<endl;
poly3=addition(poly1,poly2,poly3);
display(poly3);
Output
polynomail 1
polynomail 2
polynomail 3
--------------------------------