Below is my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
struct node {
int data;
struct node *next;
};
int choice;
struct node *head, *newnode, *temp;
head = 0;
while (choice) {
newnode = (struct node *)malloc(sizeof(struct node));
printf("enter items ");
scanf("%d", &newnode->data);
newnode->next = 0;
if (head == 0) {
head = temp = newnode;
} else
temp->next = newnode; /** **this is the problem** **/
temp = newnode; /** temp=newnode works but temp=temp->next doesn't**/
printf("do you want to continue");
scanf("%d", &choice);
}
temp = head;
while (temp != 0) {
printf("list is %d \n", temp->data);
temp = temp->next;
}
}