DELETE ELEMENT AT THE Nth POSITION

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data ;
struct node* link;
};

struct node* head = NULL;

void insert(int data)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->link = head;
head = temp;
}
print()
{
struct node* temp = head ;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp = temp->link;
}
printf("\n");
}

void delete(int pos)
{
int i;
struct node* temp = head ;
if(pos == 1)
{
head = temp->link;
free(temp);
   return;
}
for(i=0;i<pos-2;i++)
{
temp = temp->link;
}
struct node* temp1 = temp->link;
temp->link = temp1->link;
free(temp1);
}
void main()
{
int n;
insert(9);
insert(10);
insert(11);
insert(12);
print();
printf("enter position\n");
scanf("%d",&n);
delete(n);
print();
}

Comments