Sunday, 23 September 2012

CREATION OF LINK LIST AND INSERTION IN THE LINK LIST AT ANY NODE


CREATION OF LINK LIST AND INSERTION IN THE LINK LIST AT ANY NODE


#include
#include
int main()
{
    struct node{
        int data;
        struct node *next;
    }*head;
    struct node *temp,*temp1;
    head=NULL;
    int num,i,item;
    scanf("%d",&num);
    for(i=0;i    {
        scanf("%d",&item);
        if(head==NULL)
        {
            head=malloc(sizeof(struct node));
            head->data=item;
            temp=head;
        }
        else
        {
            temp->next=malloc(sizeof(struct node));
            temp->next->data=item;
            temp=temp->next;
        }
    }

    temp->next=NULL;
    temp=head;
    int num1,p;
    printf("ener a number=");
    scanf("%d",&num1);
    printf("position\n");
    scanf("%d",&p);
    struct node *head1,*temp2;
    head1=malloc(sizeof(struct node));
    head1->data=num1;
    p=p-1;
    for(i=0;i    {
        if(i==p)
        {
            //store previous link in another pointer//
            temp2->next=temp->next;
            temp->next=head1;
            head1->next=temp2->next;
        }
        else
        {
            temp=temp->next;
        }
    }
    temp=head;
    while(temp!=NULL)
    {
        printf("%d\n",temp->data);
        temp=temp->next;
    }
    return 0;
}


No comments: