0% found this document useful (0 votes)
10 views40 pages

2023 Slot09 LinkedList

Uploaded by

Mỹ Linh Bàng
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)
10 views40 pages

2023 Slot09 LinkedList

Uploaded by

Mỹ Linh Bàng
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/ 40

University of Science – VNU-HCM

Faculty of Information Science


Department of Computer Science
MTH083 - Advanced Programming for Artificial Intelligence

Slot 09 –
Singly Linked List
Advisor:
Dr. Nguyễn Tiến Huy
Dr. Lê Thanh Tùng

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 1
Content

1 Linked List
2 Linked List Operations
3 Linked List with Tail

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 2
Introduction

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 3
Linked List
▪ A linked list is a linear data structure that includes a series of connected
nodes. Here, each node stores the data and the address of the next
node

▪ Unlike Arrays, Linked List elements are not stored at a contiguous


location

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 4
Advantages of Linked List
▪ Dynamic Data structure: based on the operation insertion or deletion
▪ Ease of Insertion/Deletion
▪ Efficient Memory Utilization: avoids the wastage of memory
▪ Implementation: Various advanced data structures can be implemented
using a linked list like a stack, queue, graph, hash maps, etc

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 5
Linked List
▪ So, how do linked lists differ than arrays
▪ An array is direct access; we supply an element number and can go
directly to that element (through pointer arithmetic) – via index
▪ With a linked list, we must either start at the head or the tail pointer
and sequentially traverse to the desired position in the list

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 6
Linked List
▪ So, how about the fundamental operations:
▪ Declare …
▪ Input and Output …
▪ Traversal …
▪ Find an element from …
▪ Add an element into …
▪ Delete an element from …
▪ Sort the … in ascending/descending

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 7
Types of Linked List

▪ Singly Linked List:

▪ Doubly Linked List:

▪ Circular Linked List

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 8
Singly Linked List

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 9
Singly Linked List
▪ A linear linked list starts out as empty
▪ An empty list is represented by a null pointer
▪ We commonly call this the head pointer

nullptr
head

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 10
Node in Linked List
▪ A node in a linked list contains one or more members that represent data
▪ Each node also contains (at least) a link to another node

▪ A linked list is a collection of nodes


▪ The first node is called the head

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 11
Create a Linked List
▪ Create the node to store the data
class Node:
def __init__(self, data):
self.data = data
self.next = None

▪ Create a class LinkedList to represent your linked list


class LinkedList:
def __init__(self):
self.head = None
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 12
Singly Linked List
▪ As we add the first data item, the list gets one node added to it with the
following procedure:
▪ Create a node containing data and connection
▪ Add node into the current list nullptr
head

current list
data data
connection connection
pointer pointer

new node head new node

1. Create node 2. Add node into list

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 13
Singly Linked List
▪ To add another data item we must first decide in what order
▪ does it get added at the beginning
▪ does it get inserted in sorted order
▪ does it get added at the end
▪ does it get added at the position pos

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 14
Add Node to Head
▪ Assume that we will control a linked list by only pointer head

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 15
Add Node to Head
def addHead(self, data):
newNode = Node(data)
if self.head is None:
self.head = newNode
else:
newNode.next = self.head
self.head = newNode

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 16
Add Tail

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 17
Add Node to Tail
▪ Assume that we will control a linked list by only pointer pHead

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 18
Sequential Traversal
▪ Move to the next node via the next pointer:
// move to the next node
pCur = pCur.next;

▪ Display all the members in the linked list

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 19
Traverse a Linked List
▪ Print all value of data in a linked list with pointer head
def traverse(self):
if self.head is None:
print("Empty List")
return
else:
curNode = self.head
while (curNode is not None):
print(curNode.data, end = "->")
curNode = curNode.next
▪ Note: Must not change the value of head node
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 20
Add Tail
▪ So, to add a node in the end of linked list, we should go to the last node
of the list
▪ Step 1: Traverse into the end of the list
▪ Step 2: Change the next of ending node into the new node

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 21
Add Node to Tail
def addTail(self, data):
newNode = Node(data)
if self.head is None:
self.head = newNode
else:
curNode = self.head
while curNode.next is not None:
curNode = curNode.next
curNode.next = newNode

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 22
Remove First Node

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 23
Remove First Node

def removeHead(self):
if self.head is not None:
self.head = self.head.next

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 24
Remove Last Node

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 25
Remove Last Node
def removeTail(self):
if self.head is not None:
# only 1 Node
if self.head.next is None:
self.head = None
else:
curNode = self.head
while curNode.next.next is not None:
curNode = curNode.next
curNode.next = None

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 26
Singly Linked List

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 27
Linked List Vs Array
Array Linked List
Stored in contiguous location Not stored in contiguous location

Fixed in size Dynamic in Size

Memory is allocated at compile time Memory is allocated at run time

Less memory than linked list for each More memory, cuz it stores data and
element connection (address to next node)
Random Access via index Access via sequential traversal

Insertion and deletion ops takes time Insertion and deletion ops is faster

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 28
Exercise
▪ Write a function to get the length of singly linked list

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 29
Exercise
▪ Write a function to get the length of singly linked list
def getLength(self):
size = 0
curNode = self.head
while curNode is not None:
curNode = curNode.next
size += 1
return size

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 30
Exercise
▪ Write a function to get the length of singly linked list
def __len__(self):
size = 0
curNode = self.head
while curNode is not None:
curNode = curNode.next
size += 1
return size

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 31
Linked List
▪ Control a linked list with pointer to head and tail

▪ Now, let’s write again the function to add a node at the end of list

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 32
Linked List
def addTail(self, data):
newNode = Node(data)
if self.head is None:
self.head = newNode
self.tail = newNode
else:
self.tail.next = newNode
self.tail = newNode

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 33
Linked List
Tail Pointer Without Tail Pointer

Insertion at the End: sequentially


Insertion at the End: directly
traverse
Memory Usage: Slightly higher
More memory-efficient
due to the additional pointer
Use Cases: Suitable for
Use Cases: Better suited for
applications where end-insertions
scenarios such as queue
are rare or where the list is not
implementations
expected to grow significantly

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 34
Linked List with Tail
▪ Rewrite the function:
▪ Remove Last Node
▪ Add Node to Last

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 35
Exercise
▪ Write a function to add the value into the position pos of linked
list

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 36
Insert Node with Position
Example: Solution:
▪ Input: insert 10 into position 2 ▪ Traverse the Linked list up to
3→5→8→4 position -1 nodes.

▪ Out: ▪ Once all the position -1 nodes


are traversed, create new Node
3 → 5 → 10 → 8 → 4
▪ Point the next pointer of the
0 1 2 3 4 new node to the next of current
node.
▪ Point the next pointer of
current node to the new node.

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 37
Insert Node with Position
def insertAtPos(self, data, pos):
if pos == 0: self.addHead(data)
elif pos >= self.getLength(): self.addTail(data)
else:
curNode = self.head
while pos != 1:
curNode = curNode.next
pos = pos - 1
newNode = Node(data)
newNode.next = curNode.next
curNode.next = newNode
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 38
Exercise
▪ Insert a key into the linked list such that its ordering is
ascending

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 39
THANK YOU
for YOUR ATTENTION

Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 40

You might also like