doubly linked list. contents linked list doubly linked list structure insertion of node ...

32
Doubly Linked List

Upload: buddy-blake

Post on 21-Dec-2015

253 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

Doubly Linked List

Page 2: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

Contents

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

Page 3: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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

Page 4: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

Structure of linked list

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

info next

node

Page 5: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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

Page 6: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

Circular linked lists

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

NULL

Page 7: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

Representation of node

Node of doubly linked list consist of three parts:

1. previous 2. data3. next

prev data

next

node

Page 8: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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

Page 9: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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;

Page 10: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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

Page 11: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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

Page 12: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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

Page 13: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

Doubly linked list

Page 14: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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

Page 15: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

Inserting a node in doubly linked list

Page 16: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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;

Page 17: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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;

Page 18: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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;

Page 19: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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)

Page 24: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

Deletion At Last Node Before:

Page 24 24

After:

44 68 14

Deleted Node

146844

N N

NN

P

Page 25: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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.

Page 26: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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

1.Inorder/Forward Traversal2.reversorder/Backward Traversal

Page 27: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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);

Page 28: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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);

Page 29: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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).

Page 30: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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.

Page 31: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

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.

Page 32: Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application

THANK YOU