Please help i don't know where is the error.I'm getting segmentation fault.I am using code-blocks as IDE.I am new to programming and this is my first attempt to create linked list. I guess there is problem in my push function but i am not able to find it.
#include<stdio.h>
#include<stdlib.h>
typedef struct list
{
int val;
struct list* next;
} node;
int main()
{
node* top;
top = NULL;
int i;
int n,m;
while(1)
{
fflush(stdin);
printf("Please enter i\n");
scanf("%d", i);
switch(i)
{
case 1:
{
printf("\nenter val");
scanf("%d", &n);
top=push(n, top);
}
case 2:
{
m = pop(top);
printf("Deleted value is %d", m);
}
case 3:
{
return 0;
}
}
}
}
node* push(int a,node* s)
{
if(s==NULL)
{
s = malloc(sizeof(node));
s->val = a;
s->next = NULL;
return s;
}
else
{
node* temp;
temp = malloc(sizeof(node));
temp->val = a;
temp->next = s;
s = temp;
return s;
}
}
node* pop(node* s)
{
int x;
node* temp;
x = s->val;
printf("deleted value is %d", x);
temp = s->next;
s->next = NULL;
free(s);
s = temp;
return s;
}