| | |
Linked List - Please help needed Urgently!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 35
Reputation:
Solved Threads: 0
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.
thank you very much for your help.
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.
C++ Syntax (Toggle Plain Text)
#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;*/ }
Last edited by olams; Feb 8th, 2008 at 9:57 pm.
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:
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?
C++ Syntax (Toggle Plain Text)
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?
Last edited by John A; Feb 8th, 2008 at 10:24 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Feb 2007
Posts: 35
Reputation:
Solved Threads: 0
Hi,
my add function only adds the last element in the polynomial. it does not add each 1.
please help.
my add function only adds the last element in the polynomial. it does not add each 1.
please help.
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Feb 2007
Posts: 35
Reputation:
Solved Threads: 0
yes, thanks, i already fixed that. but please can you help me with the add function. it's a part of this thread.
thanks.
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:
C++ Syntax (Toggle Plain Text)
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?
Before this,
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?
C++ Syntax (Toggle Plain Text)
ptrL = ptrL->next; ptrL1 = ptrL1->next;
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). "Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
make the following changes to the set function.
You can easily debug these type of programs by using
Figure out the way to stop printing the last
cpp Syntax (Toggle Plain Text)
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
tailanywhere. -
currentanddegreeshould not be member variables. They should be variables defined inside the functions. Havingcurrentanddegreeas 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. ![]() |
Other Threads in the C++ Forum
- Previous Thread: Help stopping a while loop
- Next Thread: nested template help
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






