typedef struct node_s{
int data;
struct node_s *next;
}node_t;
void insert(node_t *pointer, int data){
while(pointer->next != NULL){
pointer = pointer->next;
}
pointer->next = (node_t *)malloc(sizeof(node_t));
pointer = pointer->next;
pointer->data = data;
printf("Elemnet inserted
"); //2. Followed by this statment once done.
pointer->next = NULL;
}
int main(){
node_t *start, *temp;
start = (node_t *)malloc(sizeof(node_t));
temp = start;
temp->next = NULL;
printf("1. Insert
");
printf("2. Delete
");
printf("3. Print
");
printf("4. Find
");
while(1){
int input;
scanf("%d
", &input);
if(input==1){
int data;
printf("Input data
");//1. I want this to print out first once I give 1 input.
fflush(stdout);
scanf("%d", &data);
insert(start, data);
}
}
When I compile and execute, I can give inputs but the order of printf statements are not in sequence. For instance, this is how I get the output after I give input and enter the data.
sh-4.1$ ./linked_list
1. Insert
2. Delete
3. Print
4. Find
1
23
Input data
Elemnet inserted
1
45
Input data
Elemnet inserted
I tried adding fflush(stdout), after the printf statment as well.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…