943,810 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1153
  • C++ RSS
Feb 8th, 2008
0

Linked List - Please help needed Urgently!!!

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include<iostream>
  3. using namespace std;
  4. //A class to handle a list node
  5. class Lnode{
  6. public:
  7. Lnode();
  8. Lnode(int);
  9. int data;
  10. Lnode *next;//This is the pointer to the next node
  11. };
  12.  
  13. Lnode::Lnode(){
  14. data = 0;
  15. next = NULL;
  16. }
  17. Lnode::Lnode(int d){
  18. data = d;
  19. next = NULL;
  20. }
  21.  
  22. class LList{
  23. public:
  24. LList();
  25. LList(int);
  26. int getSize();
  27. void print();
  28. void printP();
  29. void add(LList L1);
  30. void set(int, int);
  31.  
  32. private:
  33. void addToF(int);//adding a number to the front of the list.
  34. Lnode *head, *tail, *current;//a list has two pointers, head and tail.
  35. //both are pointing to the already defined List node.
  36. int size, degree;
  37. };
  38. //Constructor Definition
  39. LList::LList(){
  40. size = 0;
  41. head = tail = NULL;//initially, there is nothing. none of them exist.
  42. }
  43. LList::LList(int n){
  44. head=tail=NULL;
  45. size = n;
  46. for(int i=size; i>0; i--)
  47. addToF(0);
  48. }
  49. int LList::getSize(){
  50. return size;//to know the size of the list
  51. }
  52. void LList::addToF(int d){
  53. Lnode *nnode = new Lnode(d);//pointer to the list node
  54. nnode -> next=head;
  55.  
  56. head = nnode;
  57. if(tail==NULL)
  58. //size++;
  59. }
  60.  
  61. void LList::print(){
  62. Lnode *current = head;
  63.  
  64. while( current!= NULL)
  65. {
  66. cout<<current->data<<" ";
  67. current = current->next;
  68. }
  69.  
  70. }
  71. void LList::printP(){
  72. current = head;
  73. degree = size-1;
  74. while( current != NULL)
  75. {
  76. cout<<current->data<<"x^"<<degree<<" "<<"+";
  77. current = current->next;
  78. degree--;
  79. }
  80. }
  81.  
  82.  
  83. void LList::set(int c, int val)
  84. {
  85. int i = size;
  86. current = head;
  87. if(i==(c+1))
  88. {
  89. current->data = val;
  90. //cout<<current->data;
  91. }
  92. else
  93. {
  94. current = current->next;
  95. i--;
  96. }
  97. }
  98.  
  99.  
  100. /*void LList::add(LList L1){
  101. //Lnode *newnode = new Lnode;
  102. //int data;
  103. Lnode *tempL2;
  104. Lnode *tempL1;
  105. tempL2 = tail;
  106. tempL1 = L1.tail;
  107. tempL2->data = tempL1->data + tempL2->data;
  108. tempL1 = tempL1->next;
  109. tempL2 = tempL2->next;
  110. //return newnode->data;
  111. }*/
  112.  
  113.  
  114.  
  115. //Driver Main Program
  116. void main(){
  117. LList a(3);//a is an object
  118. //cout<<a.getSize()<<endl;
  119. //cout<<" "<<endl;
  120. a.set(2, 4);
  121. a.set(1, 5);
  122. a.set(0, 6);
  123. a.printP();
  124. cout<<endl;
  125. //a.printx();
  126. cout<<endl;
  127.  
  128. /*LList b(3);
  129. //b.print();
  130. //b.Sizen(3);
  131. b.set(2, 4);
  132. b.set(1, 6);
  133. b.set(0, 9);
  134. b.printP();
  135. cout<<endl;
  136. //b.add(a);
  137. //cout<<endl;*/
  138.  
  139.  
  140. }
thank you very much for your help.
Last edited by olams; Feb 8th, 2008 at 9:57 pm.
Reputation Points: 12
Solved Threads: 0
Light Poster
olams is offline Offline
35 posts
since Feb 2007
Feb 8th, 2008
0

Re: Linked List - Please help needed Urgently!!!

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)
  1. void LList::set(int c, int val)
  2. {
  3. int i = size;
  4. current = head;
  5. 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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 8th, 2008
0

Re: Linked List - Please help needed Urgently!!!(modified)

Hi,
my add function only adds the last element in the polynomial. it does not add each 1.
please help.
C++ Syntax (Toggle Plain Text)
  1. void LList::add(LList L1){
  2. Lnode *ptrL;
  3. Lnode *ptrL1;
  4. ptrL = tail;
  5. ptrL1 = L1.tail;
  6. ptrL->data = ptrL->data+ ptrL1->data;
  7. ptrL = ptrL->next;
  8. ptrL1 = ptrL1->next;
  9.  
  10.  
  11. }
Reputation Points: 12
Solved Threads: 0
Light Poster
olams is offline Offline
35 posts
since Feb 2007
Feb 8th, 2008
0

Re: Linked List - Please help needed Urgently!!!

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:

C++ Syntax (Toggle Plain Text)
  1. void LList::set(int c, int val)
  2. {
  3. int i = size;
  4. current = head;
  5. 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?
Reputation Points: 12
Solved Threads: 0
Light Poster
olams is offline Offline
35 posts
since Feb 2007
Feb 8th, 2008
0

Re: Linked List - Please help needed Urgently!!!

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?
C++ Syntax (Toggle Plain Text)
  1. ptrL = ptrL->next;
  2. 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).
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 8th, 2008
1

Re: Linked List - Please help needed Urgently!!!

make the following changes to the set function.

cpp Syntax (Toggle Plain Text)
  1. void LList::set(int c, int val)
  2. {
  3. int i = size-1;
  4. current = head;
  5. while ( current != NULL )
  6. {
  7. if(i==c)
  8. {
  9. current->data = val;
  10. //cout<<current->data;
  11. break;
  12. }
  13. else
  14. {
  15. current = current->next;
  16. i--;
  17. }
  18. }
  19. }

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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help stopping a while loop
Next Thread in C++ Forum Timeline: nested template help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC