struct node *insert(struct node *p);
void *add (struct node *a,struct node *b);
#include<stdio. h>
#include<conio. h>
#include<alloc. h>
struct node
{
int cof,ex,ey,ez;
struct node *link;
};
void main ()
{
struct node *a,*b,*q;
int i,ter1,ter2;
clrscr();
printf("\n\nNo of terms you want to enter for first polynomial");
scanf("%d",&ter 1);
printf("\n\nNo of terms you want to enter for second polynomial");
scanf("%d",&ter 2);
a=NULL;
b=NULL;
for(i=0;i<ter1; i++)
{
a=insert(a);
}
for(i=0;i<ter2; i++)
{
b=insert(b);
}
printf("\n\nFor first polynomial");
q=a;
for(i=0;i<ter1; i++)
{
printf("\n\nEnt er the coffecient & exponent of x,y,z of %d term :",i+1);
scanf("%d%d%d%d ",&q->cof,&q->ex,&q->ey,&q->ez);
q=q->link;
}
printf("\n\nFor second polynomial");
q=b;
for(i=0;i<ter2; i++)
{
printf("\n\nEnt er the coffecient & exponent of x,y,z of %d term :",i+1);
scanf("%d%d%d%d ",&q->cof,&q->ex,&q->ey,&q->ez);
q=q->link;
}
printf("\n\nFir st polynomial is :\n\n");
q=a;
for(i=0;i<ter1; i++)
{
printf("%d(X^%d Y^%dZ^%d) + ",q->cof,q->ex,q->ey,q->ez);
q=q->link;
}
printf("\n\nSec ond polynomial is :\n\n");
q=b;
for(i=0;i<ter2; i++)
{
printf("%d(X^%d Y^%dZ^%d) + ",q->cof,q->ex,q->ey,q->ez);
q=q->link;
}
add(a,b);
getch();
}
struct node *insert(struct node *p)
{
struct node *temp;
if(p==NULL)
{
temp=(struct node*)malloc(si zeof(struct node));
p=temp;
temp->link=NULL;
}
else
{
temp=p;
temp->link=(struct node*)malloc(si zeof(struct node));
temp=temp->link;
temp->link=NULL;
}
return(p);
}
void *add(struct node *a,struct node *b)
{
struct node *q,*p,*c,*temp;
int i=0,j;
p=a;
q=b;
c=NULL;
while(p->link!=NULL && q->link!=NULL)
{
if(p->ex==q->ex && p->ey==q->ey && p->ez==q->ez)
{
c=insert(c) ;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof + q->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
temp->link=NULL;
i=i+1;
p=p->link;
q=q->link;
}
else
{
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
c=insert(c);
temp->link=insert(c) ;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=q->cof;
temp->ex=q->ex;
temp->ey=q->ey;
temp->ez=q->ez;
i=i+2;
}
}
if(p->link==NULL && q->link!=NULL)
{
if(p->ex==q->ex && p->ey==q->ey && p->ez==q->ez)
{
c=insert(c) ;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=insert(c) ;
temp->cof=p->cof + q->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
temp->link=NULL;
i=i+1;
q=q->link;
}
else
{
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=insert(c) ;
temp=temp->link;
temp->cof=p->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
c=insert(c);
temp->link=insert(c) ;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=q->cof;
temp->ex=q->ex;
temp->ey=q->ey;
temp->ez=q->ez;
i=i+2;
q=q->link;
}
do
{
temp->link=insert(c) ;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=q->cof;
temp->ex=q->ex;
temp->ey=q->ey;
temp->ez=q->ez;
i=i+1;
q=q->link;
}while(q->link!=NULL);
}
if(q->link==NULL)
{
if(p->ex==q->ex && p->ey==q->ey && p->ez==q->ez)
{
c=insert(c) ;
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof + q->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
temp->link=NULL;
i=i+1;
p=p->link;
}
else
{
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=q->cof;
temp->ex=q->ex;
temp->ey=q->ey;
temp->ez=q->ez;
i=i+2;
p=p->link;
}
do
{
c=insert(c);
temp=c;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->cof=p->cof;
temp->ex=p->ex;
temp->ey=p->ey;
temp->ez=p->ez;
i=i+1;
}while(p->link!=NULL);
}
printf("\n\n");
temp=c;
for(j=0;j<i;j++ )
{
printf("%d(X^%d Y^%dZ^%d)+",tem p->cof,temp->ex,temp->ey,temp->ez);
temp=temp->link;
}
}
Help me to solve this question
2 4746
I think you are storing each term of the polynomial in a new node in a linked list. By using the below function you are creating the nodes for storing the terms of the polynomial.
struct node *insert(struct node *p)
{
struct node *temp;
if(p==NULL)
{
temp=(struct node*)malloc(si zeof(struct node));
p=temp;
temp->link=NULL;
}
else
{
temp=p;
temp->link=(struct node*)malloc(si zeof(struct node));
temp=temp->link;
temp->link=NULL;
}
return(p);
}
When you call this function for first two times, it works properly. But third time onwards, it will not work.
Let us assume that you require 4 nodes to store the terms of the polynomial and list is created upto the second node. When the above function is called for creating the third node it does the following...
1. The execution control will go the else portion and assign the head of the list to temp variable.
2. Now you are creating another node in the link portion where already a node exists. So the new node will overwrite this portion and you will ultimately get two nodes irrespective of the number of calls you made to this function.
So change the function to..........
struct node *insert(struct node *p)
{
if(p==NULL)
{
p=(struct node*)malloc(si zeof(struct node));
p->link=NULL;
}
else
{
p->link=insert( p->link);
}
return(p);
}
OR
struct node *insert(struct node *p)
{
struct node *temp;
if(p==NULL)
{
temp=(struct node*)malloc(si zeof(struct node));
p=temp;
temp->link=NULL;
}
else
{
temp=p;
while(temp->link != NULL) /* This loop will take you to the last node */
temp=temp->link;
temp->link=(struct node*)malloc(si zeof(struct node));
temp=temp->link;
temp->link=NULL;
}
return(p);
}
Thank you for pointing out the error
I want to multiply these two polynomial
Code for multipliction is
void *multiply(struc t node *a,struct node *b,int ter1,int ter2)
{
struct node *p,*q,*c,*temp;
int i,j;
c=NULL;
p=a;
q=b;
for(i=0;i<ter1* ter2;i++)
{
c=insert(c);
}
temp=c;
for(i=0;i<ter1; i++)
{
for(j=0;j<ter2; j++)
{
temp->cof=p->cof*q->cof;
temp->ex=p->ex+q->ex;
temp->ey=p->ey+q->ey;
temp->ez=p->ez+q->ez;
temp=temp->link;
q=q->link;
}
q=b;
temp=temp->link;
p=p->link;
}
printf("\n\n ");
temp=c;
for(i=0;i<(ter1 *ter2);i++)
{
printf("%d(X^%d Y^%dZ^%d)+",tem p->cof,temp->ex,temp->ey,temp->ez);
temp=temp->link;
}
}
please help to solve this.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: C++fan |
last post by:
Suppose that I define the following class:
class example_class{
public:
example_class();
void funtion_1();
void function_2();
protected:
|
by: Dream Catcher |
last post by:
1. I don't know once the node is located, how to return that node.
Should I return pointer to that node or should I return the struct of that
node.
2. Also how to do the fn call in main for that LOCATE subroutine that
returns a node????
Any help would be appreciated.
Thanks
|
by: Kent |
last post by:
Hi!
I want to store data (of enemys in a game) as a linked list, each node will
look something like the following:
struct node
{
double x,y; // x and y position coordinates
struct enemy *enemydata; // Holds information about an enemy (in a game)
// Its a double linked list node
|
by: Steve Lambert |
last post by:
Hi,
I've knocked up a number of small routines to create and manipulate a linked
list of any structure. If anyone could take a look at this code and give me
their opinion and details of any potential pitfalls I'd be extremely
grateful.
Cheers
Steve
|
by: Eugen J. Sobchenko |
last post by:
Hi!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition
should be kept for references to previous element of list.
Here is my solution below:
struct node {
| |
by: joshd |
last post by:
Hello,
Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1 and list2, each
node within list1 has various data and needs to have a pointer to the
corresponding node in list2, but I cant figure out how to do...
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort for
linked lists, I couldn't find one. I read something about Mcilroy's
"Optimistic Merge Sort" and studied some implementation, but they
were for...
|
by: theeverdead |
last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like:
Trevor Johnson
Kevin Smith
Allan Harris
I need to read that file into program and then turn it into a linked list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the...
|
by: Atos |
last post by:
SINGLE-LINKED LIST
Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be anything from a short integer value to a complex struct type, also has a pointer to
the next node in the single-linked list. That pointer will be NULL...
|
by: QiongZ |
last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |