|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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[i]=new node; ARR[i]->data=i; } for(int i=1;i<=NU-1;i++) { ARR[i]->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??? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Jul 16, 6:37 pm, aki <akhileshrawat...@gmail.com> wrote:
> 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[i]=new node; > ARR[i]->data=i; > } > for(int i=1;i<=NU-1;i++) > { > ARR[i]->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 ....??? |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
aki <akhileshrawat007@gmail.com> writes:
> 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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Wed, 16 Jul 2008 06:37:00 -0700, aki wrote:
> 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[i]=new node; > ARR[i]->data=i; > } > for(int i=1;i<=NU-1;i++) > { > ARR[i]->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. -- Lionel B |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Jul 16, 9:37am, aki <akhileshrawat...@gmail.com> wrote:
> #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[i]=new node; > ARR[i]->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[i]->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[i]=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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 2008-07-16 11:22:29 -0400, Puppet_Sock <puppet_sock@hotmail.com> said:
> On Jul 16, 9:37Âam, aki <akhileshrawat...@gmail.com> wrote: >>     int NU; >>     cout<<"enter nu"<<endl; >>     cin>>NU; >>     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. The standard requires only that the compiler issue a diagnostic. The standard never requires that code not compile. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book) |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
aki wrote:
> ... > node* ARR[NU]; // Array of pointers to type node Is this Ok > for(int i=1;i<=NU;i++) > { > ARR[i]=new node; > ARR[i]->data=i; > } > for(int i=1;i<=NU-1;i++) > { > ARR[i]->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[i] = 2 * (i+1); // do the data if(i < nu-1) next_link[i] = 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. |
|
![]() |
| Outils de la discussion | |
|
|