0
4.3kviews
Write a C program to implement doubly linked list. Provide following operations a) Insert at beginning b) Insert at location c) Remove from beginning d) Remove from location

Mumbai University > COMPS > Sem 3 > Data Structures

Marks: 10 M

Year: May 2014, May 2015

1 Answer
0
44views
#include<stdio.h>
#include<conio.h>
struct node *insert begin(struct node *start)
{
    Struct node *new_node;
    int num;
    printf("\n enter the value");
    scanf("%d",&num);
    new_node=(struct node *)malloc(sizeof(struct node));
    new_node->data=num;
    start->prev=new_node;
    new_node->next=start;

new->prev=NULL;
    start=new_node;
    return start;
}
struct node *insert_end(struct node *start)
{
    struct node *new_node,*ptr;
    ptr=start;
    int num;
    printf("\n enter the value");
    scanf("%d",&num);
    new_node=(struct node *)malloc(sizeof(struct node));
    new_node->data=num;
    while(ptr->next!=NULL){
        ptr=ptr->next;  
    }
    ptr->next=new_node;
    start->prev=new_node;
    new_node->next=NULL;
    new->prev=ptr;
    return start;
}

struct node *delete_begin(struct node *start)
{
    struct node *ptr;
    ptr=start;
    start=start->next;
    start=start->prev;
    free(ptr);
    return start;
}


struct node *delete_end(struct node *start)
{
    struct node *ptr;
    ptr=start;
    while(ptr->next!=NULL)
{
        ptr=ptr->next;
    }
    ptr->prev->next=NULL;
    free(ptr);
    return start;
}
struct node *create_linked_list(struct node *start)
{
    struct node *new_node,*ptr;
    int num;
    printf("\n Enter the data:");
    scanf("%d",&num);
    while(num!=-1)
    {
        if(start==NULL)
        {
            new_node=(struct node*)malloc(sizeof(struct node));
            new_node->prev=NULL;
            new_node->data=num;
            new_node->next=NULL;
            start=new_node;
        }
        else
        {
            ptr=start;
            new_node=(struct node*)malloc(sizeof*struct node));
            new_node->data=num;
            while(ptr->next!=NULL)
        {
                ptr=ptr->next;
            }
            ptr->next=new_node;
            new_node->prev=ptr;
            new_node->next=NULL;
        }
        printf("\n Enter next data:");
        scanf("%d",&num);
    }
    return start;
}
struct node
{
    struct node *next;
    struct node *prev;
    int data;
};
struct node *start=NULL;

int main()
{
    int choice;
    printf("\n 1. create linked list");
    printf("\n 2. add in beginning");
    printf("\n 3. add at end");
    printf("\n 4. delete from beginning");
    printf("\n 5. delete from end");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1:start=create_linked_list(start);
        break;
        case 2: start=insert_begin(start);
        break;
        case 3:start=insert_end(start);
        break;
        case 4:start=delete_begin(start);
        break;
        case 5:start=delete_end(start);
        break;
    }
    return0;
}
Please log in to add an answer.