Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Linked Lists

Rizoan Toufiq
Assistant Professor
Department of Computer Science & Engineering
Rajshahi University of Engineering & Technology
Rajshahi-6204
Outline 2/71

• Introduction
• Linked List
• Representation of Linked Lists in Memory
• Traversing a Linked List
• Searching a Linked List
• Memory Allocation; Garbage Collection
• Insertion into a Linked List
• Deletion from a Linked List
• Header Linked List
• Two Way Lists

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
3/56

Linked List

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Linked List 4/71

• A linked list or one way list is a linear collection of data


elements, called nodes, where the linear order is given by
means of pointers.
• Each node is divided into two parts:
– The first part - the information of the element/node
– The second part- the address of the next node
• There is a special pointer Start/List contains the address
of first node in the list.
• If this special pointer contains null, means that List is
empty.

Introd
uction
Rizoan Toufiq, Assistant Prof, Dept. of CSE, RUET 4 and
Overvi
5/56

Representation of Linked Lists in


Memory

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Representation of Linked Lists in
Memory 6/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Representation of Linked Lists in
Memory 7/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Representation of Linked Lists in
Memory 8/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create A Linked List 9/71

ch = ‘H’
INFO LINK

START -1 0 2
1 3
2 4
3 5
AVAIL 0 4 6
5 7
6 8
7 9
8 1
9 -1

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create A Linked List 10/71

INFO LINK

START -1 0 2
1 3

NEW 0 2 4
3 5
AVAIL 0 4 6
AVAIL 2 5 7
6 8
AVAIL != NULL, (TRUE)
NEW = AVAIL, 7 9
AVAIL = LINK[AVAIL] 8 1
9 -1

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create A Linked List 11/71

START -1 INFO LINK

START 0 0 H -1
1 3
PTR 0 2 4
NEW 0 3 5
AVAIL 2 4 6
5 7
INFO[NEW] = ‘H’
6 8
LINK[NEW ] = -1
7 9
START == -1 (TRUE) 8 1
START = NEW
9 -1
PTR = START

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create A Linked List 12/71

INFO LINK

START 0 0 H -1
1 3
PTR 0 2 4
NEW 0 3 5
NEW 2
AVAIL 2 4 6
AVAIL 4
5 7
AVAIL!=NULL (TRUE)
6 8
NEW = AVAIL 7 9
AVAIL = LINK[AVAIL] 8 1
9 -1

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create A Linked List 13/71

ch = ‘I’
INFO LINK

START 0 0 H 2
-1

PTR 2 1 3
PTR 0 2 I -1
NEW 2 3 5
AVAIL 4 4 6
5 7
INFO[NEW] = ‘I’
6 8
LINK[NEW ] = -1
7 9
START == -1 (FALSE) 8 1
LINK[PTR] = NEW;
9 -1
PTR = NEW

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Available Memory Check 14/71

Algorithm: (Check Memory Overflow) This algorithm check available


memory.
1. [OVERFLOW] If AVAIL = NULL, then: Write: OVERFLOW,
and Exit
2. [Remove first node from AVAIL.]
Set NEW:=AVAIL and AVAIL := LINK[AVAIL]
3. Exit

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create A Linked List 15/71

Algorithm: (Create a Linked List) This algorithm create a linked list with n
nodes.
1. START := NULL
2. Repeat Steps 3 to 5 for I = 1 to N
[OVERFLOW] If AVAIL = NULL, then: Write:
OVERFLOW, and Exit
3. [Remove first node from AVAIL.]
Set NEW:=AVAIL and AVAIL := LINK[AVAIL]
4. Set INFO[NEW] := ITEM and LINK[NEW] := NULL
5. If START = NULL, then:
Set START := NEW and PTR : = START
Else:
Set LINK[PTR] := NEW and PTR = NEW
[End of If structure]
6. Exit

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using Array 16/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using Array 17/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using Array 18/71

INFO LINK

AVAIL 0 0 2
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 1
9 -1

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using Array 19/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using Array 20/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using Array 21/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using Array 22/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using
Structure 23/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using
structure 24/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using
Structure 25/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using
Structure 26/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
Create a Linked List Using
Structure 27/71

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers
28/71

END

Rizoan Toufiq, Assistant Prof., CSE, RUET Arrays, Records and Pointers

You might also like