TEAM CODING-BCA

DOUBLY LINKED LIST.


BASIC  FUNTIONS OF  DOUBLY  LINKED  LIST
-Submitted By Suman Kanrar.

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

void add_beg();
void add_after();
void add_before();
void add_end();
void display();
void delete_beg();
void delete_end();
void delete_pos();

struct node
{
            int data;
            struct node *next;
            struct node *prev;
};
struct node *start=NULL;





void main()
{
            int choice;
            printf("\n DOUBLY LINK LIST \n");
            while(1)
            {
                        printf("\n Enter 1 to add at the beginning ");
                        printf("\n Enter 2 to add after a position ");
                        printf("\n Enter 3 to add at the end ");
                        printf("\n Enter 4 to delete from the beginning ");
                        printf("\n Enter 5 to delete from the end ");
                        printf("\n Enter 6 add before a position ");
                        printf("\n Enter 7 to delete by position ");
                        printf("\n Enter 8 to Exit ");
                        printf("\n Enter 9 to Display ");
                       
                        printf("\n Enter your choice: ");
                        scanf("%d",&choice);

                        switch(choice)
                        {
                                    case 1:
                                                add_beg();
                                    break;

                                   
case 2:
                                                add_after();
                                    break;

                                    case 3:
                                                add_end();
                                    break;

                                    case 4:
                                                delete_beg();
                                    break;

                                    case 5:
                                                delete_end();
                                    break;

                                    case 6:
                                                add_before();
                                    break;

                                    case 7:
                                                delete_pos();
                                    break;

                                    case 8:
                                                exit(0);
                                    break;

                                    case 9:
                                                display();
                                    break; 
                                                                                                                       
                                    default:
                                                printf("\n Wrong choice Enter again!! ");
                        }
            }
}                                                                                              


                       
void display()
{
    struct node *temp;
    temp=start;
    while(temp!=NULL)
    {
                        printf("%d \t",temp->data);
                        temp=temp->next;
    }
}




void add_beg()
{
            struct node *tmp;
            tmp=(struct node *)malloc(sizeof(struct node));
            printf("\n Enter data ");
            scanf("%d",&tmp->data);

            tmp->prev=NULL;
            if(start==NULL)
                        tmp->next=NULL;
            else
            {
                        tmp->next=start;
                        start->prev=tmp;
            }
            start=tmp;
}







void add_after()
{
            struct node *tmp,*q;
            int pos,i;

            printf("\n Enter position ");
            scanf("%d",&pos);

            if(start==NULL)
            {
                        printf("\n Empty List ");
                        return;
            }

            q=start;
            for(i=0;i<pos-1;i++)
            {
                        q=q->next;
                        if(q==NULL)
                        {
                                    printf("\n Less Node Present");
                                    return;
                        }
            }
            tmp=(struct node *)malloc(sizeof(struct node));
            printf("\n Enter data: ");
            scanf("%d",&tmp->data);
            if(q->next!=NULL)
            {
                        q->next->prev=tmp;
                        tmp->next=q->next;
                        tmp->prev=q;
                        q->next=tmp;
            }
            else
            {
                        q->next=tmp;
                        tmp->prev=q;
                        tmp->next=NULL;
            }
}











void add_end()
{
            struct node *tmp,*q;
            tmp=(struct node *)malloc(sizeof(struct node));
            printf("\n Enter data: ");
            scanf("%d",&tmp->data);
           
            q=start;
            while(q->next!=NULL)
                        q=q->next;
                       
            tmp->next=NULL;
            q->next=tmp;
            tmp->prev=q;
}











void delete_pos()
{
            struct node *q;
            int pos,i;

            printf("\n Enter the position: ");
            scanf("%d",&pos);
           
            q=start;
            for(i=0;i<pos-1;i++)
            {
                        q=q->next;
                        if(q==NULL)
                        {
                                    printf("\n Position not found");
                                    return;
                        }
            }
            q->prev->next=q->next;
            q->next->prev=q->prev;
            if((pos==1)||(q==start))
            {
                        start=q->next;
            }
            free(q);
}

void delete_beg()
{
            struct node *q,*p;
            q=start;
            p=q->next;
            free(q);
            start=p;
}

void delete_end()
{
                        struct node *q,*p;
            q=start;
    if(q==NULL)
    {
               printf("\n Empty List ");
               return;
    }            
           
            while(q->next!=NULL)
                        q=q->next;

            if(q->next==NULL)
            {
                        q->prev->next=NULL;
        free(q);
                       
            }
            else
            {          
                        p=q->prev;
                        free(q);
                        p->next=NULL;
            }
}

void add_before()
{
            struct node *tmp,*q;
            int pos,i;

            printf("\n Enter position ");
            scanf("%d",&pos);
            q=start;i=1;
            if(start==NULL)
            {
                        printf("\n Empty List ");
                        return;
            }
            tmp=(struct node *)malloc(sizeof(struct node));
            printf("\n Enter data: ");
            scanf("%d",&tmp->data);
            while(i<pos-1)
            {
                        q=q->next;
                        i++;
            }

            if(q->next!=NULL)
            {
                        q->next->prev=tmp;
                        tmp->next=q->next;
                        tmp->prev=q;
                        q->next=tmp;
            }
            else
            {
                        q->next=tmp;
                        tmp->prev=q;
                        tmp->next=NULL;
            }

           
            /*
            q->next->prev=tmp;
            tmp->next=q->next;
            tmp->prev=q;
            q->next=tmp; */
}
                       
           






RESPECTIVE OUTPUTS

1.      ADD NODE AT THE BEGINNING







1.      ADD NODE AFTER A GIVEN POSITION

 

1.      ADD NODE AT THE END OF THE LINKED LIST

 

1.      DELETE NODE FROM THE BEGINNING OF THE LINK LIST

 

1.      DELETE NODE FROM THE END OF THE LINKED LIST

 

1.      ADD  A  NODE  BEFORE  A  SPECIFIED  POSITION


 

1.      DELETE  A  NODE  BY A  GIVEN  POSITION




No comments:

Post a Comment