Linked List - Please help needed Urgently!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 35
Reputation: olams is an unknown quantity at this point 
Solved Threads: 0
olams olams is offline Offline
Light Poster

Linked List - Please help needed Urgently!!!

 
0
  #1
Feb 8th, 2008
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #2
Feb 8th, 2008
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:

  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.
"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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 35
Reputation: olams is an unknown quantity at this point 
Solved Threads: 0
olams olams is offline Offline
Light Poster

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

 
0
  #3
Feb 8th, 2008
Hi,
my add function only adds the last element in the polynomial. it does not add each 1.
please help.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 35
Reputation: olams is an unknown quantity at this point 
Solved Threads: 0
olams olams is offline Offline
Light Poster

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

 
0
  #4
Feb 8th, 2008
yes, thanks, i already fixed that. but please can you help me with the add function. it's a part of this thread.
thanks.
Originally Posted by joeprogrammer View Post
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:

  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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #5
Feb 8th, 2008
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?
  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).
"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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

 
1
  #6
Feb 8th, 2008
make the following changes to the set function.

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC