Data Structure

Insertion At Begining in doubly linked list

Algorithm

```InsertAtBegDll(info,prev,next,start,end)
1.create a new node and address in assigned to ptr.
2.check[overflow] if(ptr=NULL)
write:overflow and exit
3.set Info[ptr]=item;
4.if(start=NULL)
set prev[ptr] = next[ptr] = NULL
set start = end = ptr
else
set prev[ptr] = NULL
next[ptr] = start
set prev[start] = ptr
set start = ptr
[end if]
5.Exit.
```

Fuction For Insert at begining

```void insertAtBeg(node **start,node **end,int item )
{
node *ptr;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*start==NULL)
{
*start=ptr;
*end=ptr;
}
else
{
ptr->prev = NULL;
ptr->next=*start;
(*start)->prev=ptr;
*start=ptr;
}
}
```

C programe for insertion at begining in dubly linked list

```#include<stdio.h>
#include<malloc.h>
#include<conio.h>
typedef struct Node
{
struct Node *prev;
int info ;
struct Node *next;
}node;

void createdub(node**,node**);
void insertAtBeg(node **,node**,int);
void display(node *);

void main()
{
int ch, item, pos;
node *start ,*end ;
start = end = NULL;
clrscr();
createdub(&start,&end);
printf("\nThe list is : ");
display(start);
printf("\n\nEnter the item to be inserted at begining :");
scanf("%d",&item);
insertAtBeg(&start,&end,item);
printf("\nNow the list is : ");
display(start);
getch();
}

void createdub(node **start,node **end)
{
int i,item ,k=1;
printf("\nEnter number of node: ");
scanf("%d",&i);
while(i)
{
node *ptr;
printf("\nEnter the info for node %d : ",k);
scanf("%d",&item);
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*start==NULL)
{
ptr->prev = ptr->next = NULL ;
*start = *end = ptr ;
}
else
{
ptr->prev = *end;
(*end)->next = ptr ;
ptr->next= NULL;
(*end)=ptr;
}
i--;
k++;
}
}
void insertAtBeg(node **start,node **end,int item )
{
node *ptr;
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*start==NULL)
{
*start=ptr;
*end=ptr;
}
else
{
ptr->prev = NULL;
ptr->next=*start;
(*start)->prev=ptr;
*start=ptr;
}
}

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