why ‘0’ is not there in beginning ?
also if in for loop I start i with ‘-1’…then it’s perfectly fine !
why ‘0’ is not there in beginning ?
also if in for loop I start i with ‘-1’…then it’s perfectly fine !
please elaborate your doubt .your code link seems empty
here is the code …online ide is not working for me !
iostream is there…it automatically hidding it !
#include
using namespace std;
class node{
public:
node* next;
int data;
node(){
next=NULL;
data=NULL;
}
};
void insertInList(node* head,int d){
if(head->data==NULL){
head->next=NULL;
head->data=d;
return;
}
else{
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
// temp->next= new node();
// temp->next->data=d;
node* n = new node();
temp->next=n;
n->data=d;
}
}
void print(node* head){
node* temp=head;
while(temp->next!=NULL){
cout<data<<" -> ";
temp=temp->next;
}
cout<data;
}
int main(){
#ifndef ONLINE_JUDGE
freopen(“input.txt”,“r”,stdin);
freopen(“output.txt”,“w”,stdout);
#endif
node* head=new node();
for(int i=0;i<11;++i){
insertInList(head,i);
}
print(head);
return 0;
}
what beginning
which for loop
this?
please use some other online ide and you can mark your doubts as comments in the code
run the code the output is comin
1 -> 2 -> … -> 10 and NOT
0 -> 1 -> …-> 10
if you start for loop from -1 then the ouput is un-ambigous !
-1 -> 0 -> 1 -> 2 ->…-> 10
yes
I don’t know any other online ide that supports link sharing !
this data null is creating the issue.You don’t really need NULL for this. NULL is typically used for pointers, but it really is just a macro for 0. Using NULL here, replace it with zero and you ll why you are getting the ans .
see this:
Macros are a piece of code in a program which is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code.
NULL is a macro which is defined in C header files. The value of NULL macro is 0.
It is defined in C header files as below.
#define NULL (void *) 0;
NULL is used for pointers only as it is defined as (void *) 0. It should not be used other than pointers. If NULL is assigned to a pointer, then pointer is pointing to nothing.
0 (zero) is a value.