473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

polynomials using a linked list

hey im trying to write a class of polynomials using a linked list. But I am getting an error at run time with my Polynomial* getNext() function. The error says access violation reading location. Im not sure what im doing wrong.

Expand|Select|Wrap|Line Numbers
  1.  #ifndef POLYNOMIAL_H
  2. #define POLYNOMIAL_H
  3.  
  4. class Polynomial
  5. {
  6. public:
  7.     Polynomial( ){}
  8.     Polynomial(int theCoefficient);
  9.     Polynomial(int theCoefficient, int thePower);
  10.     Polynomial(Polynomial&);//copy constructor
  11.     Polynomial* getNext( ) {return next;}
  12.     void setNext(Polynomial* polyLoc) {next = polyLoc;}
  13.     double evaluate(double value);
  14.     Polynomial& operator =(Polynomial& rightSide);
  15.     Polynomial operator +(Polynomial& polyObj);
  16.     Polynomial operator -(Polynomial& polyObj);
  17.     Polynomial operator *(Polynomial& polyObj);
  18.     virtual ~Polynomial( );
  19. private:
  20.     int coefficient;
  21.     int power;
  22.     Polynomial* next;
  23.     Polynomial* head;
  24. };
  25. #endif //POLYNOMIAL_H
  26.  
  27.  
  28. //begin Polynomial.cpp 
  29.  
  30. #include <iostream>
  31. #include <math.h>
  32. #include "polynomial.h"
  33.  
  34. using namespace std;
  35.  
  36. Polynomial::Polynomial(int theCoefficient)
  37. {
  38.     head = new Polynomial;
  39.     coefficient = theCoefficient;
  40.     power = 0;
  41.     next = new Polynomial;
  42.     setNext(NULL);
  43. }
  44.  
  45. Polynomial::Polynomial(int theCoefficient, int thePower)
  46. {
  47.     head = new Polynomial;
  48.     coefficient = theCoefficient; 
  49.     power = thePower;
  50.     next = new Polynomial;
  51.     setNext(NULL);
  52. }
  53.  
  54. Polynomial::Polynomial(Polynomial& polyObj)
  55. {/*not completed*/ }
  56.  
  57.  
  58. double Polynomial::evaluate(double value)
  59. {
  60.     double total = 1;
  61.     Polynomial* temp = head;
  62.     while(temp->getNext() != NULL)
  63.     {
  64.         total = total + pow((coefficient*value), power);
  65.         temp = temp->getNext();
  66.     }
  67.  
  68.  
  69. return total;
  70. }
  71.  
  72. Polynomial& Polynomial::operator =(Polynomial& rightSide)
  73. {
  74.     power = rightSide.power;
  75.     coefficient = rightSide.coefficient;
  76.     next = rightSide.next;
  77.  
  78.     return *this;
  79. }
  80.  
  81. Polynomial Polynomial::operator +(Polynomial& polyObj)
  82. {
  83.     Polynomial* temp = head;
  84.     while(temp->getNext() != NULL)
  85.     {
  86.         if (power == polyObj.power)
  87.             coefficient = coefficient + polyObj.coefficient;
  88.         temp = temp->getNext();
  89.     }
  90.  
  91.     return *this;    
  92. }
  93.  
  94. Polynomial Polynomial::operator -(Polynomial& polyObj)
  95. {
  96.     Polynomial* temp = head;
  97.     while(temp->getNext() != NULL)
  98.     {
  99.         if (power == polyObj.power)
  100.             coefficient = coefficient - polyObj.coefficient;
  101.         temp = temp->getNext();
  102.     }
  103.  
  104.     return *this;    
  105. }
  106.  
  107. Polynomial Polynomial::operator *(Polynomial& polyObj)
  108. {
  109.     Polynomial* temp = head;
  110.     while(temp->getNext() != NULL)
  111.     {
  112.         coefficient = coefficient*polyObj.coefficient;
  113.         power = power+polyObj.power;
  114.         temp = temp->getNext();
  115.     }
  116.  
  117.     return *this;    
  118. }
  119.  
  120.  
  121. Polynomial::~Polynomial( )
  122. {
  123.     Polynomial* temp = head;
  124.  
  125.     while(temp->getNext() != NULL)
  126.     {
  127.         temp = temp->getNext();
  128.         delete head;
  129.         head = temp;
  130.     }
  131.  
  132.     delete head;
  133. }

THanks
May 16 '07 #1
7 13925
AdrianH
1,251 Expert 1GB
I would guess that it may have something to do with you not initialising your member variables. ;)


Adrian
May 16 '07 #2
which ones are not initialized? dont my constructors take care of that?

thanks
May 16 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
This is C++, right? Then how come:
Polynomial* next;
Polynomial* head;
this is in your Polynomial class???

It looks like you are using as a node and then somehwere else you use next/head pointers to make a list, etc, etc...

Use a list:
Expand|Select|Wrap|Line Numbers
  1. list<Polynomial> thelist;
  2.  
and remove all the linked list code from your class.

What you have here is spaghetti code. The polynomial and the linked list are interwoven and not separable. If you need a different type in the linked list, you have to recode the linked list again.


The list is already coded for you in the C++ Standard Library. It's a waste of time to recode linked lists over and over.
May 16 '07 #4
AdrianH
1,251 Expert 1GB
This is C++, right? Then how come:


this is in your Polynomial class???

It looks like you are using as a node and then somehwere else you use next/head pointers to make a list, etc, etc...

Use a list:
Expand|Select|Wrap|Line Numbers
  1. list<Polynomial> thelist;
  2.  
and remove all the linked list code from your class.

What you have here is spaghetti code. The polynomial and the linked list are interwoven and not separable. If you need a different type in the linked list, you have to recode the linked list again.


The list is already coded for you in the C++ Standard Library. It's a waste of time to recode linked lists over and over.
That's true. I assumed it was done that way as part of the assignment. ;)


Adrian
May 17 '07 #5
AdrianH
1,251 Expert 1GB
which ones are not initialized? dont my constructors take care of that?

thanks
Only the ones that initialise them. And there is at least one that doesn't.


Adrian
May 17 '07 #6
svlsr2000
181 Expert 100+
It would be great if you could post your other code as well. ie piece of code where your creating and using objects of your class. with just a brief look i found one mistake, which could be source of your problem

Expand|Select|Wrap|Line Numbers
  1. Polynomial::Polynomial(int theCoefficient, int thePower)
  2.  
  3. {
  4.  
  5. head = new Polynomial;
  6.  
  7. coefficient = theCoefficient; 
  8.  
  9. power = thePower;
  10.  
  11. next = new Polynomial; //ERROR 
  12.  
  13. setNext(NULL); //ERROR
  14.  
  15. }
  16.  
  17.  
Here your creating a new object and making next to point to it, but the very next statement your calling setNext with NULL as arguement. This would ground your next pointer, without even using it or freeing it.

The same is the case with other constructor too.
May 17 '07 #7
AdrianH
1,251 Expert 1GB
Only the ones that initialise them. And there is at least one that doesn't.


Adrian
Hint: look at line 7.


Adrian
May 17 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: simpleman | last post by:
Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the...
1
by: zaacze | last post by:
hi! i am new to java and i really have this program i don't have any idea what to do.. here's the problem... A program for a collection of cars using linked list only. Each car comes with...
1
by: unknowncute | last post by:
can you help me make a code about division of polynomials using linked list? thnx!
5
by: blackx | last post by:
I am creating a Polynomial class using linked list. My problem is that my code has gotten some run-time errors (not compile or build errors) when I was running the program. Specifically, I am...
1
by: breadstix2189 | last post by:
Good day to you all, as of now, I am having trouble with this part of my program, I don't know how to save the temporary files (using linked list) to an external file (using txt file) can somebody...
3
by: praveenaj | last post by:
I was given to write a C program using Linked list to enter student marks, but i can't figure out how this node insertion works: how nodes are linked with each other??? A Computer Training...
1
by: monalisa mohanty | last post by:
please help me to complete the below written c++ program code. i have witten the code to insert student's information(case:1) and display it using linked list(case:5). Help me to write the code for...
5
by: basavaraj linga | last post by:
Hi aal, I am trying to draw different shapes in BREW MP. it’s a mob OS by Qualcomm. The programming is in C. The operation is I’ve to draw different shapes on screen based on the selected button....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.