2024-10-21 Ads With Java - Day 3
2024-10-21 Ads With Java - Day 3
element will
At Front
new
-Add
the element
=>
be
only
Initially list will be empts
of the list.
Front
At tail head
↓I
tail
2 -
[ =>
Et
ED newNode
add At Front
-
T
(10)
head
*
tail
Y
head ta
newodest
·
Noti
class Node & head tail
wit data ; E
!
3
Node next
;
<
Reference
/
new Node
Node head
; let
Node Tail
; Ent
21
Nock();
tail I new
dt
t
AddAtFront(element)
newNode Nodel);
- Make space for new element, say newNode. > Nod new=
-
2
- Set head to newNode. return ;
3
- Stop.
next head;
newlock
=
,
Front (5)
adat
head tail tail
head
Fut Est
new rea 1
Node = E
# raue
At Front (10)
a
head
tail
oran
new Noc
l
Due ↓
Dead tr
It us
22 51
AddAtFront(element) - Optimised
- Make space for new elements, say newNode.
- Store element in newNode’s data.
- Set newNode’s next to head.
- Set head to newNode.
- if tail is empty then
- Set tail to head.
- Stop.
End/add
At
At Rear
At Read (5)
& No tail
head empty
tail head ,
↓ -- => E
Node
El new
de
At a head
tail
↓, Y
E EttMD
new Node
AddAtRear(element)
new Nodel)
- Make space for new elements, say newNode. > Node new Node
- =
;
- Store element in newNode’s data. > new Node data-element
;
-
·
;
- Set head and tail to newNode. If (head mul
==
>
-
- Set tail’s next to newNode. fail newNode
=
2
- Set tail to newNode. return ;
2
- Stop. 3
Node
tail-next = new
;
tail new Node
=
;
Hint
-
:
Traverse to find last mode .
Node
Pet First
=> empty
Tead-empty tail .
deleFirst
aFirst
Noch()
twin
Read
headstai empts D
empty
=
D
tip M
temp head-empty
twin
DeleteFirstNode()
null) I
- if list is empty then > if (head
==
-
=
head;
Y
Ae
as list
void add Ativont (int element) ;
vid add At Rear (int element);
mit delete First Node
;
vid print();
Y
In list linked
stacking
blement
push() Ab
-
↓ I,
N
add At Front() delete FirstNoch()
① Reverse linked .
list
a
singly
head 7
EtoLTNRT >
-
Use stack.
↓, Reverse
>
-
Use 3
head t pointere .
Inte DesI]
Previous current
pointers
③ Find 19th last mode of list.
>
-
Use Stuck
>
- use 2
pointers
head t
hasale
lettest
>
-
or not
>
-
Node at a
time
time .
e
lit
>
-
add
ment a especific position .
previous current
head In ↓ ↓
D
E
new Node
① Create new Node
②
Store element to insert in new Nod's data
.
before current
>
-
to .
empts
>
-
Set previous
>
- while (current is not empty)
I -
if (current data > new Node
data)
.
>
- end traversal .
Set current
previous to
next to Node .
>
-
Set previous : new
>
-
Set new Node - next to current
&
Special/corner
cases
① Embly list .
② Adding swalst to
headed a
. As
value tot
③ Adding largest value to .
list If tail is
used to keep
track of last noc .
Ind
using single pointer
a
Insert( element )
- Make space for new element, say newNode.
- Store element in newNode’s data.
- Set newNode’s next to empty.
- if list is empty then
- Make newNode as first (and only) node of list.
- Stop