When printing out my linked list, what is displayed is not what I assumed it to be. How do I get the right output?
struct node{
int data;
struct node *next;
};
struct node *newNode(int data){
struct node *new_node=(struct node *) malloc(sizeof(struct node));
new_node->data=data;
new_node->next=NULL;
return new_node;
}
void push(struct node*** head, int data){
struct node* new_node=newNode(data);
new_node->next=(**head);
(**head)=new_node;
}
void create(struct node **number, char num[]){
int x=0;
while(x<strlen(num)){
int d=(int)(num[x]);
push(&number, d);
x++;
}
}
void printList(struct node *number){
while(number!=NULL){
printf("%d", number->data);
number=number->next;
}
printf("\n");
}
int main (void){
struct node *first;
char num1[10];
scanf("%s", num1);
create(&first, num1);
printList(first);
return 0;
}
Examples
Input : 1
Expected Output: 1
Actual Output : 49
Input : 12345
Expected Output: 12345
Actual Output : 5352515049
I think it is printing where the value is stored, not the value itself. Correct me on that if that's wrong. Anyways how do I get the expected output I want.