Sunday, 17 June 2012

DOUBLY LINKED LIST


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};
void main()
{
struct node *head,*tp,*temp;
int ch,key,value;
clrscr();
head = NULL;
printf("\n1.Insert @ first\t 2.Insert @ last\t 3.Insert @ middle\n");
printf("4.Deletion\t 5.Display");
do
{

printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: {
            printf("\nenter a no to insert @ first:");
            temp=(struct node*)malloc(sizeof(struct node));
            scanf("%d",&value);
            temp->prev=NULL;
            temp->data=value;
            temp->next=NULL;
            if(head==NULL)
            head=temp;
            else
            {
            temp->next=head;
            head->prev=temp;
            head=temp;
            }}
            break;
case 2: {
            printf("\nenter a no to insert @ last:");
            temp=(struct node*)malloc(sizeof(struct node));
            scanf("%d",&value);
            temp->prev=NULL;
            temp->data=value;
            temp->next=NULL;
            if(head==NULL)
            head=temp;
            else
            {
             tp=head;
             while(tp->next!=NULL)
             {
             tp=tp->next;
             }
             tp->next=temp;
             temp->prev=tp;
            }}
            break;
case 3: {
            printf("\nEnter a no to find:");
            scanf("%d",&key);
            printf("\nenter a no to insert @ middle:");
            temp=(struct node*)malloc(sizeof(struct node));
            scanf("%d",&value);
            temp->prev=NULL;
            temp->data=value;
            temp->next=NULL;
            if(head==NULL)
            head=temp;
            else
            {
            tp=head;
            while(tp!=NULL && tp->data!=key)
            tp=tp->next;
            if(tp!=NULL)
            {
             temp->next=tp->next;
             temp->prev=tp;
             tp->next->prev=temp;
             tp->next=temp;
             }
             else
             printf("\nElement not found...");
             }}
             break;
case 4: {
            printf("\nEnter a no to delete:");
            scanf("%d",&key);
            if(head==NULL)
            printf("\nlist is empty");
            else
            {
            tp=head;
            while(tp!=NULL && tp->data!=key)
            {
            tp=tp->next;
            }
            if(tp!=NULL)
            {
            if(tp==head)
            {
            head=head->next;
            head->prev=NULL;
            free(tp);
            }
            else
            {
            if(tp->next!=NULL)
            {
            temp->prev->next=tp->next;
            temp->next->prev=tp->prev;
            free(tp);
            }
            else
            {
            tp->prev->next=NULL;
            free(tp);
            }
            }}
            else
            printf("\nelement not found");
            }          }break;
case 5: {
            if(head!=NULL)
            tp=head;
            while(tp!=NULL)
            {
            printf("%d\t",tp->data);
            tp=tp->next;
            }
            }break;
default:printf("\nenter correct choice");
            break;
}
}while(ch<=5);
getch();
}

No comments:

Post a Comment