Doubly Linked List. Contents Linked list Doubly linked list Structure Insertion of node ...

Post on 21-Dec-2015

256 views 2 download

Transcript of Doubly Linked List. Contents Linked list Doubly linked list Structure Insertion of node ...

Doubly Linked List

Contents

Linked list Doubly linked list Structure Insertion of node Deletion of node Traversing of node Application Advantage & disadvantage

Linked List Linked list is data structure used to store

similar data in memory (may or may not be in adjacent memory location )

Types of linked list1. Singly linked list2. Doubly linked list3. Circular linked list

Structure of linked list

Struct node{ int info; struct node *next; };

info next

node

Doubly linked list

Doubly linked list is linked data structure that consist of sequentially linked records called nodes and each node consist of two fields called links.

Two fields(links):1. Previous (backward)2. Next(forward)

Head is the starting node or first node Tail is the ending node or last node

Circular linked lists

Last node references the first node Every node has a successor No node in a circular linked list contains

NULL

Representation of node

Node of doubly linked list consist of three parts:

1. previous 2. data3. next

prev data

next

node

Representation

If there is only one sentinel node then the list is circularly linked via the sentinel node. It can be concept as two singly linked list formed from the same data item , but in opposite sequencial order

Structure of node

doubly linked list is a list that contains links to links to next and previous nodes . Doubly linked list allows traversal in both ways

typedef struct*node ;{ void *data; struct node *next; struct node *prev;} node;

Dummy head nodes

Eliminates the special case for insertion into & deletion from beginning of linked list.

Dummy head node • Always present, even when the linked list is

empty.• insertion & deletion algorithms initialize

previous to reference the dummy head node rather than NULL

(a) A circular doubly linked list with a dummy head node(b) An empty list with a dummy head node

Double-ended list

Doubly –linked list can also be created as double-ended list

The reference to the last link permits to insert a new link directly at the end of the list as well as at the beginning.

This could not be done with ordinary linked list without traversing the whole list .

NULLFirstlast

Doubly linked list

Inserting a node in doubly linked list

A node can be inserted:a)after given nodeb)before given nodec) at the beginning of an empty list

d) at the end of an empty list

Inserting a node in doubly linked list

Inserting a node in doubly linked listSuppose a new node, B needs to be inserted

after the node ACode:

void insertafter()struct node *B;B=(struct node *)malloc(sizeof(struct node));A->next = B->next; A->next = B; B->prev = A; (B->next)->prev = B;

Inserting a node in doubly linked listSuppose a new node, B needs to be

inserted before the node C

void insertbefore()struct node *B;B=(struct node *)malloc(sizeof(struct

node));C->prev=B->prev;C->prev=B;B->next=C;(B->next)->prev = B;

Inserting a node in doubly linked listSuppose a new node, B needs to be

inserted before the node A

Void insertbegin()Struct node *B;B=(struct node *)malloc(sizeof(struct

node));B->next=A;A->prev=B;B->prev=NULL;

Inserting a node in doubly linked listSuppose a new node, B needs to be

inserted after the node C

Void insertend()Struct node *B;B=(struct node *)malloc(sizeof(struct

node));C->next=B;B->prev=C;B->next=NULL;

Page 20 20

Deleting a Node from a Doubly-Linked List

Deleting a node requires that we logically remove the node from the list by changing various links and then physically deleting the node from the list (i.e., return it to the heap).

Any node in the list can be deleted. Note that if the only node in the list is to be deleted, an empty list will result. In this case the head pointer will be set to NULL.

To logically delete a node:◦ First locate the node itself .

◦ Change the predecessor’s and succesor’s link fields to point each other .

◦ Recycle the node using the free() function.

Page 21 21

Deleting the First Node from a Doubly-Linked List

Deletion At First:

If(first==NULL) printf(“memory of link list is empty”);else{ *p=firstFirst=first->rptrfirst->lptr=NULLFree(p)}

Page 22 22

DELETION OF FIRST NODE

Before:

After:

75 12446

75 124Recycled

7723

46 77

p

first

first

N N

N N

Page 23 23

Deleting a Last Node From a Doubly-Linked ListThe Code

if (first == NULL)

{

printf(“memory of link list is empty”);

}

Else

{

*p=first

while(p->rptr != NULL)

{

P=p->rptr

}

p->lptr->rptr=NULL

free(p)

Deletion At Last Node Before:

Page 24 24

After:

44 68 14

Deleted Node

146844

N N

NN

P

Traversing the Doubly linklist

1.Traversal of a doubly-linked list can be in either direction.

2.In fact, the direction of traversal can change many times, if desired. 

Traversal is often called iteration, but that choice of terminology is unfortunate, for iteration has well-defined semantics (e.g., in mathematics) which are not analogous to traversal.

A doubly linked list can be traversed either way and that too very conveniently.

1.Inorder/Forward Traversal2.reversorder/Backward Traversal

Inorder Traversal To traverse the doubly linked list, we walk the list

from the beginning, and process each element until we reach the last element.

void traverseinorder(node *head) { while(head!=NULL) { printf("%dn",head->info); head=head->next; } }   To call the above function, use:

traverseinorder(head);

Revers order traversalThe following listing shows how to traverse a doubly linked list in the backward direction. Note that the tail pointer will need to be passed in a call to this function.

void traversereverseorder(node *tail)

{

if(tail!=NULL)

{

printf("%dn",tail->info); //print data

tail = tail->prev; // go to previous node

}

}  

To call the above function, use: traversereverseorder(tail);

Applications of a doubly linked list- A great way to represent a deck

of cards in a game.- Applications that have a Most Recently Used (MRU) list (a linked list of file names) - A stack, hash table, and binary tree can be implemented using a doubly linked list.  - Undo functionality in Photoshop or Word (a linked list of state).

Advantage

We can traverse in both directions i.e. from starting to end and as well as from end to starting.

It is easy to reverse the linked list.

If we are at a node, then we can go to any node. But in linear linked list, it is not possible to reach the previous node.

Disadvantage

It requires more space per node because one extra field is required for pointer to previous node.

Insertion and deletion take more time than linear linked list because more pointer operations are required than linear linked list.

THANK YOU