0% found this document useful (0 votes)
5 views

8. programs polynomial sparse and two array in linked list

Uploaded by

asodariadhruv
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

8. programs polynomial sparse and two array in linked list

Uploaded by

asodariadhruv
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Array 2D in linked list

#include <cstdlib>

#include <iostream>

# include<stdlib.h>

using namespace std;

struct node

int size;

int a[10][10];

struct node* next;

}*head,*newnode,*tail,*temp;

int n;

void insert()

newnode=(struct node *)malloc(sizeof(struct node));

cout<<"Enter number of elements in array"<<endl;

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();

Sparse matrix implementation using linked list

#include <cstdlib>

#include <iostream>

# include<stdlib.h>

using namespace std;

struct node

int row;

int col;

int val;

struct node* next;

}*head,*tail,*temp,*newnode;

void insert(int r, int c,int input)

newnode=(struct node *)malloc(sizeof(struct node));

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();

Polynomial expression in linked list

#include <cstdlib>

#include <iostream>

# include<stdlib.h>

using namespace std;

struct node

int coeff;

int expo;

struct node* next;

}*head,*tail,*temp,*newnode;

void insert(int c, int e)


{

newnode=(struct node *)malloc(sizeof(struct node));

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();

Polynomial addition without using class

# include<iostream>

# include<stdlib.h>

using namespace std;

struct node

int coeff;

int expo;

struct node* next;

}*head,*newnode,*temp,*tail;

struct node* insert(struct node*,int,int);

void display(struct node*);

struct node* insert(struct node* head,int c,int exp)

newnode=(struct node*)malloc(sizeof(struct node));

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;

void display(struct node* 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)

if(poly1==NULL && poly2==NULL)

cout<<"list is empty";

else

while(poly1!=NULL && poly2!=NULL)

poly3=insert(poly3,poly1->coeff+poly2->coeff,poly1->expo);

poly1=poly1->next;

poly2=poly2->next;

return poly3;

int main()

struct node* poly1=NULL;

struct node* poly2=NULL;

struct node* poly3=NULL;

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

6X^2 + 3X^1 + 4X^0

polynomail 2

3X^2 + 7X^1 + 2X^0

polynomail 3

9X^2 + 10X^1 + 6X^0

--------------------------------

Process exited after 2.163 seconds with return value 0

Press any key to continue . . .

You might also like