Linked List
Linked List
Linked List
1
Data Structures
● The terms data structures and Abstract Data Types are generally confused
with each other, but they are not the same.
● Data Structures like array lists and linked lists implement this interface.
Studying basic ADTs: List
● List ADT
○ A sequence of “n” elements of same type (n>=0) :
● List ADT
○ Define: END(L) (returns position following the last element or An)
○ Operations on List ADT
■ INSERT (x, p, L) :
● Inserts “x” at position “p” in List L (shifts elements to right)
■ LOCATE (x, L) :
● Returns position “p” in List L if element x is found at “p” or returns
END(L)
■ RETREIVE (p, L) :
● Returns element at position “p” in List L and UNDEFINED if p = END(L)
or p is not a position in L
■ DELETE (p, L) :
● Removes an element at position “p” of List L
● Undefined if p = END(L) or p is not a valid position
Studying basic ADT: List
● List ADT
○ Operations on List ADT
■ NEXT (p, L) :
● Returns the POSITION following “p” in the list
● END(L) if p = n (last position of the list)
● Undefined if p = END(L)
● Undefined is “p” is not a position in L
■ PREVIOUS (p, L) :
● Returns the POSITION preceding “p” in the list
● Undefined if p = 1
● Undefined is “p” is not a position in L
■ MAKENULL (L) :
● Causes L to become empty list and returns position END(L)
■ FIRST (L) :
● Returns first position in List L
● END(L) if list is empty
Studying basic ADT: List
● List ADT
○ Operations on List ADT
■ PRINTLIST (L) :
● Print elements in the order of occurrence
Implementation of List ADT: Arrays
0 1 2 3 4 5 n-1
A1 A2 A3 A4 A5 A6 ..... An
IntegerListNode IntegerListNode
IntegerListNode
A1 A2 A2 An
.....
head
list1 (object)
Implementation of List ADT: Linked List
Node Node
Node
A1 A2 A2 An
.....
Python code that creates a Node
head
list1 (object)
Implementation of List ADT: Linked List
Singly Linked list O(1) (FIRST) O(1) (at FIRST) O(1) (at FIRST) O(1) (at FIRST)
Implementation of List ADT: Linked List
Head
Head NULL
NULL NULL
Define a node of a linked list
element
head = null
Linked List Visualizations
LinkedList Object
head NULL
Linked List Visualizations
elemen
t
LinkedList Object
head
Linked List Visualizations
NULL
Linked List Visualizations
LinkedList Object
head 1
2
NULL
LinkedList Object
head
Linked List Visualizations
LinkedList Object
head NULL
Linked List Visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
LinkedList Object
head
NULL
Linked List Visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
LinkedList Object
head
NULL
Linked List Visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method) [What is the end result that we expect]
element
element
element element element
LinkedList Object
head NULL
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Create a new node
With data = element
and
Next = null
element
element element element
element
LinkedList Object
head
NULL
NULL
Linked List Visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Make the last node refer to
the newly created node
element
element element element
element
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Make the last node refer to
the newly created node
element
element element element
element
LinkedList Object
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Make the last node refer to
the newly created node
element
element element element
element
LinkedList Object
head
The question is:
NULL
How will we know that it is the last node?
NULL
temp => The last node has next = null
Linked List Visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Make the last node refer to
the newly created node
element
element element element
element
LinkedList Object
head
NULL
NULL
temp
Linked List Visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Make the last node refer to
the newly created node
element
element
element element element
LinkedList Object
head
NULL
temp
Linked List Visualizations
NULL
Linked List Visualizations
NULL
Linked List Visualizations
head
NULL
NULL
Linked List Visualizations
head
NULL
NULL
Linked List Visualizations
head
temp
NULL
Start with temp (at head) . . .then temp must jump (p-
2) times to reach the (p-1)th node
NULL
Say p = 4 (I want to add a node at 4th position0 temp
head
NULL
head
NULL
Then make
next of temp . .
.refer to the
new node
NULL
● Adding elements to a list when list is empty (and element is passed to add()
method)
● Adding elements to a list when list is empty (and element is passed to add()
method)
head
Thanks for
Listening!