0% found this document useful (0 votes)
26 views154 pages

INF210 Lec9 Data Structures

The document discusses linked lists, which are linear data structures made up of nodes where each node contains data and a reference to the next node. It describes how linked lists work and their basic components like nodes, data, and pointers. It also provides examples and diagrams illustrating linked lists and nodes.

Uploaded by

Nader Yasser
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)
26 views154 pages

INF210 Lec9 Data Structures

The document discusses linked lists, which are linear data structures made up of nodes where each node contains data and a reference to the next node. It describes how linked lists work and their basic components like nodes, data, and pointers. It also provides examples and diagrams illustrating linked lists and nodes.

Uploaded by

Nader Yasser
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/ 154

Data Structures

INF 210
Fall 2022-2023
Lecture 9

The LinkedList

INSTRUCTOR
DR/ AYMAN SOLIMAN & DR/ OSAMA FAROUK
Ø Contents
1) LinkedList Basics
2) Linked List Visualization
3) Adding and Removing Information
4) Time Complexity Equations
5) Uses for LinkedLists
6) Review and Conclusion
7) One Big Drawback

12/4/22 Dr/ Ayman Soliman 2


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 3


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 4


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 5


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 6


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 7


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 8


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 9


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 10


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 11


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 12


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Salaries 10,000 12,500 8,750 15,000

12/4/22 Dr/ Ayman Soliman 13


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Salaries 10,000 12,500 8,750 15,000

12/4/22 Dr/ Ayman Soliman 14


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Salaries 10,000 12,500 8,750 15,000

12/4/22 Dr/ Ayman Soliman 15


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 16


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Employee
Salary

Sector

Age

12/4/22 Dr/ Ayman Soliman 17


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 18


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node

12/4/22 Dr/ Ayman Soliman 19


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node

12/4/22 Dr/ Ayman Soliman 20


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node Node Node

12/4/22 Dr/ Ayman Soliman 21


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node

12/4/22 Dr/ Ayman Soliman 22


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node

Data

12/4/22 Dr/ Ayman Soliman 23


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node

Data Reference/
Pointer

12/4/22 Dr/ Ayman Soliman 24


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node
Strings

Data Reference/
Pointer

12/4/22 Dr/ Ayman Soliman 25


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node
Strings

Integers
Data Reference/
Pointer

12/4/22 Dr/ Ayman Soliman 26


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node
Strings

Integers
Data Reference/
Pointer
Booleans

12/4/22 Dr/ Ayman Soliman 27


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node
Strings

Integers
Data Reference/
Pointer
Booleans

12/4/22 Dr/ Ayman Soliman 28


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node
Strings

Integers The next Node in


Data Reference/
Pointer
the LinkedList
Booleans

12/4/22 Dr/ Ayman Soliman 29


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

12/4/22 Dr/ Ayman Soliman 30


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node

12/4/22 Dr/ Ayman Soliman 31


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node

Reference/
Data Pointer

12/4/22 Dr/ Ayman Soliman 32


q The Linked List - LinkedList Basics
Ø A LinkedList is a sequential access linear data structure in which every
element is a separate object called a Node, which has 2 parts
o The data
o The reference (or pointer) which points to the next Node in the List

Node Node Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

12/4/22 Dr/ Ayman Soliman 33


q The Linked List - Linked List Visualization

Linked List

12/4/22 Dr/ Ayman Soliman 34


q The Linked List - Linked List Visualization

Linked List

Head Node

12/4/22 Dr/ Ayman Soliman 35


q The Linked List - Linked List Visualization

Linked List

Head Node

Reference/
Data Pointer

12/4/22 Dr/ Ayman Soliman 36


q The Linked List - Linked List Visualization

Linked List

Head Node

Reference/
Data Pointer

12/4/22 Dr/ Ayman Soliman 37


q The Linked List - Linked List Visualization

Linked List

Head Node

Reference/
Data Pointer

null

12/4/22 Dr/ Ayman Soliman 38


q The Linked List - Linked List Visualization

Linked List

Head Node

Reference/ Reference/
Data Pointer Data Pointer

null

12/4/22 Dr/ Ayman Soliman 39


q The Linked List - Linked List Visualization

Linked List

Head Node

Reference/ Reference/
Data Pointer Data Pointer

1 2

null

12/4/22 Dr/ Ayman Soliman 40


q The Linked List - Linked List Visualization

Linked List

Head Node

Reference/ Reference/
Data Pointer Data Pointer

1 2

null

12/4/22 Dr/ Ayman Soliman 41


q The Linked List - Linked List Visualization

Linked List

Head Node

Reference/ Reference/
Data Pointer Data Pointer

1 2

null

12/4/22 Dr/ Ayman Soliman 42


q The Linked List - Linked List Visualization

Linked List

Head Node

Reference/ Reference/
Data Pointer Data Pointer

1 2

null

12/4/22 Dr/ Ayman Soliman 43


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/
Data Pointer Data Pointer

1 2

null

12/4/22 Dr/ Ayman Soliman 44


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2

null

12/4/22 Dr/ Ayman Soliman 45


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 46


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 47


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 48


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 49


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 50


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 51


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 52


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 53


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 54


q The Linked List - Linked List Visualization

Linked List

Head Node Tail Node

Reference/ Reference/ Reference/


Data Pointer Data Pointer Data Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 55


q The Linked List - Adding and Removing Information

Stack

12/4/22 Dr/ Ayman Soliman 56


q The Linked List - Adding and Removing Information

Queue
Stack

12/4/22 Dr/ Ayman Soliman 57


q The Linked List - Adding and Removing Information

Data can only flow in and out of a few specified points

Queue
Stack

12/4/22 Dr/ Ayman Soliman 58


q The Linked List - Adding and Removing Information

Data can only flow in and out of a few specified points

Queue
Stack

12/4/22 Dr/ Ayman Soliman 59


q The Linked List - Adding and Removing Information

Data can only flow in and out of a few specified points

Queue
Stack

12/4/22 Dr/ Ayman Soliman 60


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

12/4/22 Dr/ Ayman Soliman 61


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Add to Head

Remove from
Head

12/4/22 Dr/ Ayman Soliman 62


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Add to the
Add to Head
Middle

Remove from Remove from


Head the Middle

12/4/22 Dr/ Ayman Soliman 63


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Add to the
Add to Head Add to Tail
Middle

Remove from Remove from Remove from


Head the Middle Tail

12/4/22 Dr/ Ayman Soliman 64


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 65


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 66


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 67


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 68


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 69


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 70


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Head Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 71


q The Linked List - Adding and Removing Information
Data can flow in and out of any point of a LinkedList

Head Node Tail Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 72


q The Linked List - Adding and Removing Information

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 73


q The Linked List - Adding and Removing Information
Adding to the Head of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 74


q The Linked List - Adding and Removing Information
Adding to the Head of a LinkedList

Make that new Node’s pointer point to the current Head of the LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 75


q The Linked List - Adding and Removing Information
Adding to the Head of a LinkedList

Make that new Node’s pointer point to the current Head of the LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 76


q The Linked List - Adding and Removing Information
Adding to the Head of a LinkedList

Make that new Node’s pointer point to the current Head of the LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 77


q The Linked List - Adding and Removing Information
Adding to the Head of a LinkedList

Make that new Node’s pointer point to the current Head of the LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 78


q The Linked List - Adding and Removing Information
Adding to the Head of a LinkedList

Make that new Node’s pointer point to the current Head of the LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 79


q The Linked List - Adding and Removing Information

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 80


q The Linked List - Adding and Removing Information
Removing from the Head of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 81


q The Linked List - Adding and Removing Information
Removing from the Head of a LinkedList

Set the Head Node’s pointer to a null value

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 82


q The Linked List - Adding and Removing Information
Removing from the Head of a LinkedList

Set the Head Node’s pointer to a null value

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 83


q The Linked List - Adding and Removing Information
Removing from the Head of a LinkedList

Set the Head Node’s pointer to a null value

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 84


q The Linked List - Adding and Removing Information
Removing from the Head of a LinkedList

Set the Head Node’s pointer to a null value

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 85


q The Linked List - Adding and Removing Information
Removing from the Head of a LinkedList

Set the Head Node’s pointer to a null value

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

12/4/22 Dr/ Ayman Soliman 86


q The Linked List - Adding and Removing Information
Removing from the Head of a LinkedList

Set the Head Node’s pointer to a null value

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 87


q The Linked List - Adding and Removing Information
Removing from the Head of a LinkedList

Set the Head Node’s pointer to a null value

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 88


q The Linked List - Adding and Removing Information
Removing from the Head of a LinkedList

Set the Head Node’s pointer to a null value

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 89


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 90


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList
Make the pointer of the new Node point to the Node after the location we want to insert at

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 91


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList
Make the pointer of the new Node point to the Node after the location we want to insert at
Set the Node before the location we want to insert at to point towards the new Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 92


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList
Make the pointer of the new Node point to the Node after the location we want to insert at
Set the Node before the location we want to insert at to point towards the new Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 93


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList
Make the pointer of the new Node point to the Node after the location we want to insert at
Set the Node before the location we want to insert at to point towards the new Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 94


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList
Make the pointer of the new Node point to the Node after the location we want to insert at
Set the Node before the location we want to insert at to point towards the new Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 95


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList
Make the pointer of the new Node point to the Node after the location we want to insert at
Set the Node before the location we want to insert at to point towards the new Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 96


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList
Make the pointer of the new Node point to the Node after the location we want to insert at
Set the Node before the location we want to insert at to point towards the new Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 97


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList
Make the pointer of the new Node point to the Node after the location we want to insert at
Set the Node before the location we want to insert at to point towards the new Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 98


q The Linked List - Adding and Removing Information
Adding a Node to the Middle of a LinkedList
Make the pointer of the new Node point to the Node after the location we want to insert at
Set the Node before the location we want to insert at to point towards the new Node

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null
Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 99


q The Linked List - Adding and Removing Information
Removing a Node from the middle of a LinkedList

Make the pointer of the Node previous to the one we’re removing, to now
point to the Node after the one we’re removing

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 100


q The Linked List - Adding and Removing Information
Removing a Node from the middle of a LinkedList

Make the pointer of the Node previous to the one we’re removing, to now
point to the Node after the one we’re removing

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 101


q The Linked List - Adding and Removing Information
Removing a Node from the middle of a LinkedList

Make the pointer of the Node previous to the one we’re removing, to now
point to the Node after the one we’re removing

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

Data Reference/Pointer

1.5

12/4/22 Dr/ Ayman Soliman 102


q The Linked List - Adding and Removing Information
Removing a Node from the middle of a LinkedList

Make the pointer of the Node previous to the one we’re removing, to now
point to the Node after the one we’re removing

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 103


q The Linked List - Adding and Removing Information

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 104


q The Linked List - Adding and Removing Information

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 105


q The Linked List - Adding and Removing Information
Adding to the Tail of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 106


q The Linked List - Adding and Removing Information
Adding to the Tail of a LinkedList

Make the current tail point towards the new Node you want to add

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 107


q The Linked List - Adding and Removing Information
Adding to the Tail of a LinkedList

Make the current tail point towards the new Node you want to add

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

Data Reference/Pointer null


4

12/4/22 Dr/ Ayman Soliman 108


q The Linked List - Adding and Removing Information
Adding to the Tail of a LinkedList

Make the current tail point towards the new Node you want to add

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

Data Reference/Pointer null


4

12/4/22 Dr/ Ayman Soliman 109


q The Linked List - Adding and Removing Information
Adding to the Tail of a LinkedList

Make the current tail point towards the new Node you want to add

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

Data Reference/Pointer null


4

12/4/22 Dr/ Ayman Soliman 110


q The Linked List - Adding and Removing Information
Adding to the Tail of a LinkedList

Make the current tail point towards the new Node you want to add

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

Data Reference/Pointer null

12/4/22 Dr/ Ayman Soliman 111


q The Linked List - Adding and Removing Information

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

Data Reference/Pointer null

12/4/22 Dr/ Ayman Soliman 112


q The Linked List - Adding and Removing Information
Removing from the Tail of a LinkedList

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

Data Reference/Pointer null

12/4/22 Dr/ Ayman Soliman 113


q The Linked List - Adding and Removing Information
Removing from the Tail of a LinkedList
Set the previous tail to point towards a null value instead of the current
tail

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

Data Reference/Pointer null

12/4/22 Dr/ Ayman Soliman 114


q The Linked List - Adding and Removing Information
Removing from the Tail of a LinkedList
Set the previous tail to point towards a null value instead of the current
tail

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

Data Reference/Pointer null

12/4/22 Dr/ Ayman Soliman 115


q The Linked List - Adding and Removing Information
Removing from the Tail of a LinkedList
Set the previous tail to point towards a null value instead of the current
tail

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

Data Reference/Pointer null

12/4/22 Dr/ Ayman Soliman 116


q The Linked List - Adding and Removing Information
Removing from the Tail of a LinkedList
Set the previous tail to point towards a null value instead of the current
tail

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 117


q The Linked List - Adding and Removing Information
Removing from the Tail of a LinkedList
Set the previous tail to point towards a null value instead of the current
tail

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 118


q The Linked List - Adding and Removing Information
Removing from the Tail of a LinkedList
Set the previous tail to point towards a null value instead of the current
tail

Data Reference/Pointer Data Reference/Pointer


Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 119


q The LinkedList - Time Complexity Equations

Accessing

12/4/22 Dr/ Ayman Soliman 120


q The LinkedList - Time Complexity Equations

Accessing

O(n)

12/4/22 Dr/ Ayman Soliman 121


q The LinkedList - Time Complexity Equations

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3
Accessing

null
O(n)

12/4/22 Dr/ Ayman Soliman 122


q The LinkedList - Time Complexity Equations

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3
Accessing

null
O(n)

12/4/22 Dr/ Ayman Soliman 123


q The LinkedList - Time Complexity Equations

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3
Accessing

null
O(n)

12/4/22 Dr/ Ayman Soliman 124


q The LinkedList - Time Complexity Equations

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3
Accessing

null
O(n)

12/4/22 Dr/ Ayman Soliman 125


q The LinkedList - Time Complexity Equations

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3
Accessing

null
O(n)

12/4/22 Dr/ Ayman Soliman 126


q The LinkedList - Time Complexity Equations

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3
Accessing

null
O(n)

12/4/22 Dr/ Ayman Soliman 127


q The LinkedList - Time Complexity Equations

Accessing Searching

O(n)

12/4/22 Dr/ Ayman Soliman 128


q The LinkedList - Time Complexity Equations

Accessing Searching

O(n) O(n)

12/4/22 Dr/ Ayman Soliman 129


q The LinkedList - Time Complexity Equations

Data Reference/Pointer

Data Reference/Pointer

2
Accessing Searching
Data Reference/Pointer

3
O(n) O(n)
null

12/4/22 Dr/ Ayman Soliman 130


q The LinkedList - Time Complexity Equations

Data Reference/Pointer

Data Reference/Pointer

2
Accessing Searching
Data Reference/Pointer

3
O(n) O(n)
null

12/4/22 Dr/ Ayman Soliman 131


q The LinkedList - Time Complexity Equations

Data Reference/Pointer

Data Reference/Pointer

2
Accessing Searching
Data Reference/Pointer

3
O(n) O(n)
null

12/4/22 Dr/ Ayman Soliman 132


q The LinkedList - Time Complexity Equations

Data Reference/Pointer

Data Reference/Pointer

2
Accessing Searching
Data Reference/Pointer

3
O(n) O(n)
null

12/4/22 Dr/ Ayman Soliman 133


q The LinkedList - Time Complexity Equations

Data Reference/Pointer

Data Reference/Pointer

2
Accessing Searching
Data Reference/Pointer

3
O(n) O(n)
null

12/4/22 Dr/ Ayman Soliman 134


q The LinkedList - Time Complexity Equations

Accessing Searching Inserting Deleting

O(n) O(n)

12/4/22 Dr/ Ayman Soliman 135


q The LinkedList - Time Complexity Equations

Accessing Searching Inserting Deleting

O(n) O(n)

12/4/22 Dr/ Ayman Soliman 136


q The LinkedList - Time Complexity Equations

Accessing Searching Inserting Deleting

O(n) O(n)

12/4/22 Dr/ Ayman Soliman 137


q The LinkedList - Time Complexity Equations

Accessing Searching Inserting Deleting

O(n) O(n)

12/4/22 Dr/ Ayman Soliman 138


q The LinkedList - Time Complexity Equations

Accessing Searching Inserting Deleting

O(n) O(n) O(1) O(1)

12/4/22 Dr/ Ayman Soliman 139


q The LinkedList - Time Complexity Equations

Accessing Searching Inserting Deleting

O(n) O(n) O(n) O(1) O(n) O(1)

12/4/22 Dr/ Ayman Soliman 140


q The LinkedList - Uses for LinkedLists
Ø LinkedLists can be used in the backing of other data structures

o We can use LinkedLists to make Stacks, Queues, etc.

12/4/22 Dr/ Ayman Soliman 141


q The LinkedList - Uses for LinkedLists
Ø LinkedLists can be used in the backing of other data structures

o We can use LinkedLists to make Stacks, Queues, etc.

Array
Behind the Scenes
ArrayList

12/4/22 Dr/ Ayman Soliman 142


q The LinkedList - Uses for LinkedLists
Ø LinkedLists can be used in the backing of other data structures

o We can use LinkedLists to make Stacks, Queues, etc.

Data Reference/Pointer

1
Data Reference/Pointer

null

12/4/22 Dr/ Ayman Soliman 143


q The LinkedList - Uses for LinkedLists
Ø LinkedLists can be used in the backing of other data structures

o We can use LinkedLists to make Stacks, Queues, etc.

Data Reference/Pointer

1
Data Reference/Pointer

null
Stack

12/4/22 Dr/ Ayman Soliman 144


q The LinkedList - Uses for LinkedLists
Ø LinkedLists can be used in the backing of other data structures

o We can use LinkedLists to make Stacks, Queues, etc.

Data Reference/Pointer
Queue
1
Data Reference/Pointer

null
Stack

12/4/22 Dr/ Ayman Soliman 145


q The LinkedList - Uses for LinkedLists

T i t l e

Length

12/4/22 Dr/ Ayman Soliman 146


q The LinkedList - Uses for LinkedLists

12/4/22 Dr/ Ayman Soliman 147


q The LinkedList - Review + Conclusion
Ø A LinkedList is sequential access linear data structure in which every element
is a separate object called a node, containing 2 parts
o The data
o The reference (or pointer) which points to the next node in the List

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 148


q The LinkedList - One Big Drawback

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 149


q The LinkedList - One Big Drawback

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 150


q The LinkedList - One Big Drawback

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 151


q The LinkedList - One Big Drawback

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 152


q The LinkedList - One Big Drawback

Data Reference/Pointer Data Reference/Pointer Data Reference/Pointer

1 2 3

null

12/4/22 Dr/ Ayman Soliman 153


12/4/22 Dr/ Ayman Soliman 154

You might also like