2024-10-22 Ads With Java - Day 4
2024-10-22 Ads With Java - Day 4
new Nodel)
- Make space for new element, say newNode.>
-
Node new Node =
;
- Store element in newNode’s data. > new Node. date= element;
-
- if (current node’s data > newNode’s data) then > if data &
-
Current ,
- Stop. - returne ,
3
// Add newNode between previous and current Node
- Set newNode as next of previous. > previous next= ;
new
-
- Stop.
insert emb1
current
↓
Ad
↓ X X
newNode
↓
X
read headIn
t DD
(10)
-sect
previous
↓ Nod
In
head new
DON
-
current > emply
↓
head
↓
ITDT-MAX
Exercise : Implement insert for a sorted
singly
-
.
① Do not let current become
empty
XI ,
last mode.
Stop traversal at
N D E
EX ↑
↑
↑
current
ewNode newMode
current
node
let a
previous current
headI
I, ↓ delete (10)
Et
-
nochis
Set previous
next to current
Etis
special
crea
② list has only one
noc .
3
① Swap current node and its next modes
values .
of its two
adjacent modes
head
I
Itail
NITTN
class Noch &
int data ;
Node next;
Node
previous;
3
averal > Forward traversal - Same as
singly list
I
-
traversal
Backward/Reverse traversal
.
↓
starts with last
Forward Traversal
- Set current to first node of list. node and more
Backward Traversal
- Set current to last node of list.
- while (current is not empty) do
- Process current node.
- Set current to current node’s previous .
- Stop.
AddAtFront( element )
J
- Make space for new element, say newNode.
e -
Else
- Set newNode as previous of head.
-
Front
-
- Stop. wil
Fronts
ant
new Node
-
tail
Mode
head
(
# tail
=Iit MAN
d At End
~
addAEnd
tail
tail
headn
I => A
new Node
addAta
-
head I stail
# NN
ewNode
AddAtEnd( element )
J
- Make
-
space for new element, say newNode.
- Store element in newNode’s data.
-
- Stop.
- Set newNode as next of tail node.
-
- Set previous of newNode to tail.
- Set tail to newNode.
-
- Stop.
Node()
delirst
headus tail heada tail
N = ND
↑
current
- Release
- current node. Not needed in JAVA .
- Stop.
Adelement
to doubly list => insest
>
-
current
tul
↓
#
head >
-
&
test not
(7)
-Insert be done
before b)
and c)
Node's to current
a) Set new next
next newMode ;
current
· previous =
newNode
current
previous =
;
-pecial
cases
Set new Node as first and last
. - mode .
① list is empty
add new Node as first
② Adding smallest value. -
mode
③ Adding largest valueIf add new Node as last
mode .
Insert( element )
// 1. Create new node
- Make memory for new element, say newNode.
- Store element in newNode’s data.
- Set newNode’s next and previous to empty.
// 2. If list is empty?
- if head is empty then
// Make newNode as the first and last node of the list.
- Set head and tail to newNode.
- Stop.