0% found this document useful (0 votes)
17 views13 pages

4 - DS-Doubly Linked List

The document discusses doubly linked lists and provides code examples for creating a doubly linked list and inserting, deleting nodes from the beginning, middle, and end of the list.

Uploaded by

Anandkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views13 pages

4 - DS-Doubly Linked List

The document discusses doubly linked lists and provides code examples for creating a doubly linked list and inserting, deleting nodes from the beginning, middle, and end of the list.

Uploaded by

Anandkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Doubly Linked List

Node data

DATA: the user's data


NEXT, PREV: the address of the next and
previous node in the list

prev data next


Inserting into a Doubly Linked List

1. newNode->back = location->back; 3. location->back->next=newNode;


2. newNode->next = location 4. location->back = newNode;
Deleting from a Doubly Linked List
Insertion of a node
Create List
struct node *createlist() if(r==NULL)
{ {
int i,size; r=n;
struct node *r,*n; current=n;
printf("\nEnter the size of list"); }
scanf("%d",&size); else
r=NULL; {
printf("Enter the elements"); current->next=n;
for(i=0;i<size;i++) n->prev=current;
{ current=n;
n=(struct }
node*)malloc(sizeof(struct node)); }
scanf("%d",&n->data); return r;
n->next=NULL; }
n->prev=NULL;
Insert Begin
struct node *insert_begin(struct node *r, int value)
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
n->data=value;
n->prev=NULL;
n->next=NULL;
if(r==NULL)
{
r=n;
current=n;
}
else
{
n->next=r;
r->prev=n;
r=n;
}
return r;
}
Insert End
struct node *insert_end(struct node *r, int value)
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
n->data=value;
n->prev=NULL;
n->next=NULL;
if(r==NULL)
{
r=n;
current=n;
}
else
{
current->next=n;
n->prev=current;
current=n;
}
return r;
}
Insert Middle
struct node* insert(struct node *r,int r=n;
value,int pos) }
else
{
{
int i; i=2;
struct node *p,*n,*tmp; for(p=r;p!=NULL;p=p->next)
n=(struct node*)malloc(sizeof(struct {
node)); if(i==pos)
n->data=value; {
tmp=p->next;
n->next=NULL; p->next=n;
n->prev=NULL; n->next=tmp;
if(r==NULL) n->prev=p;
{ tmp->prev=n;
printf("Empty List"); return r;
return NULL; }
i=i+1;
} }
else if(pos==1) }
{ printf("Given position not available. Exiting");
n->next=r; return r;
r->prev=n; }
Delete Begin
struct node *delete_begin(struct node *r)
{
struct node *p;
if(r==NULL)
{
printf("There is no element to delete");
}
else
{
r=r->next;
r->prev=NULL;
}
return r;
}
Delete End
struct node *delete_end(struct node *r)
{
struct node *p;
if(r==NULL)
{
printf("There is no element to delete");
}
else if(r->next==NULL)
r=NULL;
else
{
for(p=r;p->next->next!=0;p=p->next);

p->next=NULL;
current=p;
}
return r;
}
Delete Middle
struct node *delet(struct node *r,int value) if(p->next->data==value)
{ {
struct node *p,*tmp; tmp=p->next;
if(r==NULL) p->next=p->next->next;
{ tmp->next->prev=p;
printf("There is no element to delete"); return r;
return r; }
} }
else if(r->data==value) }
{ if(p->next->data==value)
r=r->next; {
r->prev=NULL; p->next=NULL;
return r; current=p;
} }
else else
{ printf("Element Not
for(p=r;p->next->next!=NULL;p=p->next) Available!. Exiting");
{ return r;
}
THANK YOU!!!

You might also like