Algo - 2 Linked Linear List L2
Algo - 2 Linked Linear List L2
Dr BENDOUMA Tahar
Updated 27/02/2023
www.yourwebsite.com Bendouma.T
2
Recursion
Trees
Bendouma.T
3
Recursion
Trees
Bendouma.T
4
P adresse
P^
P : is a pointer variable.
p^: is location whose address is stored in P
Bendouma.T
Definition of Linked linear Lists 6
Bendouma.T
Definition of Linked linear Lists (List Elements: Nodes) 7
Linked list element (node) is user defined structure data type, typically
contains two parts
o Data variables
o pointers to next node, hold the addresses of next node
Freight “data”
Pointer/reference
Data (data) Connector
(connector)
Node
Bendouma.T
Implementation of Linked linear lists 8
Type
List :^ Node
Node = record
Val : Any type (according to needs)
Succ : List ( pointer to the next node)
end
a b c d
Bendouma.T
Implementation of Linked linear lists… 9
• Null: indicates the address which does not point to any node.
Bendouma.T
1
Implementation of Linked linear lists (example)
0
Example :
P P L P
b a b
4 - New (L)
2- P^Val `b`
5- L^Val `a`
1- New (P) 3- P^succ Nil
6- L^succ P
Bendouma.T
1
Implementation of Linked linear lists (example 2)
1
Example 2 :
Q P
a b
Q
P Q P
P Q
Q P
a b a b
P^.s P Q
ucc
Q
a b
Bendouma.T
1
Implementation of Linked linear lists (example 2)
2
P
P Q
a b a b
Q P^succ
Q
c c
Bendouma.T
1
most frequently errors… (double allocation) 3
P ?
P
a
New(P)
Bendouma.T
1
most frequently errors… (ghost reference) 4
Q P Q
P
? ?
delete(P)
Bendouma.T
1
Static allocation vs dynamic allocation 5
Insert and delete element Require shift ✔ Very simple (by chaining)
Bendouma.T
1
Type of Linked linear lists 6
START
1 2 3 4 5 6 7 nil
Bendouma.T
1
Type of Linked linear lists (Circular linked lists)
8
• May need to cycle through a list repeatedly, e.g. round robin system
for a shared resource
• Solution : Have the last node point to the first node
1 2 3 4 5 6 7
Bendouma.T
1
Type of Linked linear lists (Double linked lists)
9
1 5 2 8 4
Bendouma.T
2
Type of Linked linear lists (Circular double linked lists)
0
1 5 2 8 4
Bendouma.T
2
Some operations on Linked linear Lists 1
Bendouma.T
2
Some operations on Linked linear Lists (Traversal) 2
9 7 6 4 1 3 8 nil
P P^ .succ
P
9 7 6 4 1 3 8 nil
P P^ .succ P
9 7 6 4 1 3 8 nil
Bendouma.T
2
Some operations on Linked linear Lists (Traversal) … 3
P L
While(P ≠ NIL) do
…….
……
…….
P P^ .succ
Done
Bendouma.T
2
Some operations on Linked linear Lists (insert) 4
At the beginning:
EX: insert 9
L
7 6 4 1 3 8 nil
New (P) P L
7 6 4 1 3 8 nil
P ^ .val 9 P L
P ^ .succ L
9 7 6 4 1 3 8 nil
P L
L P
L 9 7 6 4 1 3 8 nil
Bendouma.T
2
Some operations on Linked linear Lists (insert) 5
At the end:
EX: insert 9 at the end
L
P L 7 6 4 1 3 8 nil
While P ^ .succ ≠ nil Do
P P ^ .succ P
Done L
7 6 4 1 3 8 nil
L P Q
New (Q)
Q ^ .val 9
7 6 4 1 3 8 nil 9 nil
Q ^ .succ nil
P Q
L
Bendouma.T
2
Some operations on Linked linear Lists (Delete) 6
7 6 4 1 3 8 nil
P L L
P
7 6 4 1 3 8 nil
L
L L ^ .succ
P
7 6 4 1 3 8 nil
L
Delete (P) P
nil
7 6 4 1 3 8 nil
Bendouma.T
2
Some operations on Linked linear Lists (Delete) 7
L
P Delete the last Node :
P L
Before L
9 7 6 4 1 3 8 nil
Before
Before P
P L L
While P ^ .succ ≠ nil Do
Before P
9 7 6 4 1 3 8 nil
P P ^ .succ
Done
Before P
L
Delete (P)
Bendouma.T
2
Some operations on Linked linear Lists (create list) 8
✔ ✔
New (L) New (P)
Input (x) Input (x)
L^ .Val x P^ .Val x
L^ .succ P if L = Nil then
P L L P
Done else
Q^ .succ P
EndIf
Q P
Done
What is the difference ????? P^ .succ Nil
You must find the difference on your own
Help: Try adding the numbers from 1 to N (1,2,3,,,,N) in order for code1 and code2
Bendouma.T
2
Work required 9
Bendouma.T
3
0
o n s
s ti
q ue
o ur
Y
Bendouma.T