Singly Linked List

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

void insert_at_beg()

{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
if(new_node == NULL)
printf("nFailed to Allocate Memory");
printf("nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
new_node->next=start;
start=new_node;
}
}

void insert_mid()
{
int pos,i;
struct node *new_node,*current,*temp,*temp1;
new_node=(struct node *)malloc(sizeof(struct node));
printf("nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;
st :
printf("nEnter the position : ");
scanf("%d",&pos);
if(pos>=(length()+1))
{
printf("nError : pos > length ");
goto st;
}

if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
temp = start;
for(i=1;i< pos-1;i++)
{
temp = temp->next;
}
temp1=temp->next;
temp->next = new_node;
new_node->next=temp1;
}
}

You might also like