Regarding linked lists

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

Mike Wahler

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

Here you dictate that function 'listsize()'s
expects a type 'int' argument.
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);

... but here, you pass it type 'int *'.

Why?
cout << "The list size is " << sum;

system("pause");
return 0;
}
int listsize(int &x)
{

int num=0;

while(*p!= NULL);

The expression '*p' has type 'listrec'.
'NULL' is for pointers.
BTW what is pointer 'p' pointing at?

{
*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'

See above.

-Mike
 
M

Michael

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

Make your life easier. Don't do this. Instead, move the definition
of listsize up here (i.e., before main).
struct listrec *p;

Get rid of this global variable. (See below.)
int main()
{
struct listrec e1,e2,e3;

You don't need the word 'struct' here. 'listrec' is now a real type
of its own. (Note, you did need it above, because you were using it
in its own definition.)
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);

Get rid of x. You're not really using it.

And this isn't 'sum.' It's 'size' or some such name.

The line should be something like:
int size = listsize(&e1);
cout << "The list size is " << sum;

system("pause");
return 0;}

int listsize(int &x)

Per your requirements:
Write a function called listsize that takes a ***pointer to start
of
linked list*** and returns number of elements.

This should probably be
int listsize(listrec* head)
{

int num=0;

Move your 'p' down here, with the appropriate initialization. (I.e.,
'p' is local to the scope of this function.) And drop the 'struct.'
while(*p!= NULL);
{
*p = *p -> next;
num++;

This is close but not quite. I'll let you figure it out once you've
got the rest.
 
Z

zfareed

Thank you so much for your help. I have cleared all compilation errors
but on execution the program just terminates with this error
lab8.exe has encountered a problem and needs to close. We are sorry
for the inconvenience.

-
 
O

osmium

Thank you so much for your help. I have cleared all compilation errors
but on execution the program just terminates with this error
lab8.exe has encountered a problem and needs to close. We are sorry
for the inconvenience.

You might think about posting the code that is giving you problems.
 

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

Similar Threads

linked list 10
Infinite loop problem 1
C coding a rotate function (help me pleasee) 1
Lexical Analysis on C++ 1
remove index 14
linked list 26
Stack using doubly linked list 1
Function is not worked in C 2

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top