linked list



 DEVELOP > c-Plus-Plus > linked list

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 31 Mar 2007 03:35:59 PM
Object: linked list
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.
.

User: "Alf P. Steinbach"

Title: Re: linked list 31 Mar 2007 03:41:27 PM
* zfareed@umd.umich.edu:

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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: ""

Title: Re: linked list 31 Mar 2007 03:47:09 PM
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&)'
.
User: "Keith Davies"

Title: Re: linked list 31 Mar 2007 04:03:32 PM
<
> wrote:


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
--
Keith Davies "Sometimes my brain is a very strange
keith.davies@kjdavies.org to live in."
keith.davies@gmail.com -- Dana Smith
http://www.kjdavies.org/
.
User: ""

Title: Re: linked list 31 Mar 2007 04:07:41 PM
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
}
.
User: "Keith Davies"

Title: Re: linked list 31 Mar 2007 07:26:13 PM
<
> wrote:

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
--
Keith Davies "Sometimes my brain is a very strange
keith.davies@kjdavies.org to live in."
keith.davies@gmail.com -- Dana Smith
http://www.kjdavies.org/
.

User: ""

Title: Re: linked list 31 Mar 2007 04:11:40 PM
On Mar 31, 5:07 pm,
wrote:

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)'
.
User: "Duane Hebert"

Title: Re: linked list 31 Mar 2007 06:09:50 PM
<zfareed@mail.com> wrote in message
news:1175375500.370758.298490@n76g2000hsh.googlegroups.com...

On Mar 31, 5:07 pm,

wrote:

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...
.
User: ""

Title: Re: linked list 31 Mar 2007 07:00:10 PM
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'
.
User: "Jim Langston"

Title: Re: linked list 31 Mar 2007 08:41:43 PM
<zfareed@mail.com> wrote in message
news:1175385610.508164.23610@o5g2000hsb.googlegroups.com...

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



User: "Gianni Mariani"

Title: Re: linked list 31 Mar 2007 05:54:38 PM
wrote:

On Mar 31, 5:07 pm,

wrote:

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

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
.







  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER