943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2150
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 31st, 2007
0

linked list errors

Expand Post »
I have a program that creates a linked list and is required to count the number of elements in the list. This is what I have thus far.
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. struct listrec
  7. {
  8. int value;
  9. struct listrec *next;
  10. };
  11. listrec e1,e2,e3;
  12.  
  13. int listsize(listrec);
  14. struct listrec* temp;
  15.  
  16. int main()
  17. {
  18. e1.value = 4;
  19. e1.next = &e2;
  20. e2.value = 5;
  21. e2.next = &e3;
  22. e3.value = 3;
  23. e3.next = NULL;
  24.  
  25. int sum = 0;
  26. cout << "The list size is " << listsize(*temp);
  27.  
  28. system("pause");
  29. return 0;
  30.  
  31. }
  32.  
  33.  
  34. int listsize(listrec *p)
  35. {
  36. struct listrec* temp = p;
  37. int num=0;
  38. do{
  39. temp = temp -> next;
  40. num++;
  41. }while(temp!= NULL);
  42. return num;
  43.  
  44.  
  45. }
I get the following error on compilation.

[Linker error] undefined reference to `listsize(listrec)'

Please advise.
Last edited by WaltP; Mar 31st, 2007 at 9:39 pm. Reason: Added CODE tags. Please use them!
Similar Threads
Reputation Points: 8
Solved Threads: 0
Newbie Poster
maverick786 is offline Offline
23 posts
since Mar 2007
Mar 31st, 2007
0

Re: linked list errors

Can someone help, please?
Reputation Points: 8
Solved Threads: 0
Newbie Poster
maverick786 is offline Offline
23 posts
since Mar 2007
Mar 31st, 2007
0

Re: linked list errors

You forgot the pointer in the function prototype:
int listsize(listrec*);
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 31st, 2007
0

Re: linked list errors

Thanx joe. I made the change and a new error. I'm uncertain about the syntax. I need the count of nodes or size of list returned; what should the parameter be in that case?
  1. #include <iostream>
  2. using namespace std;
  3. struct listrec
  4. {
  5. int value;
  6. struct listrec *next;
  7. };
  8. listrec e1,e2,e3;
  9.  
  10. int listsize(listrec*);
  11.  
  12. int main()
  13. { struct listrec* temp;
  14.  
  15. e1.value = 4;
  16. e1.next = &e2;
  17. e2.value = 5;
  18. e2.next = &e3;
  19. e3.value = 3;
  20. e3.next = NULL;
  21.  
  22. int sum = listsize(*temp);
  23.  
  24. cout << "The list size is " << sum;
  25.  
  26. system("pause");
  27. return 0;
  28. }
  29. int listsize(listrec *p)
  30. {
  31. struct listrec* temp = p;
  32. int num=0;
  33. do{
  34. temp = temp -> next;
  35. num++;
  36.  
  37. }while(temp!= NULL);
  38. return num;
  39. )


25 D:\ cannot convert `listrec' to `listrec*' for argument `1' to `int listsize(listrec*)'
Last edited by WaltP; Mar 31st, 2007 at 9:41 pm. Reason: Added CODE tags again.
Reputation Points: 8
Solved Threads: 0
Newbie Poster
maverick786 is offline Offline
23 posts
since Mar 2007
Mar 31st, 2007
0

Re: linked list errors

>int sum = listsize(*temp);
Try removing the *. And what in the world is "temp" doing in your program? You haven't even assigned it to anything when you pass it as an argument to listsize() !
Last edited by John A; Mar 31st, 2007 at 8:44 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 31st, 2007
0

Re: linked list errors

I think I have been going at this all wrong. Let me state the rquirements:
Write a function called listsize that takes a pointer to start of linked list and returns number of elements.
Write a main to create linked list of 4,5,3 as value1,value2,value3 and the call the function to calculate the size and print it.
Here is what I have:
  1. #include <iostream>
  2. using namespace std;
  3. struct listrec
  4. {
  5. int value;
  6. struct listrec *next;
  7. };
  8.  
  9.  
  10. int listsize(int);
  11. struct listrec *p;
  12. int main()
  13. {
  14. struct listrec e1,e2,e3;
  15. e1.value = 4;
  16. e1.next = &e2;
  17. e2.value = 5;
  18. e2.next = &e3;
  19. e3.value = 3;
  20. e3.next = NULL;
  21. int x =0;
  22.  
  23. int sum = listsize(&x);
  24.  
  25. cout << "The list size is " << sum;
  26.  
  27. system("pause");
  28. return 0;
  29. }
  30. int listsize(int &x)
  31. {
  32.  
  33. int num=0;
  34.  
  35. while(*p!= NULL);
  36. {
  37. *p = *p -> next;
  38. num++;
  39.  
  40. }
  41. return num;
  42. }
new error:
26 D: invalid conversion from `int*' to `int'
D: In function `int listsize(int&)':
39 no match for 'operator!=' in '*p != 0'
Last edited by WaltP; Mar 31st, 2007 at 9:41 pm. Reason: Added CODE tags, again
Reputation Points: 8
Solved Threads: 0
Newbie Poster
maverick786 is offline Offline
23 posts
since Mar 2007
Mar 31st, 2007
0

Re: linked list errors

*groan*

Your code was almost there, but now you wrecked it...

>Write a function called listsize that takes a pointer to start of linked list
OK, think about this. Where is the start of the linked list? Is it some random variable that you just declared, or do you think it's one of the pointers you assigned previously? Think about this carefully.

Your previous attempt was almost there except that you were passing the wrong thing to the function. That's it. This latest "revision" has made things far worse.
Last edited by John A; Mar 31st, 2007 at 9:14 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 1st, 2007
0

Re: linked list errors

So you need to create a linked list with three nodes and then call a function that follows the links and counts the number of nodes...is that correct?
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Apr 1st, 2007
0

Re: linked list errors

Click to Expand / Collapse  Quote originally posted by FC Jamison ...
So you need to create a linked list with three nodes and then call a function that follows the links and counts the number of nodes...is that correct?
Yes, please..This is what I've come up with now

#include <iostream>
#include <cstddef>
using namespace std;
struct listrec
{
int value;
struct listrec *next;
};


int listsize(listrec*);

int main()
{
struct listrec e1,e2,e3;
e1.value = 4;
e1.next = &e2;
e2.value = 5;
e2.next = &e3;
e3.value = 3;
e3.next = NULL;


int size = listsize(&e1);

cout << "The list size is " << size;

system("pause");
return 0;
}
int listsize(listrec * head)
{

int num=0;
listrec *p = head;
while(*p!=NULL) // producing error
{
*p = *p -> next;
num++;
};

return num;
}

but I'm down to this error:
40 D: no match for 'operator!=' in '*p != 0'
Last edited by maverick786; Apr 1st, 2007 at 1:20 pm.
Reputation Points: 8
Solved Threads: 0
Newbie Poster
maverick786 is offline Offline
23 posts
since Mar 2007
Apr 1st, 2007
0

Re: linked list errors

I have managed to free all compilation errors but when executing the program just terminates with no output except and error
lab8.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
Reputation Points: 8
Solved Threads: 0
Newbie Poster
maverick786 is offline Offline
23 posts
since Mar 2007

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: please help the new kid
Next Thread in C++ Forum Timeline: C++ .NET random number not getting any output





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


Follow us on Twitter


© 2011 DaniWeb® LLC