I am unable to insert data in a linked lists.
Show function is not working.
insert
Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}
Node;
Node *sptr;
Node *head=NULL;
void show();
void ins(int key);
int main()
{
int i,item;
for(i=0;i<=5;i++)
{
printf("Enter number\n"); // Inserting 6 elements in list
scanf("%d",&item);
ins(item);
}
show(); //To show the data in linked list
return 0;
}
void ins(int key)
{
Node *ptr=NULL;
ptr=(Node*)malloc(sizeof(Node));
ptr->data=key;
if(head==NULL)
{
head=ptr;
ptr->next=NULL;
}
else
{
ptr->next=head;
head=ptr;
}
}
void show()
{
while(sptr->next!=NULL)
{
printf("%d\n",sptr->data);
}
}