I was implementing linked list in c. For it i created a generation() function for making linked list when i call it inside main() the while loop don't work but when i put the generate code block in main it works fine.
#include<stdio.h>
#include<stdlib.h>
void generation(void);
void first(void);
void end(void);
void middle(void);
int n=0;
struct node{
int data;
struct node *next;
};
struct node *head,*newnode,*temp,*prevnode;
void main(){
generation();
first();
end();
middle();
}
void generation(void){
head=0;
int choice;
while(choice){
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data\n");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0)head=temp=newnode;
else{
temp->next=newnode;
temp=newnode;
}
printf("Enter for going further(0,1)\n");
scanf("%d",&choice);
n++;
}
temp=head;
printf("Your data is:\n");
while(temp!=0){
printf("Values are %d\n",temp->data);
temp=temp->next;
}
}
Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically
You're not initializing the choice variable before the loop, thus you don't know what value it contains
More details here What happens to a declared, uninitialized variable in C? Does it have a value?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.