top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Doubly linked list - How to insert and remove element from specified position using c?

+1 vote
446 views
Doubly linked list - How to insert and remove element from specified position using c?
posted Nov 7, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

for insert a node

#include < stdio.h>
#include < conio.h>
#include < malloc.h>
#include< process.h>
#include < ctype.h>

struct doubly_list
{
    int info;
    struct doubly_list *prev;
    struct doubly_list *next;
}*first,*last,*newnode,*ptr;

void main()
{
    int item,i,loc;
    char ch;
    clrscr();
    newnode=(struct doubly_list*)malloc(sizeof(struct doubly_list));
    first=newnode;
    last=newnode;
    newnode->prev=NULL;
    do
    {
        printf("\nEnter data: ");
        scanf("%d",&item);
        newnode->info=item;
        printf("\nDo you want to create another node:(y/n)");
        fflush(stdin);
        scanf("%c",&ch);
        if(tolower(ch)=='y')
        {
            newnode->next=(struct doubly_list*)malloc(sizeof(struct doubly_list));
            newnode->next->prev=newnode;
            newnode=newnode->next;
            last=newnode;
        }
        else
        {
            newnode->next=NULL;
        }
    }while(tolower(ch)!='n');
    printf(“\nDoubly Linked List is:”);
    ptr=first;
    i=1;
    while(ptr!=NULL)
    {
        printf("\nNode %d : %d",i,ptr->info);
        ptr=ptr->next;
        i++;
    }
printf("\nEnter the item tobe inserted: ");
    scanf("%d",&item);
    printf("\nEnter the location: ");
    scanf("%d",&loc);
    newnode=(struct doubly_list *)malloc(sizeof(struct doubly_list));
    newnode->info=item;
    ptr=first;
    i=1;
    while(i    {
        ptr=ptr->next;
        i++;
    }
    newnode->next=ptr->next;
    newnode->prev=ptr;
    ptr->next->prev=newnode;
    ptr->next=newnode;

    printf("\nAfter insertion the linked list is:\n");
    ptr=first;
    i=1;
    while(ptr!=NULL)
    {
        printf("\nNode %d : %d",i,ptr->info);
        ptr=ptr->next;
        i++;
    }    
    getch();
}
answer Jan 5, 2017 by Ajay Kumar
...