Beruflich Dokumente
Kultur Dokumente
Introduction
• Linked list
– Linear collection of self-referential class objects, called
nodes
– Connected by pointer links
– Accessed via a pointer to the first node of the list
– Link pointer in the last node is set to null to mark the list’s
end
• Use a linked list instead of an array when
– You have an unpredictable number of data elements
– You want to insert and delete quickly.
Linked-list
p = list
info next info next info next info next
list
6 5 3 8 null
p
Removing an Element from the front
of a Linked List
info next
p 6 list = next(p)
info next info next info next
5 3 8 null
list
Removing an Element from the front
of a Linked List
info next
p 6 x = info(p)
info next info next info next
x=6
5 3 8 null
list
Removing an Element from the front
of a Linked List
info next
p freenode(p)
info next info next info next
x=6
list 5 3 8 null
Removing an Element from the front
of a Linked List
LocP
Delete a node at given location/position
Del(info, link, Start, Avail, Loc, LocP)
This algo deletes node N with location Loc. LocP is location of node which
precedes N or when N is first node then LocP = NULL
25
Grounded Header Link List
• A grounded header list is a header list where the last
node contains the null pointer.
• The term “grounded” comes from the fact that many
texts use the electrical ground symbol to indicate the
null pointer.
Start
Header Node
10 20 40 55 70
Head
Cur->prev Cur Cur->next
Two-way lists(cont…)
• Every node (except the last node) contains the
address of the next node, and every node (except the
first node) contains the address of the previous node.
• A two-way list (doubly linked list) can be traversed in
either direction.
Insertion in 2-way list
INSTwl(info, forw, back, Start, Avail, LocA, LocB, item)
Head 40
LocA LocB
New
Inserting after a given node location in 2-way linked list
insLOC(info, link, Start, avail, loc, item)
This algo. inserts ITEM so that ITEM follows node with location Loc or inserts ITEM
as first node when Loc=NULL
1. [Delete node]
Set (Loc back) forw = Loc forw and
(Loc forw) back = Loc back
2. [Return node to avail list]
Set Loc forw = Avail and Avail = Loc
3. Exit
10 20 40 55 70
Head
loc
Deleting an item from 2-way linked list
Delete()
1. Call FindB()
2. If Loc == NULL, then
Write: ITEM not in list and Exit
3. [Delete node]
If Locback == NULL, then
Set Start = Start forw [Deletes 1st node]
Else if Loc forw == NULL, then [Deletes last node, since back link not to be made]