0% found this document useful (0 votes)
8K views53 pages

Linked List

The document discusses abstract data types and data structures. It explains that an abstract data type defines capabilities for data arrangement, while a data structure provides a concrete implementation. The document then focuses on the list abstract data type, describing its common operations and implementations using arrays and linked lists.

Uploaded by

abelweeknd61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8K views53 pages

Linked List

The document discusses abstract data types and data structures. It explains that an abstract data type defines capabilities for data arrangement, while a data structure provides a concrete implementation. The document then focuses on the list abstract data type, describing its common operations and implementations using arrays and linked lists.

Uploaded by

abelweeknd61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

#LifeKoKaroLift

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.

● An abstract data type an be understood with an example of the list


interface, which is not a data structure in itself but helps in the
implementation of other data structures.

● In more technical terms, an ADT is an entity which defines the


capabilities of a data arrangement.

● A data structure on the other hand is just the concrete implementation


of these ADTs. Examples are Array Lists and Linked Lists.
Studying basic ADTs: List

● Taking this discussion to the Object Oriented environment, an Abstract Data


Type, such as a list is best understood as an interface.

● 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) :

■ A1 A2 A3. . . .A4 A5 A6. . . .An


○ Each element has its POSITION
○ “n” denotes number of elements in the list or the LENGTH of the list
○ If n=0 we have an EMPTY LIST
○ Elements arranged in a linear fashion (Linearly Ordered)
■ Notion of PRECEDING and FOLLOWING elements
● A1 precedes A2
● A3 follows A2
■ Notion of FIRST and LAST elements
● A1 is FIRST element if n>=1
● An is LAST element if n>=1
Studying basic ADT: List

● 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

<Element_Type>[] arrList = new <Element_Type>[n]

int[] arrList = new int[n]


Implementation of List ADT: Linked List

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

Best Case Complexities

Data structure Access Search Insertion Deletion

Array O(1) O(1) O(1) (at LAST) O(1) (at LAST)

Singly Linked list O(1) (FIRST) O(1) (at FIRST) O(1) (at FIRST) O(1) (at FIRST)
Implementation of List ADT: Linked List

Worst Case Complexities

Data structure Access Search Insertion Deletion

Array O(1) O(1)


O(N) O(1)
O(N) O(1)
O(N)

Singly Linked list O(N) O(N) O(N) O(N)


element element element element

Head

Head NULL

element element element element

NULL NULL
Define a node of a linked list

What does a node have?


1. Data
element 2. Reference
Define a node of a linked list

What does a node have?


1. Data
element
a. What type of data? ⇒ Any
type (we will use int for
simplicity)
2. Reference
a. What type of reference?
i. What is “type” of
reference?
Define a node of a linked list

What does a node have?


2. Reference
element a. What type of reference?
i. What is “type” of
reference? => Type of
reference tells what
kind of object will be
referred to by the
reference variable
Define a node of a linked list

What does a node have?


2. Reference
element a. What type of reference?
i.
What is “type” of reference? =>
Type of reference tells what kind
of object will be referred to by the
reference variable
String s1 (s1 is a reference that will
refer to String objects)

Student s1 (s1 will refer to student


object)
Define a node of a linked list

What does a node have?


2. Reference
element a. What will be the type of
reference “next"?
i. Since next will refer to
a Node (the next node
in the list) thus it will be
of same type as the
node
Define a Linked List

What does a linked list


have?
element 1. It only has reference to
head of the list (the
very first node)
2. It may have other
properties like number
of nodes in the list, etc.
Linked List Visualizations

● Adding elements to BEGINNING OF a list when list is empty (and element is


passed to add() method)

WHEN SOMEONE CREATES A LINKED-


LIST OBJECT using the class that we have
defined. . . the head should be NULL
Because LINKED LIST does not even
have the first node. . . .
Linked List Visualizations

● Adding elements to BEGINNING OF a list when list is empty (and element is


passed to addFirst() method)
LinkedList Object

element
head = null
Linked List Visualizations

● Adding elements to BEGINNING OF a list when list is empty (and element is


passed to add() method)
Node Object

Create a node with element


newNode
Element and put null in
“next” variable

Beacause newly created


NULL
node is NOT referring to any
other node . . . .
Linked List Visualizations

● Adding elements to BEGINNING OF a list when list is empty (and element is


passed to add() method)

Make head of the list refer element


newNode
to that node. . .

LinkedList Object

head NULL
Linked List Visualizations

● Adding elements to BEGINNING OF a list when list is NOT empty (and


element is passed to add() method)

elemen
t

LinkedList Object

head
Linked List Visualizations

● Adding elements to BEGINNING OF a list when list is NOT empty (and


element is passed to add() method)

1 Create a node with


Element and put null in
LinkedList Object next variable

NULL Node Object


head
2
newNode

NULL
Linked List Visualizations

● Adding elements to BEGINNING OF a list when list is NOT empty (and


element is passed to add() method)
Make the new node refer to
1 current head of the list
(Note: head is still referring to
that node)
LinkedList Object

NULL Node Object


head
2
newNode
Linked List Visualizations

● Adding elements to BEGINNING OF a list when list is NOT empty (and


element is passed to add() method)
Make newly created node as the
1 current head

LinkedList Object

NULL Node Object


head
2
newNode
Linked List Visualizations

● Adding elements to BEGINNING OF a list when list is NOT empty (and


element is passed to add() method)

The END RESULT will


newNode
Look like this. . . .
LinkedList Object

head 1
2

Newly created Node has reference to the previous head


NULL
Linked List Visualizations

● Adding elements to LAST OF a list when list is empty (and element is


passed to add() method)

NULL
LinkedList Object

head
Linked List Visualizations

● Adding elements to LAST OF a list when list is empty (and element is


passed to add() method)
Node Object

Create a node with element


newNode
Element and put null in
“next” variable

Because newly created


NULL
node is NOT referring to any
other node . . . .
Linked List Visualizations

● Adding elements to LAST OF a list when list is empty (and element is


passed to add() method)

Make head of the list refer element


newNode
to that node. . .

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)

element element element element

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)

element element element element

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

NEW NODE at the end of the list


Linked List Visualizations

● 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

LinkedList Object The question is:


head
How will we reach the LAST NODE? (we don’t have any direct
reference to it. . . .SO WE WILL HAVE TO USE THE head to
reach the last node
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 Start with temp at head. . .


Move temp until it reaches NULL
the last node 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
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

● Adding elements to a certain position between (1) and


(sizeOfList + 1) (say position = 2)

NULL
Linked List Visualizations

● Adding elements to a certain position between (1) and


(sizeOfList + 1)

NULL
Linked List Visualizations
head

NULL

Create a new node that has the element and refers to


NULL

NULL
Linked List Visualizations
head

NULL

To insert it at any position “p” you have to go to (p-


1)th node that is JUST BEFORE the position you want
to insert at

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

After reaching the 3rd position . . .


Make newNode refer to the the next of temp
Say p = 4 (I want to add a node at 4th position0 temp

head

NULL

Then make
next of temp . .
.refer to the
new node
NULL

The final result will be. . .


NULL
NULL
Linked List Visualizations

● Adding elements to a list when list is empty (and element is passed to add()
method)

Create a node with element


Element and put null in next
variable
Linked List Visualizations

● Adding elements to a list when list is empty (and element is passed to add()
method)

Make head of the list refer element


to that node. . .

head
Thanks for
Listening!

You might also like