You are on page 1of 5

Before the first node

– To attach newNode before the first node


– First set the `next’ field of newNode to Head
• Link (newNode) = Head
– Then set Head to be newNode.
• Head = newNode

head 8 12 1 NULL

newnode 10
Between two nodes
– To attach newNode right after current
– First set the `next’ field of newNode
• Link(newNode) = Link(current)
– Then set current’s next to be newnode.
• Link(current) = Link(newNode)

current

head 8 12 1 NULL

newNode 10
After the last node
– To attach newNode after the last node
– First set current’s next to be newNode.
• Link(current) = newNode
– Then set the `next’ field of newNode to NULL
• Link(newNode) = NULL

current

head 8 12 1 NULL

newnode 10
Deleting the First Element
• This can be done without any traversal/searching
• Check that the list is not empty
if (Head <> NULL)
• Simply move Head to the next node
Current = Head (will be used to
delete node from memory)
Head = Link(Head)
head

10 20 30 NULL

Current
if (Head <> NULL) //Check that the list
is not empty
Current = Head (will be used to
traverse nodes in the list)
While( Info(Link(Current)) <> data to
be deleted)
Current = Link(Current)
D = Link(Current) (will be used to free
memory reserved by this node)
Link(Current) = Link(Link(Current))
Free(D)
head D

10 20 30 40 NULL

Current
30

You might also like