Doubly linked list (animated)

Post on 02-Jul-2015

731 views 2 download

description

All Operation with Algorithms of Doubly linked list (Animated)

transcript

Doubly-Linked List

Doubly Linked List

In doubly linked list each node contains two points.

which points has a reference to both the next point

and pervious point of node in list.

A doubly linked list is a two-way list because one

can move in either from left to right or from right to

left

Info

prev next

NODE

Node Data

Info : the user’s data.

Prev, Next : the address of next and

previous node in list

Operations on a Doubly linked list

o Create list.

o Insert element at beginning in list.

o Insert element at end in list.

o Insert element at any place in list.

o Delete element from the beginning of list.

o Delete element from the end of list.

o Delete element from any place from list.

Create doubly linked list

NULL

last

7 X

node

9X X

AlgorithmStep 1: [initially list is empty]

First = NULL

last = NULL

Step 2: [allocate space to newly created node]

new1= create new node

Step 3: [assign value to information part of node]

info[new1]= Value

Step 4: [assign NULL to the address part for the next]

next[new1]= NULL

Step 5: [check for list is empty]

if first =NULL then

first = new1

last = new1

prev[new1]= NULL

else

next[last]= new1

prev[new1]=last

last=new1

end if

Step 6: exit

7 X9

lastfirst

We assume linked list

2X X

Insert an element at beginning

doubly linked list

Algorithm

Step 1: [allocate space to newly created node]

new1= create new node

Step 2: [check for free space]

if new1=NULL then

Write “Memory full”

Step 3: [check for list is empty]

if first=NULL then

Write “list is empty”

return

Step 4: [assign value to information part of node]

info[new1]= Value

Step 5: [store the node at first]

next[new1]= first

prev[new1]= NULL

prev[first]= new1

first=new1

Step 6: exit

X 2 7 9

lastfirst

We assume linked list

4 XX

Insert an element at last of

doubly linked list

Algorithm

Step 1: [check for list is empty]

if first=NULL then

Write “list is empty”

return

Step 2: [allocate space to newly created node]

new1= create new node

Step 3: [assign value to information part to node]

info[new1]= Value

Step 4: [store the node at last]

next[new1]= NULL

next[last]= new1

prev[new1]= last

last=new1

Step 5: exit

X 2 7 9

lastfirst

X4

We assume linked list and

Insert after 7 valued node

6

tra

t1

Insert an element at Any place

doubly linked list

Algorithm

Step 1: [check for list is empty]

if first=NULL then

Write “list is empty”

return

Step 2: [allocate space to newly created node]

new1= create new node

Step 3: [read values of information part of new node]

info[new1]=value

Step 4: [initialization]

tra=first

Step 5: [perform insertion operation]

repeat through step 7 while tra != NULL

Step 6: if info[tra] =no then

if tra=last then

next[tra]=new1

next[new1]=NULL

prev[new1]=tra

last=new1

else

t1=next[tra]

next[tra]=new1

next[new1]=t1

prev[t1]=new1

prev[new1]=tra

Step 7: [increment temp value]

tra=next[tra]

Step 8: exit

X 2 7 9

lastfirst

X4

We assume linked list

ffirst

X

Delete an element at beginning

of doubly linked list

Algorithm

Step 1: [check for list is empty]

if first=NULL then

Write “list is empty”

return

Step 2: [perform deletion opration]

if first=last then

first=NULL

last=NULL

free(first)

else

ffirst=next[first]

free(first)

first=ffirst

prev[first]=NULL

end if

Step 3: exit

X 2 7 9

lastfirst

X4

We assume linked list

t

X

Delete an element at last of

doubly linked list

Algorithm

Step 1: [check for list is empty]

if first=NULL then

Write “list is empty”

return

Step 2: [perform deletion opration]

if first=last then

first=NULL

last=NULL

free(first)

else

t=prev[last]

free(last)

last=t

next[last]=NULL

Step 3: exit

Delete an element at any place

doubly linked list

We assume linked list and

Delete 7 valued node

X 2 7 9

lastfirst

X4

tra

t1

pr1

Algorithm eny delete

Step 1: [check for list is empty]

if first=NULL then

Write “list is empty”

return

Step 2: [perform deletion opration]

if first=last then

first=NULL

last=NULL

free(first)

Step 3: [initialization]

tra=first

Step 4: [perform insertion operation]

repeat through step 7 while tra != NULL

Step 5: IF info[tra]=number then

if tra=first then

ffirst=next[first]

free(first)

first=ffirst

else if tra=first then

free(last)

last=pr1

next[last]=NULL

else

t1=next[tra]

next[pr1]=t1

prev[t1]=pr1

free(tra)

last=t1

Step 6: [Assign previous value of tra to prev]

pr1=tra

Step 7: [increment temp value]

tra=next[tra]

Step 8: exit