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.

#include <iostream> 


using namespace std; 

struct listrec 
    { 
       int value; 
       struct listrec *next; 
    }; 
    listrec e1,e2,e3; 

int listsize(listrec); 
struct listrec* temp; 

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

    int sum = 0; 
    cout << "The list size is " << listsize(*temp); 

    system("pause"); 
    return 0; 

} 


int listsize(listrec *p) 
{ 
    struct listrec* temp  = p; 
    int num=0; 
    do{ 
            temp = temp -> next; 
            num++; 
      }while(temp!= NULL); 
    return num; 


}

I get the following error on compilation.

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

Please advise.

Can someone help, please?

You forgot the pointer in the function prototype:

int listsize(listrec*);

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?

#include <iostream>
using namespace std;
struct listrec
    {
       int value;
       struct listrec *next;
    };
    listrec e1,e2,e3;
    
int listsize(listrec*);

int main()
{   struct listrec* temp;  
 
    e1.value = 4;
    e1.next  = &e2;
    e2.value = 5;
    e2.next  = &e3;
    e3.value = 3;
    e3.next  = NULL;
         
    int sum = listsize(*temp);
         
    cout << "The list size is " << sum;
    
    system("pause");
    return 0;
}
int listsize(listrec *p)
{
    struct listrec* temp = p;
    int num=0;
    do{
            temp = temp -> next;
            num++;
        
      }while(temp!= NULL);
    return num;
)

25 D:\ cannot convert `listrec' to `listrec*' for argument `1' to `int listsize(listrec*)'

>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() !

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:

#include <iostream>
using namespace std;
struct listrec
    {
       int value;
       struct listrec *next;
    };
    
    
int listsize(int);
struct listrec *p;
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 x =0; 
         
    int sum = listsize(&x);
         
    cout << "The list size is " << sum;
    
    system("pause");
    return 0;
}
int listsize(int &x)
{
    
    int num=0;
    
    while(*p!= NULL);
    {
           *p = *p -> next;
            num++;
        
      }
    return num;
}

new error:
26 D: invalid conversion from `int*' to `int'
D: In function `int listsize(int&)':
39 no match for 'operator!=' in '*p != 0'

*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.

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?

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'

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.

It's time you start using code tags. This is getting really annoying and hard to read your code.

A few problems:

int listsize(listrec * head)
{
    
    int num=0;
    listrec *p = head;
    while(*p!=NULL)    // remove the *
    { 
        *p = *p -> next; // remove the *s.
        num++;
    };
      
    return num;
}

You see, by using a *, you dereference the pointer, and in fact in the while loop you're checking that the actual memory inside the struct is not null, which is wrong. You want to check for a null pointer, so then don't use a *. Same goes for the other line.

Thank you and sorry about the untidy postings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.