Showing posts with label Implementation of Doubly Linked List. Show all posts
Showing posts with label Implementation of Doubly Linked List. Show all posts

Sunday, February 22, 2015

Implementation of Doubly Linked List

AIM:
            To write a C program to implement doubly linked list.

ALGORITHM:
            Step 1: Define a list as a node of a structure with one data, one pointer
                        pointing to next element in the structure and another pointer pointing to
previous element in the structure.
            Step 2: Declare the function prototypes creation (), insertion (), deletion () and
display () to perform the list function.
Step 3: Declare the necessary pointer variables as structure node data type.
Step 4: Get a choice from the user.
Step 5: If the choice is create get first data from the user by calling the function
create () and display  contents of the list.
Step 6: If the choice is insert, get data and its position by calling function
insert (), assign the next and previous field of the given data node according to the position. Display the contents of the list.
Step 7: If the choice is delete, get the position of data which is going to be
removed from the list and assign next field of previous node to address.
Step 8: Repeat the steps 4, 5&6 until the choice is exit.
Step 9: Terminate the execution.       

PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{                 
int data;
struct node*next,*prev;
}*New,*New1,*temp,*start,*dummy;
void add();
struct node *get_node();
void display();
void Delete();
int find(int num);
int first=1;
void main()
{
char ans='y';
int choice,num,found,flag=0;
start=NULL;
do
{
clrscr();
printf("\nProgram for DLL\n");
printf("\n1.Insert\n2.Delete\n3.Diplay\n4.Search\n5.Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
add();break;
case 2:
Delete();break;
case 3:
display();break;
case 4:
       printf("\nEnter the number to be searched");
       scanf("%d",&num);
       temp=start;
       while((temp!=NULL)&&(flag==0))
       {
       found=find(num);
       flag=found;
       }
       if(found==1)
       printf("\nThe number is present\n");
       else
       printf("\nThe number is not present\n");
       break;
case 5:
       exit(0);break;
default:
            printf("\nInvalid option");break;
}
printf("\nDo you want to continue?(y/n)");
ans=getche();
}while(ans=='y'||ans=='Y');
getch();
}
void add()
{
clrscr();
New=get_node();
printf("\nEnter the element");
scanf("%d",&New->data);
if(first==1)
{
start=New;
first=0;
}
else
{
dummy=start;
while(dummy->next!=NULL)
dummy=dummy->next;
dummy->next=New;
New->prev=dummy;
}
}
struct node *get_node()
{
New1=malloc(sizeof(struct node));
New1->next=NULL;
New1->prev=NULL;
return(New1);
}
void display()
{
clrscr();
temp=start;
if(temp==NULL)
printf("\nThe DLL is empty\n");
else
{
while(temp!=NULL)
{
printf("\n\n%d=>",temp->data);
temp=temp->next;
}
printf("NULL");
}
getch();
}
int find(int num)
{
if(temp->data==num)
return 1;
else
temp=temp->next;
return 0;
}