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

Algo - 2 Linked Linear List L2

Uploaded by

Mohamed Hafnaoui
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 views30 pages

Algo - 2 Linked Linear List L2

Uploaded by

Mohamed Hafnaoui
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/ 30

Amar Telidji University -Laghouat 1

Computer science department

Algorithms and data


structure 3

Dr BENDOUMA Tahar
Updated 27/02/2023

www.yourwebsite.com Bendouma.T
2

Introduction to pointers and dynamic


allocation
Course outline
Linked linear lists

Stacks and Queues

Recursion

Trees
Bendouma.T
3

Introduction to pointers and dynamic


allocation
Course outline
Linked linear lists

Stacks and Queues

Recursion

Trees
Bendouma.T
4

Linked Linear Definition of Linked Linear Lists

Implementation of Linked linear lists


Lists

Type of Linked linear lists

Operations on Linked linear Lists


Bendouma.T
Reminder … 5

• The new operator produces the address of an unused chunk of heap


memory large enough to hold the data type (dynamically allocated)

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

❖ A linked list is a linear collection of data elements called nodes in which


linear representation is given by links from one node to the next node.
❖ Similar to array, it is a linear collection of data elements of the same type.
❖ Different from array, data elements of linked list are generally not lined in
consecutive memory space; instead they are dispersed in various locations.

❖ A linked list can be perceived as a train or a sequence of nodes in which


each node contains one or more data fields and a pointer to the next node.

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

A linear linked list (LLL) is characterized by:

• the address of its first element or node (The head).

• the successor of a node in the list, which is the node immediately


following it..

• 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

Static allocation Allocation dynamique

Size ✔ Known Unknown


Fixed ✔ Variable

position of a data By index By address

Access to data ✔ Direct Indirect

Insert and delete element Require shift ✔ Very simple (by chaining)

Manipulation and Update ✔ simple et direct complicated and indirect

Bendouma.T
1
Type of Linked linear lists 6

1 - Simple linked lists

2 - Circular linked lists

3 - Double linked lists

4 - Circular double linked lists


Bendouma.T
1
Type of Linked linear lists (Simple linked lists)
7

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

• Frequently, we need to traverse a sequence in BOTH directions efficiently


• Solution : Use doubly-linked list where each node has two pointers
Type
List :^ Node
Node = record
Val : Any type (according to needs)
Succ, Pred : List ( pointer to the successor and predecessor)
L
end

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

Traversal, e.g. calculate the sum of nodes, display elements


Insert node :
at beginning, at end, after/before a node
Delete node
at beginning, at end, after/before a node
Search node
Sort

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

P ^ .succ Q 7 6 4 1 3 8 nil 9 nil

Bendouma.T
2
Some operations on Linked linear Lists (Delete) 6

Delete the first Node :


L

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

9 7 6 4 1 3 nil 8 nil nil


Before ^ .succ nil

Delete (P)

Bendouma.T
2
Some operations on Linked linear Lists (create list) 8

Create list of n elements given by user:


Code 1: Code 2:
P nil L nil
For i 1 to N Do For i 1 to N Do

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

Let L be a sorted linked linear list (LLL) of integers


a) We want to insert the new value x in L, write the action insert_sorted( ).
b) We want to delete the value y from L if y exist, if there are many occurrence
of y we just delete the first one, write the action delete_sorted( ).

Bendouma.T
3
0

o n s
s ti
q ue
o ur
Y
Bendouma.T

You might also like