473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Regarding linked lists

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'

Apr 1 '07 #1
4 1702

<zf*****@mail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.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);
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
Apr 1 '07 #2
On Mar 31, 5:00 pm, zfar...@mail.com wrote:
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.
>
}
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'

Apr 1 '07 #3
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.

-
Apr 1 '07 #4
<zf*****@mail.comwrote:
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.
Apr 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
4
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL...
3
by: Rohini Ramamurthy | last post by:
Hi All, I have taken up this problem to learn a bit in using linked lists. I am trying a variant of it what it is supposed to so is delete the nth person clockwise and counter clockwise...
6
by: skishorev | last post by:
I am implementing a binary search tree. But, Here some of the nodes i need to maintain a backup of . This backup i am maintaining a Linked Lists. Example: By using 1 to 100 numkbers i need...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.