Error in this piece of code

A

aki

Hi Frnds ,

Can u reply to my query ..
thanks
Aki

#include<iostream>
using namespace std;
const int NU=10;
struct node {
int data;
node *ptr;
};
int main()
{
int NU;
cout<<"enter nu"<<endl;
cin>>NU;

node* ARR[NU]; // Array of pointers to type node Is this Ok
for(int i=1;i<=NU;i++)
{
ARR=new node;
ARR->data=i;
}
for(int i=1;i<=NU-1;i++)
{
ARR->ptr =ARR[i+1];

}
node *head=ARR[1];
ARR[NU]->ptr=NULL;
node *tmp =head;
while(tmp->ptr!=NULL)
{
static int c=1;
cout<<"node"<<c<<"="<<tmp->data<<endl;
c++;
tmp=tmp->ptr;
}
return 0;
}


output
enter nu
7
node1=1
node2=2
node3=3
node4=4
node5=5
node6=6

Why is 7th node not getting printed???
 
A

aki

Hi Frnds ,

Can u reply to my query ..
thanks
Aki

#include<iostream>
using namespace std;
const int NU=10;
struct node {
int data;
node *ptr;
};
int main()
{
int NU;
cout<<"enter nu"<<endl;
cin>>NU;

node* ARR[NU]; // Array of pointers to type node Is this Ok
for(int i=1;i<=NU;i++)
{
ARR=new node;
ARR->data=i;
}
for(int i=1;i<=NU-1;i++)
{
ARR->ptr =ARR[i+1];

}
node *head=ARR[1];
ARR[NU]->ptr=NULL;
node *tmp =head;
while(tmp->ptr!=NULL)
{
static int c=1;
cout<<"node"<<c<<"="<<tmp->data<<endl;
c++;
tmp=tmp->ptr;
}
return 0;

}

output
enter nu
7
node1=1
node2=2
node3=3
node4=4
node5=5
node6=6

Why is 7th node not getting printed???


i got sth ...Correction is needed in while condition while(tmp!
=NULL) ...

else is everthing fine in code ....???
 
T

Tim Love

node* ARR[NU]; // Array of pointers to type node Is this Ok
Not in ANSI C++ because NU isn't a const. g++ by default might compile it.
Why is 7th node not getting printed???
Because the node with data==7 also has ptr==NULL, so the while condition
stops the while body being run.
 
L

Lionel B

Hi Frnds ,

Can u reply to my query ..
thanks
Aki

#include<iostream>
using namespace std;
const int NU=10;

Note that this NU will not be used in `main()', since you define there
another `int U'
struct node {
int data;
node *ptr;
};
int main()
{
int NU;
cout<<"enter nu"<<endl;
cin>>NU;

node* ARR[NU]; // Array of pointers to type node Is this
Ok

No, not in standard C++, since an array size must be a constant known at
compile time. So your "global" NU defined before `main()' would be ok
here.
for(int i=1;i<=NU;i++)

Array indices will run from 0 to NU-1, not 1 to NU. So in your code below
you reference (repeatedly) the unallocated location ARR[NU]
{
ARR=new node;
ARR->data=i;
}
for(int i=1;i<=NU-1;i++)
{
ARR->ptr =ARR[i+1];

}
node *head=ARR[1];
ARR[NU]->ptr=NULL;
node *tmp =head;
while(tmp->ptr!=NULL)
{
static int c=1;
cout<<"node"<<c<<"="<<tmp->data<<endl; c++;
tmp=tmp->ptr;
}
return 0;
}


output
enter nu
7
node1=1
node2=2
node3=3
node4=4
node5=5
node6=6

Why is 7th node not getting printed???


Because you are indexing your array incorrectly.
 
P

Puppet_Sock

#include<iostream>
using namespace std;

Don't do that. Use the std:: decorator on things from std.
const int  NU=10;
struct node {
                int data;
                node *ptr;
            };
int main()
{
        int NU;
        cout<<"enter nu"<<endl;
        cin>>NU;

Note that this NU hides the NU you declared earlier.

        node* ARR[NU]; // Array of pointers to type node        Is this Ok

Note that this should not compile on standard
compliant compiler as NU is not a compile time
constant. If you need values that are chosen
at run time, you need ot be looking at new[]
and delete[].

Your compiler may be allowing the extension.
If so, you should be looking for flags on your
compiler that make it picky about not allowing
extensions. Always make yor compiler as picky
as you can.
        for(int i=1;i<=NU;i++)
        {
           ARR=new node;
           ARR->data=i;
        }


This loop needs to go from 0 to NU-1. Read the
note following.
        for(int i=1;i<=NU-1;i++)
        {
           ARR->ptr =ARR[i+1];

        }


Needs to go from 0 to NU-2.

It looks like you are setting up a linked list.
But the elements of that linked list are also
in an array. This looks very strange.

If you want a linked list, you need to be doing
a linked list. (Though you are much better off
learning the standard containers first.) If you
want an array, you need to be doing an array.
Overlapping them like this is whacky.
        node *head=ARR[1];

Needs to be ARR[0]
        ARR[NU]->ptr=NULL;

Needs to be ARR[NU-1]

You need to more carefully read your introductory
text on C++. Arrays go from 0 to dimension minus 1.
So, even if your compiler allowed the extension
previously mentioned, this is wrong. If you enter
7 for NU, the elements of the array would be ARR[0]
through AR[6]. Referencing ARR[7] is a problem.

It looks like (though I can't be sure) you accidentally
"got away with it" here.

But changes to your code might produce weird behaviour.

When you do this line

ARR=new node;

with i == 7, it will be writing to memory outside the
array. You don't want to be doing that.
        node *tmp =head;
        while(tmp->ptr!=NULL)

This is saying, as long as the current node has a next
node, keep going. But when you get to the 7th node,
it does not have a next node.

Should be this.

while(tmp != NULL)

Which means, as long as there is a current node, keep going.
Note that this works even if the linked list is empty.
        {
                static int c=1;
                cout<<"node"<<c<<"="<<tmp->data<<endl;
                c++;

What is the variable c for? It does not seem to get used.
                tmp=tmp->ptr;
        }
  return 0;

}

output
enter nu
7
node1=1
node2=2
node3=3
node4=4
node5=5
node6=6

Why is 7th node not getting printed???

Hope you get it now.
Socks
 
M

Mirco Wahab

aki said:
...
node* ARR[NU]; // Array of pointers to type node Is this Ok
for(int i=1;i<=NU;i++)
{
ARR=new node;
ARR->data=i;
}
for(int i=1;i<=NU-1;i++)
{
ARR->ptr =ARR[i+1];

}
node *head=ARR[1];
ARR[NU]->ptr=NULL;
node *tmp =head;
while(tmp->ptr!=NULL)
{
static int c=1;
cout<<"node"<<c<<"="<<tmp->data<<endl;
c++;
tmp=tmp->ptr;
...
Why is 7th node not getting printed???


There have been all sorts of corrections and critiques
already in this thread. This should have already answered
your questions. You obviously are trying to learn to
understand linked lists and worked trough it - which is fine.

BTW: your problem shows a "predetermined situation" - where
your number of links and your number of data items is
defined/known before the list is touched.

And: this is (therefoe, imho) *not* a good example for such
a data structure with "records involving pointers". If you
know how many data items you have and you have a simple
"linear chain" through them (in any order), you only need
a single vector to hold that (one vector for data of any
type and one vector of int "indexes") for the linked list.
You simply store in one index vector element (as a value)
the index of the "linked (next) element". This is quite
simple and much more instructive (imho):


...
void foo()
{
int nu;
cout << "enter nu" << endl;
cin >> nu;

vector<int> data_arr(nu); // data vector of size "nu"
vector<int> next_link(nu, -1); // link data, set to END_OF_CHAIN/STOP

for(int i=0; i<nu; i++) { // generate links and some data:
data_arr = 2 * (i+1); // do the data
if(i < nu-1) next_link = i+1; // (!!) keep last node at -1
}

int tmp=0, c=0; // now thats similar to yours:
while(tmp != -1) { // -1 is END_OF_CHAIN
cout << "node" << ++c << "=" << data_arr[tmp] << endl;
tmp = next_link[tmp];
}
}
....

To play with "Arrays of pointers to structures" here would
be (imho) much over the top (BYMMV).

(The above would require an additional include of <vector>)

Regards

M.
 

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top