0% found this document useful (0 votes)
6 views18 pages

2024-10-22 Ads With Java - Day 4

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)
6 views18 pages

2024-10-22 Ads With Java - Day 4

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/ 18

Insert( element )

new Nodel)
- Make space for new element, say newNode.>
-
Node new Node =
;
- Store element in newNode’s data. > new Node. date= element;
-

- Set newNode’s next to empty. > new Noc next-mull,


-

- if list is empty then > if


-
Chead == nul)S
- Make newNode as first (and only) node of list. > head
- = newNode ;
- Stop -return ;
3
// List is not empty
// => Find first node having data greater than newNode’s data.
- Set current to first node.
- Set previous to empty. => current-head a
- while (current is not empty) do > while (current := mull)9
-

- if (current node’s data > newNode’s data) then > if data &
-
Current ,

// Found the node new Nochr data)


- End the traversal. -> break
- Set previous to current. - ,
> previous - current
- Move current to current’s next node. ;
-
> current = currentinent
- if (previous is empty) then // newNode’s data is smallest > if <previous == mull &
-

// Add newNode as first node.


Node. Neet-head ;
- Set newNode’s next to first node. > new
-

- Make newNode as first node. > head newNod ;


- =

- Stop. - returne ,
3
// Add newNode between previous and current Node
- Set newNode as next of previous. > previous next= ;
new
-

- Set current as next of newNode. > newxlodk next current


;
-
=
-

- Stop.

(1) previous empty


est
-

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
-

linked list that uses head and tail pointers.


useot
list
using single pointer (currents
in a
.

.
① Do not let current become
empty
XI ,
last mode.
Stop traversal at

While (current next != null


head
head

N D E
EX ↑


current
ewNode newMode
current
node
let a

previous current
headI
I, ↓ delete (10)

Et
-

nochis
Set previous
next to current

headI Modis next

Etis
special
crea
② list has only one

noc .

③ Element not found ?


① Dehte first noch !
Delete( element )
- Set current to first node (head)
- Set previous to empty.
- while (current is not empty) do
- if (current node’s data = element) then
- End the traversal. // Element found
- Set previous to current node.
- Move current to current node’s next node.

- if (current is empty) then // Element not present in list


- Stop

- if (current node is first node of list) then // Deleting first node


- Move head to head’s next node.
- Mark current node as free.
- Stop.

- Set previous node’s next to current node’s next.


- Mark current node as free.
- Stop.
node
Ot
given a
delet Node (
head Node
current)
Det

current

3
① Swap current node and its next modes

values .

② Remove current's next noch .

can be taken Will not work if


care if lot has <
current is last mod
a
dummy noch after last dat mode .
Doubly Linked List

linked list in which each mode keep track

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

- while (current is not empty) do backwards .

- Process current node.


- Set current to current node’s next.
- Stop.

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 -

- Store element in newNode’s data.


e -

- Set previous of newNode as empty.


-

- if list is empty then


- Make newNode as last node.
-

Else
- Set newNode as previous of head.
-

- Set newNode’s next to head.

Front
-

- Set head to newNode.


-

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

- Set newNode’s next to empty.


-

- Set previous of newNode as empty.


-

- if list is empty then


- Set head to newNode.
-

- Set tail to newNode.


-

- 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

· empts head - empty


empts N =
*
tail
current
DeleteFirstNode()
- if list is empty then
- Stop.
- Set current to head
-

- Move head to head’s next node.


-

- if list is empty then // Check if head is empty, as head got changed


- Set tail to empty
-

Else // list is not empty


- Set previous of head to empty
-

- Release
- current node. Not needed in JAVA .

- Stop.
Adelement
to doubly list => insest

>
-

Create a sorted doubly list.


&

current
tul

#
head >
-

&

test not
(7)
-Insert be done

before b)
and c)
Node's to current
a) Set new next

newNode : next = current;

D) Set new Nodis previous to current's previous


new Node -
Previous = current. previous .

2) Set current noch's previous nodis next to


new Node .

next newMode ;
current
· previous =

d) Set current's previous to new Node.

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.

// 3. Traverse list to find node - current node.


- Set current to head (first node).
- while (current is not empty) do
- if (current node’s data > element) then
// Found the node, end the traversal.
- End the traversal.
- Set current to current’s next node.
// 4. If adding before the first node? - Current is the first node.
- if (current is head) then
- Before the first node comes newNode. // Set head's previous to newNode.
- After newNode comes the first node. // Set newNode'next to head.
- Make newNode as the first node. // Set head to newNode.
- Stop.

// 5. If adding after the last node? - Current is empty


- if (current is empty) then
- After the last node comes newNode. // Set tail's next to newNode.
- Before newNode comes the last node. // Set newNode's previous to tail.
- Make newNode as the last node. // Set tail to newNode.
- Stop.

// 6. Add a new node between current and current’s previous node.


- Make the current node come after newNode. // Set newNode's next to current.
- Make the current node's previous node come before newNode. // Set newNode's previous to
current node’s previous.
- Make newNode come after the current node's previous node. // Set current node's previous
node's next to newNode.
- Make newNode come before the current node. // Set current node's previous to newNode.
- Stop.

You might also like