Program to create linked list and basic operations in C++.

#include<iostream>
using namespace std;
class Polynomial
{
struct node
{
int coeff,a,b,c;
struct node * next;
};
//definition of the structure node
char ans;
int flag;
public:
struct node * head,* tail,* current_ptr,* s,* start,* corpse;
Polynomial()
{
head=new node;
current_ptr=head;
start=current_ptr;
}
//constructor
void getdatafor_node(struct node *);
void insert_node();
Polynomial add_two_polynomials(Polynomial,Polynomial);

struct node * Previous(struct node * current_ptr)
{
struct node * temp=start;
while(temp->next != current_ptr)
{
temp=temp->next;
}
return temp;
}
//to find the previous node of the current pointer
};
//definition of class

void Polynomial::insert_node()
{
ans='y';
struct node * temp;
while(ans=='y')
{
temp=new node;
getdatafor_node(temp);
current_ptr->next=temp;
current_ptr=current_ptr->next;
cout<<"continue?(y/n):";
cin>>ans;
}
if(ans=='n')
{
start=start->next;
current_ptr->next=start;
}
}
//for inserting a node in the circular linked list

void Polynomial::getdatafor_node(struct node * temp)
{
cout<<"Enter the input for the node"<<endl;
cout<<"Enter the coefficient:";
cin>>temp->coeff;
cout<<endl;
cout<<"Enter the exponents of x,y,z:";
cin>>temp->a>>temp->b>>temp->c;
}
//to get the data from the user

Polynomial Polynomial::add_two_polynomials(Polynomial P,Polynomial Q)
{
current_ptr=current_ptr->next;
start=current_ptr;
Q.current_ptr=Q.current_ptr->next;
Q.start=Q.current_ptr;
//intialising

if(current_ptr->a==Q.current_ptr->a && current_ptr->b==Q.current_ptr->b && current_ptr->c==Q.current_ptr->c)
{
flag=0;
Q.current_ptr->coeff=Q.current_ptr->coeff+current_ptr->coeff;
}
else
{
flag=1;
Q.current_ptr=Q.current_ptr->next;
}
//to compare first nodes of both

while(Q.current_ptr!=Q.start)
{
if(current_ptr->a==Q.current_ptr->a && current_ptr->b==Q.current_ptr->b && current_ptr->c==Q.current_ptr->c)
{
flag=0;
Q.current_ptr->coeff=Q.current_ptr->coeff+current_ptr->coeff;
Q.current_ptr=Q.start;
}
else
{
flag=1;
Q.current_ptr=Q.current_ptr->next;
}
}
//compare first node of P with Q

if(flag==1)
{
Q.s=Q.Previous(Q.current_ptr);
cout<<(Q.Previous(Q.current_ptr))->coeff;
current_ptr=current_ptr->next;
Q.s->next=Previous(current_ptr);
(Previous(current_ptr))->next=Q.current_ptr;
Q.current_ptr=Q.current_ptr->next;
}
//inserting the node of P in Q

else
current_ptr=current_ptr->next;

while(current_ptr!=start)
{
if(current_ptr->a==Q.current_ptr->a && current_ptr->b==Q.current_ptr->b && current_ptr->c==Q.current_ptr->c)
{
flag=0;
Q.current_ptr->coeff=Q.current_ptr->coeff+current_ptr->coeff;
}
else
{
flag=1;
Q.current_ptr=Q.current_ptr->next;
}

while(Q.current_ptr!=Q.start)
{
if(current_ptr->a==Q.current_ptr->a && current_ptr->b==Q.current_ptr->b && current_ptr->c==Q.current_ptr->c)
{
flag=0;
Q.current_ptr->coeff=Q.current_ptr->coeff+current_ptr->coeff;
Q.current_ptr=Q.start;
}
else
{
flag=1;
Q.current_ptr=Q.current_ptr->next;
}
}
if(flag==1)
{
Q.s=Q.Previous(Q.current_ptr);
cout<<(Q.Previous(Q.current_ptr))->coeff;
current_ptr=current_ptr->next;
cout<<current_ptr->coeff;
Q.s->next=Previous(current_ptr);
(Previous(current_ptr))->next=Q.current_ptr;
cout<<Q.current_ptr->coeff;
Q.current_ptr=Q.current_ptr->next;
}
else
current_ptr=current_ptr->next;
}
cout<<Q.current_ptr->coeff;
cout<<current_ptr->coeff;
return Q;
}
main()
{
Polynomial P,Q,R;
P.insert_node();
Q.insert_node();
R=P.add_two_polynomials(P,Q);
R.current_ptr=R.start;
cout<<(R.Previous(R.current_ptr))->coeff;
while(R.current_ptr->next!=R.start)
{
cout<<"Print the coefficient of resulting polynomial:"<<R.current_ptr->coeff<<endl;
cout<<"Print the exponent of x:"<<R.current_ptr->a<<endl;
cout<<"Print the exponent of y:"<<R.current_ptr->b<<endl;
cout<<"Print the exponent of z:"<<R.current_ptr->c<<endl;
R.current_ptr=R.current_ptr->next;
}
cout<<"Print the coefficient of resulting polynomial:"<<R.current_ptr->coeff<<endl;
cout<<"Print the exponent of x:"<<R.current_ptr->a<<endl;
cout<<"Print the exponent of y:"<<R.current_ptr->b<<endl;
cout<<"Print the exponent of z:"<<R.current_ptr->c<<endl;
//to print the resulting polynomial
}
Advertisements

One thought on “Program to create linked list and basic operations in C++.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s