0% found this document useful (0 votes)
7 views19 pages

2024-10-21 Ads With Java - Day 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views19 pages

2024-10-21 Ads With Java - Day 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Create Linked List

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 new Nock =

Nock();
tail I new

dt
t
AddAtFront(element)
newNode Nodel);
- Make space for new element, say newNode. > Nod new=
-

- Store element in newNode’s data. > new Node data-element


,
-
·

- Set newNode’s next to empty. > new Nock-next null


;
- =

- if list is empty then >


if (head == null) I
-

- Set head and tail to newNode.


>
-
head =new Node ;
- Stop.
new Node
-
>
- Set newNode’s next to head. tail =

2
- Set head to newNode. return ;
3
- Stop.
next head;
newlock
=
,

head new Node


;
=

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 newNode’s next to empty. next


=> newNod null =

- if list is empty then


,

;
- Set head and tail to newNode. If (head mul
==

> New Node


-
head
- Stop. =
;

>
-
- Set tail’s next to newNode. fail newNode
=

2
- Set tail to newNode. return ;

2
- Stop. 3
Node
tail-next = new
;
tail new Node
=
;

Implement add At Rear() without


Exercise :
-
using
noch) .
tail (not keeping tracket last

Hint
-
:
Traverse to find last mode .
Node
Pet First

delt First Node() Stop if list is

=> 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
==
-

- Stop - > return ;


3
- Set temp to head. - head
> temp :
;
- Set head to head’s next.
- if list is empty then head-head - next ;
- Set tail to head.
null) &
- Stop.
2Chead
tall
==

=
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]

② Find 2nd last mode of list.

head 7 > use Stuck


EtoLTNRT 2
Use
↑ ↑
>
-

Previous current
pointers
③ Find 19th last mode of list.

>
-
Use Stuck
>
- use 2
pointers

⑦ Detect if list contains a loop/cycle .


head t
=
No cycle
LotteSET

head t
hasale

lettest
>
-

keep trac if a mode is already visited

or not

>
-

use 2 pointers (have and tortoise)


↓ ↓
fast pointer slow pointer
H ↓
moves
moves abead
ahead one
two nodes at a

Node at a
time
time .
e
lit

>
-

add
ment a especific position .

element after value


>
-
add a
specific
>
-
add element to a sorted list
.

previous current
head In ↓ ↓

D
E
new Node
① Create new Node


Store element to insert in new Nod's data
.

③ Traverse list to find current mode (node


before which new Node will be added)·

Also have previous keep track A mode

before current

>
-

Set current to head

to .
empts
>
-
Set previous

>
- while (current is not empty)

I -
if (current data > new Node
data)
.
>
- end traversal .

Set current
previous to

Set current to current next

Add Node between previous and current


① new

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

// List is not empty


// => Find first node having data greater than newNode’s data.
- Set current to first node.
- Set previous to empty.
- while (current is not empty) do
- if (current node’s data > newNode’s data) then
// Found the node
- End the traversal.
- Set previous to current.
- Move current to current’s next node.
- if (previous is empty) then // newNode’s data is smallest
// Add newNode as first node.
- Set newNode’s next to first node.
- Make newNode as first node.
- Stop.

// Add newNode between previous and current


- Set newNode as next of previous.
- Set current as next of newNode.
- Stop.

You might also like