linked list

Z

zfareed

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,int&);
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,sum);

system("pause");
return 0;
}

int listsize(listrec *p,int &num)
{
struct listrec* temp = p;
int num=0;
do{
temp = temp -> next;
num++;

}while(temp!= NULL);
return num;
}

error: `pntr' undeclared (first use this function)
That is my error and I always have problems calling functions.
 
A

Alf P. Steinbach

* (e-mail address removed):
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,int&);
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,sum);

system("pause");
return 0;
}

int listsize(listrec *p,int &num)
{
struct listrec* temp = p;
int num=0;
do{
temp = temp -> next;
num++;

}while(temp!= NULL);
return num;
}

error: `pntr' undeclared (first use this function)
That is my error and I always have problems calling functions.

The code you have posted is not the code you have compiled.

Try again.
 
Z

zfareed

Sorry about that. I kept changing stuff. Here it is:

#include <iostream>
using namespace std;

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

int listsize(listrec *,int&);
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,sum);

system("pause");
return 0;
}

int listsize(listrec *p,int &num)
{
struct listrec* temp = p;

do{
temp = temp -> next;
num++;

}while(temp!= NULL);
return num;
}


D:\ In function `int main()':
31 D:\ cannot convert `listrec' to `listrec*' for argument `1' to `int
listsize(listrec*, int&)'
 
K

Keith Davies

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

int listsize(listrec *,int&);
struct listrec* temp;

cout << "The list size is " << listsize(*temp,sum);

[much elided because it has no bearing]

Of what type is temp?
Of what type is *temp?
What type does listsize() want?


Keith
 
Z

zfareed

Pardon me. I am trying to return the int number of items in the list
from the listsize function. I have tried different variables in there
and still getting compilation errors.
int listsize(listrec *p,int &num)
{
struct listrec* temp = p;
do{
temp = temp -> next;
num++;
}while(temp!= NULL);
return num; >>>>>>return this value back to the main and print
}
 
Z

zfareed

Pardon me. I am trying to return the int number of items in the list
from the listsize function. I have tried different variables in there
and still getting compilation errors.

This is what I've come up with now.
#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;
}


This is my error:
[Linker error] undefined reference to `listsize(listrec)'
 
G

Gianni Mariani

This is what I've come up with now.
#include <iostream>

using namespace std;

struct listrec
{
int value;
struct listrec *next;
};

should not allow copy of this struct (IMHO).
listrec e1,e2,e3;

int listsize(listrec);

listsize requires making a copy of listrec ... not really good.

// at a minimum it should be this:
int listsize(const listrec &);

struct listrec* temp;

temp is never assigned - not good

This is C++ - loose the "struct".
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);

here you call "int listsize(listrec)" but you never define it.
system("pause");
return 0;
}

This function below is not the same as "int listsize(listrec)".
int listsize(listrec *p)
{
struct listrec* temp = p;

.... again - loose the "struct" - it's unnecessary

int num=0;
do{
temp = temp -> next;
num++;
}while(temp!= NULL);
return num;
}


This is my error:
[Linker error] undefined reference to `listsize(listrec)'

You seem to be confused with pointer syntax.


struct T {}; // define a struct T

T obj; // obj is an object of type T

T * p; // p is a pointer to a T


int main()
{
p = & obj; // p is assigned the pointer (address) of obj

const T & r = * p; // r is "seated" with a reference to obj

T * p1 = & r; // p1 now has the same address as p
}


const T * p2( & obj ); // initialize p2 with address of obj
 
D

Duane Hebert

Pardon me. I am trying to return the int number of items in the list
from the listsize function. I have tried different variables in there
and still getting compilation errors.

This is what I've come up with now.
#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;
}


This is my error:
[Linker error] undefined reference to `listsize(listrec)'

The error report is telling you what the problem is.
You declare a function above that takes a listrec
and you try to define it here with a listrec*.
Once you fix this you can deal with the other places
where you have ** instead of *.

BTW, you only need the keyword struct when you
declare the struct, not each time you make a pointer
to it...
 
Z

zfareed

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'
 
K

Keith Davies

Pardon me. I am trying to return the int number of items in the list
from the listsize function. I have tried different variables in there
and still getting compilation errors.

There's your problem. You appear to be *trying* rather than *thinking*.

The compiler told you *exactly* what was wrong with the code you posted.
Fix that, and I think it'll work[1]. There are style issues (it seems
evident that you've been exposed to C before now and it's coloring your
code) but what you have *will* work once you fix the syntax error.

[1] there is a case where listsize() will fail painfully. It does not
occur in your program, but you should fix it anyway.


Keith
 
J

Jim Langston

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);

Quote: Write a function called listsize that takes a pointer to start of
linked list and returns number of elements.

Your listsize is not taking a pointer to the start of a linked list. It's
taking an int, which is basically meaningless. Try instead:

int listsize( listrec* );

Lets see, list size (fuction called listsize) listrec* (takes a pointer to
start of link list) returning int (returns number of elements).

Doesn't that sound more like the requirements?
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)

Go up to the top and read my explantation of why this function should be:
int listsize( listrec* x)
{

int num=0;

while(*p!= NULL);

And where is p defined? Can't use a variable you didn't define. You
problem meant before this:
listrec* p = x;
{
*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'

1. Read the requirements and make your functions/classes/methods match them.
2. Declare your variables and initialize them.
3. Compare apples to apples, not apples to oranges.

Your algorithm seems right. It's just in the implimentation you seem to be
getting confused. C++ syntax is something you need to work on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top