I am simply trying to create a linked list of characters. Here's the code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
char data;
struct node *next;
};
void push(struct node** head,char ndata)
{
struct node* temp=(struct node*)malloc(sizeof(node));
temp->data=ndata;
temp->next=(*head);
*head=temp;
}
void display(struct node* head)
{
struct node* temp=head;
while(temp)
{
printf("%c ",temp->data);
temp=temp->next;
}
}
int main()
{
struct node* head;
int a=1;
push(&head,'a');
push(&head,'b');
push(&head,'c');
push(&head,'b');
push(&head,'a');
display(head);
getch();
return 0;
}
I am using push() function which inserts the values at head. And then using display() method to display the values in the list. When I execute the program, it says "program10.exe has stopped working". I don't understand what the problem is. Can anyone help?
sizeof(node)? without atypedef? how??