+ All Categories
Home > Engineering > Doubly linked list (animated)

Doubly linked list (animated)

Date post: 02-Jul-2015
Category:
Upload: divyeshkumar-jagatiya
View: 731 times
Download: 2 times
Share this document with a friend
Description:
All Operation with Algorithms of Doubly linked list (Animated)
21
Doubly - Linked List
Transcript
Page 1: Doubly linked list (animated)

Doubly-Linked List

Page 2: Doubly linked list (animated)

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

Page 3: Doubly linked list (animated)

Info

prev next

NODE

Node Data

Info : the user’s data.

Prev, Next : the address of next and

previous node in list

Page 4: Doubly linked list (animated)

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.

Page 5: Doubly linked list (animated)

Create doubly linked list

NULL

last

7 X

node

9X X

Page 6: Doubly linked list (animated)

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

Page 7: Doubly linked list (animated)

7 X9

lastfirst

We assume linked list

2X X

Insert an element at beginning

doubly linked list

Page 8: Doubly linked list (animated)

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

Page 9: Doubly linked list (animated)

X 2 7 9

lastfirst

We assume linked list

4 XX

Insert an element at last of

doubly linked list

Page 10: Doubly linked list (animated)

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

Page 11: Doubly linked list (animated)

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

Page 12: Doubly linked list (animated)

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

Page 13: Doubly linked list (animated)

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

Page 14: Doubly linked list (animated)

X 2 7 9

lastfirst

X4

We assume linked list

ffirst

X

Delete an element at beginning

of doubly linked list

Page 15: Doubly linked list (animated)

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

Page 16: Doubly linked list (animated)

X 2 7 9

lastfirst

X4

We assume linked list

t

X

Delete an element at last of

doubly linked list

Page 17: Doubly linked list (animated)

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

Page 18: Doubly linked list (animated)

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

Page 19: Doubly linked list (animated)

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

Page 20: Doubly linked list (animated)

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

Page 21: Doubly linked list (animated)

Recommended