//How to create linked list(self referential structure) and perform insert operaton on it.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
main()
{
struct node *p;
char c;
int n;
void create(struct node **, int);
void show(struct node *);
void insert(struct node **);
p=NULL;
printf("Enter the elements you want in your linked list :");
scanf("%d",&n);
create(&p,n);
show(p);
fflush(stdin);
printf("\nWould you like to insert element in the begining of linked list Y/N:");
scanf(" %c",&c);
if(c=='y'||c=='Y')
{
insert(&p);
show(p);
}
}
void create(struct node **q,int n)
{
int i;
struct node *lst;
struct node *p;
lst=NULL;
for(i=1;i<=n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
printf("Enter the %d element :",i);
scanf("%d",&(p->data));
p->next=NULL;
if(*q==NULL)
*q=p;
else
lst->next=p;
lst=p;
}
}
void show(struct node *start)
{
printf("Entered linked list is :\n");
while(start!=NULL)
{
printf(" %d\n",start->data);
start=start->next;
}
}
void insert(struct node **q)
{
struct node *p;
struct node *start;
start=*q;
p=(struct node *)malloc(sizeof(struct node));
printf("Enter the element to be inserted ");
scanf("%d",&(p->data));
p->next=start;
*q=p;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
main()
{
struct node *p;
char c;
int n;
void create(struct node **, int);
void show(struct node *);
void insert(struct node **);
p=NULL;
printf("Enter the elements you want in your linked list :");
scanf("%d",&n);
create(&p,n);
show(p);
fflush(stdin);
printf("\nWould you like to insert element in the begining of linked list Y/N:");
scanf(" %c",&c);
if(c=='y'||c=='Y')
{
insert(&p);
show(p);
}
}
void create(struct node **q,int n)
{
int i;
struct node *lst;
struct node *p;
lst=NULL;
for(i=1;i<=n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
printf("Enter the %d element :",i);
scanf("%d",&(p->data));
p->next=NULL;
if(*q==NULL)
*q=p;
else
lst->next=p;
lst=p;
}
}
void show(struct node *start)
{
printf("Entered linked list is :\n");
while(start!=NULL)
{
printf(" %d\n",start->data);
start=start->next;
}
}
void insert(struct node **q)
{
struct node *p;
struct node *start;
start=*q;
p=(struct node *)malloc(sizeof(struct node));
printf("Enter the element to be inserted ");
scanf("%d",&(p->data));
p->next=start;
*q=p;
}