Hi,
please help me with this program that i am writing. i am writing a program to create a polynomial. i set the values, but whenever, i debug it, i get this:
4x^2 + 0X^1 + 0x^0 + which do not correspond to the values that i set it to. it's not reading the second and 3rd coefficients.
i expect to get 4x^2 + 5x + 6.
please help me . also, how do i get rid of the plus that comes after the polynomial.

#include<iostream>
using namespace std;
//A class to handle a list node
class Lnode{
public:
	Lnode();
	Lnode(int);
int data;
Lnode *next;//This is the pointer to the next node
};

Lnode::Lnode(){
	data = 0;
	next = NULL;
}
Lnode::Lnode(int d){
	data = d;
	next = NULL;
}

class LList{
public:
	LList();
	LList(int);
	int getSize();
	void print();
	void printP();
	void add(LList L1);
	void set(int, int);
	
private:
	void addToF(int);//adding a number to the front of the list. 
	Lnode *head, *tail, *current;//a list has two pointers, head and tail.
	//both are pointing to the already defined List node.
	int size, degree;
};
//Constructor Definition
LList::LList(){
	size = 0;
	head = tail = NULL;//initially, there is nothing. none of them exist.
}
LList::LList(int n){
	head=tail=NULL;
	size = n;
	for(int i=size; i>0; i--)
		addToF(0);
}
int LList::getSize(){
	return size;//to know the size of the list
}
void LList::addToF(int d){
Lnode *nnode = new Lnode(d);//pointer to the list node
	   nnode -> next=head;

		head = nnode;
		if(tail==NULL)
		//size++;
	}

void LList::print(){
	Lnode *current = head;

	while( current!= NULL)
	{
		cout<<current->data<<"  ";
		current = current->next;
	}

}
void LList::printP(){
	current = head;
	degree = size-1;
	while( current != NULL)
	{
		cout<<current->data<<"x^"<<degree<<" "<<"+";
		current = current->next;
		degree--;
			}
	}


void LList::set(int c, int val)
{
	int i = size;
	current = head;
		if(i==(c+1))
		{
			current->data = val;
			//cout<<current->data;
		}
		else
		{
			current = current->next;
			i--;
		}
	}


/*void LList::add(LList L1){
	//Lnode *newnode = new Lnode;
	//int data;
	Lnode *tempL2;
	Lnode *tempL1;
	tempL2 = tail;
	tempL1 = L1.tail;
	tempL2->data = tempL1->data + tempL2->data;
	tempL1 = tempL1->next;
	tempL2 = tempL2->next;
	//return newnode->data;
}*/
	


//Driver Main Program
void main(){
LList a(3);//a is an object
//cout<<a.getSize()<<endl;
	//cout<<" "<<endl;
	a.set(2, 4);
	a.set(1, 5);
	a.set(0, 6);
	a.printP();
	cout<<endl;
	//a.printx();
	cout<<endl;
	
	/*LList b(3);
	//b.print();
	//b.Sizen(3);
	b.set(2, 4);
	b.set(1, 6);
	b.set(0, 9);
	b.printP();
	cout<<endl;
	//b.add(a);
	//cout<<endl;*/
	

}

thank you very much for your help.

I don't know the details of what this function is trying to do, nor do I have the energy to try to figure it out, but something looks wrong with your set() function here:

void LList::set(int c, int val)
{
	int i = size;
	current = head;
		if(i=(c+1))

I'm guessing you typed '=' when you meant '=='. And if you did mean '=', then why did you initialize 'i' to 'size' at the beginning of the function?

Hi,
my add function only adds the last element in the polynomial. it does not add each 1.
please help.

void LList::add(LList L1){
	Lnode *ptrL;
	Lnode *ptrL1;
	ptrL = tail;
	ptrL1 = L1.tail;
	ptrL->data = ptrL->data+ ptrL1->data;
	ptrL = ptrL->next;
	ptrL1 = ptrL1->next;
	
	
}

yes, thanks, i already fixed that. but please can you help me with the add function. it's a part of this thread.
thanks.

I don't know the details of what this function is trying to do, nor do I have the energy to try to figure it out, but something looks wrong with your set() function here:

void LList::set(int c, int val)
{
	int i = size;
	current = head;
		if(i=(c+1))

I'm guessing you typed '=' when you meant '=='. And if you did mean '=', then why did you initialize 'i' to 'size' at the beginning of the function?

You sound far too urgent. What you need to realize is that 'urgent' for you does not mean 'urgent' for us. People aren't going to respond to your thread any faster than they normally would (and in fact they may reply slower or not at all).

Anyway, I'm a bit confused with your code. What exactly is add() supposed to do? Is it supposed to add a node to the linked list? Is it supposed to add data nodes together? Explain in more detail.

And what is this supposed to do?

ptrL = ptrL->next;
	ptrL1 = ptrL1->next;

Before this, ptrL and ptrL1 pointed to the tails of their respective linked lists, did they not? Because if they did, they're now pointing to nothing, if you've set the next pointers at the end of the list to null (which is the normal way of implementing a linked list).

make the following changes to the set function.

void LList::set(int c, int val)
{
	int i = size-1;
	current = head;
	while ( current != NULL )
	{
		if(i==c)
		{
			current->data = val;
			//cout<<current->data;
			break;
		}
		else
		{
			current = current->next;
			i--;
		}
	}
}

You can easily debug these type of programs by using cout . Next time when you are programming, check function by function first before debugging the whole program at once. Also, you are using too many member variables.

  • You are not using tail anywhere.
  • current and degree should not be member variables. They should be variables defined inside the functions. Having current and degree as member variables will give you some very hard to find bugs because they can be used and updated from any function and it is hard to keep track.

Figure out the way to stop printing the last + character. It can be easily done by having a check to find out if current->next is null or not.

Comments
This article has been dead for over six months. Start a new discussion instead.