SlideShare a Scribd company logo
Linked List
1
• A linked list is a sequence of data elements,
which are connected together via links. Each
data element contains a connection to
another data element in form of a pointer.
2
• A linked list is a data structure which can change
during execution.
– Successive elements are connected by pointers.
– Last element points to NULL.
– It can grow or shrink in size during execution of a
program.
– It does not waste memory space.
Why Linked List?
• The size of the arrays is fixed
• Insertion / Deletion of an element in an is
expensive.
3
Advantages of Linked Lists over arrays:
• Dynamic Array.
• Ease of Insertion/Deletion.
Drawbacks of Linked Lists:
• Random access is not allowed. We have to access elements
sequentially.
• Extra memory space for a pointer is required with each
element of the list.
• Basic operations on Linked Lists:
• Display
• Insertion
• Deletion
• Search
4
Function to initialize the node
object
class Node:
def __init__(self,data):
self. data=data
self.ref=None # next as null
Programming and Data Structure 5
Creating single linked list
6
Displaying the list
def display_list(self):
if self.head is None:
print("Empty List")
else:
print("Elements in the list are: ")
p=self.head
while p is not None:
print(p.data,"-->",end='')
p=p.ref
print() 7
Searching a List
def search(self, x):
p=self.head
position=1
while p is not None:
if p.data==x:
print("Found element =",x," At position = ",position)
return True
else:
p=p.ref
position+=1
print("Could not find element =",x)
return False
8
Insert at beginning
def insert_at_beginning(self,data):
temp=Node(data)
temp.ref=self.head
self.head=temp
9
Insert at end
def insert_at_end(self,data):
temp=Node(data)
# Case of empty list
if self.head is None:
self.head=temp
return
## Non-empty list, first traverse the list
p=self.head
while p.ref is not None:
p=p.ref
#Now p is pointing to last node in the list
p.ref=temp
10
Insert at position
def insert_at_position(self,data,k):
if k==1:
temp=Node(data)
temp.ref=self.head
self.head=temp
return
## Need to find position before k
p=self.head
i=1
while i < k-1 and p is not None:
p=p.ref
i+=1
if p is None: ## outside list bounds
print("you cannot insert beyond position index ",i)
else:
temp=Node(data)
temp.ref=p.ref
p.ref=temp 11
: Insertion
12
A
A
Item to be
inserted
X
X
A B C
B C
curr
tmp
def delete_node(self,del_element):
if self.head is None: #Case of Empty List
print("Empty List!")
return
# Case first node contain value
"del_element"
if self.head.data==del_element:
self.head=self.head.ref
return
13
## Traverse list up until predecessor of node containing
"del_element"
p=self.head
while p.ref is not None:
if p.ref.data==del_element: # Check if next cell contain
"del_element"
break
p=p.ref
if p.ref is None:
print("Element ",del_element, "is not in the list")
else:
p.ref=p.ref.ref
14
Illustration: Deletion
Spring 2012 Programming and Data Structure 15
A B
A B C
C
Item to be deleted
curr
tmp
Stacks:
• #Python code to demonstrate Implementing
stack using list
stack = ["Amar", "Akbar", "Anthony"] stack.append("Ram")
stack.append("Iqbal")
print(stack)
print(stack.pop())
print(stack)
print(stack.pop())
print(stack)
16
Stack Using Linked List
17
Stack Operations:
1. push() : Insert the element into
linked list nothing but which is
the top node of Stack.
2. pop() : Return top element
from the Stack and move the top
pointer to the second node of
linked list or Stack.
3. peek(): Return the top element.
4. display(): Print all element of
Stack.
IS_EMPTY(S)
if S.top == null
return TRUE
return FALSE
18
Queue
• A queue follows FIFO (First-in, First out) policy.
It is equivalent to the queues in our general
life. For example, a new person enters a queue
at the last and the person who is at the front
(who must have entered the queue at first)
will be served first.
19
• Enqueue → Enqueue is an operation which
adds an element to the queue.
• Dequeue → it returns and deletes the front
element from the queue.
• isEmpty → It is used to check whether the
queue has any element or not.
• isFull → It is used to check whether the queue
is full or not.
• Front → it returns the front element of the
queue (but don’t delete it).
20

More Related Content

Similar to linked.ppt

Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
MrsSHEEBAM
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
duraimurugan alagarsamy
 
03_LinkedLists_091.ppt
03_LinkedLists_091.ppt03_LinkedLists_091.ppt
03_LinkedLists_091.ppt
soniya555961
 
1.ppt
1.ppt1.ppt
linkedlist.ppt
linkedlist.pptlinkedlist.ppt
linkedlist.ppt
soniya555961
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
msohail37
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
KasthuriKAssistantPr
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
SSE_AndyLi
 
Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdf
KanchanPatil34
 
Linked list ppt
Linked list pptLinked list ppt
Linked list ppt
SiddhiDeshpade
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Linked List
Linked ListLinked List
Linked List
CHANDAN KUMAR
 
Data structure
 Data structure Data structure
Data structure
Shahariar limon
 
Link list
Link listLink list
Link list
zzzubair
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
sarala9
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked List
ThenmozhiK5
 

Similar to linked.ppt (20)

DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 
03_LinkedLists_091.ppt
03_LinkedLists_091.ppt03_LinkedLists_091.ppt
03_LinkedLists_091.ppt
 
1.ppt
1.ppt1.ppt
1.ppt
 
linkedlist.ppt
linkedlist.pptlinkedlist.ppt
linkedlist.ppt
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
 
Unit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdfUnit 1_SLL and DLL.pdf
Unit 1_SLL and DLL.pdf
 
Linked list ppt
Linked list pptLinked list ppt
Linked list ppt
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Linked List
Linked ListLinked List
Linked List
 
Data structure
 Data structure Data structure
Data structure
 
Link list
Link listLink list
Link list
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked List
 
Savitch ch 13
Savitch ch 13Savitch ch 13
Savitch ch 13
 

Recently uploaded

Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 

Recently uploaded (20)

Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 

linked.ppt

  • 2. • A linked list is a sequence of data elements, which are connected together via links. Each data element contains a connection to another data element in form of a pointer. 2
  • 3. • A linked list is a data structure which can change during execution. – Successive elements are connected by pointers. – Last element points to NULL. – It can grow or shrink in size during execution of a program. – It does not waste memory space. Why Linked List? • The size of the arrays is fixed • Insertion / Deletion of an element in an is expensive. 3
  • 4. Advantages of Linked Lists over arrays: • Dynamic Array. • Ease of Insertion/Deletion. Drawbacks of Linked Lists: • Random access is not allowed. We have to access elements sequentially. • Extra memory space for a pointer is required with each element of the list. • Basic operations on Linked Lists: • Display • Insertion • Deletion • Search 4
  • 5. Function to initialize the node object class Node: def __init__(self,data): self. data=data self.ref=None # next as null Programming and Data Structure 5
  • 7. Displaying the list def display_list(self): if self.head is None: print("Empty List") else: print("Elements in the list are: ") p=self.head while p is not None: print(p.data,"-->",end='') p=p.ref print() 7
  • 8. Searching a List def search(self, x): p=self.head position=1 while p is not None: if p.data==x: print("Found element =",x," At position = ",position) return True else: p=p.ref position+=1 print("Could not find element =",x) return False 8
  • 9. Insert at beginning def insert_at_beginning(self,data): temp=Node(data) temp.ref=self.head self.head=temp 9
  • 10. Insert at end def insert_at_end(self,data): temp=Node(data) # Case of empty list if self.head is None: self.head=temp return ## Non-empty list, first traverse the list p=self.head while p.ref is not None: p=p.ref #Now p is pointing to last node in the list p.ref=temp 10
  • 11. Insert at position def insert_at_position(self,data,k): if k==1: temp=Node(data) temp.ref=self.head self.head=temp return ## Need to find position before k p=self.head i=1 while i < k-1 and p is not None: p=p.ref i+=1 if p is None: ## outside list bounds print("you cannot insert beyond position index ",i) else: temp=Node(data) temp.ref=p.ref p.ref=temp 11
  • 12. : Insertion 12 A A Item to be inserted X X A B C B C curr tmp
  • 13. def delete_node(self,del_element): if self.head is None: #Case of Empty List print("Empty List!") return # Case first node contain value "del_element" if self.head.data==del_element: self.head=self.head.ref return 13
  • 14. ## Traverse list up until predecessor of node containing "del_element" p=self.head while p.ref is not None: if p.ref.data==del_element: # Check if next cell contain "del_element" break p=p.ref if p.ref is None: print("Element ",del_element, "is not in the list") else: p.ref=p.ref.ref 14
  • 15. Illustration: Deletion Spring 2012 Programming and Data Structure 15 A B A B C C Item to be deleted curr tmp
  • 16. Stacks: • #Python code to demonstrate Implementing stack using list stack = ["Amar", "Akbar", "Anthony"] stack.append("Ram") stack.append("Iqbal") print(stack) print(stack.pop()) print(stack) print(stack.pop()) print(stack) 16
  • 17. Stack Using Linked List 17 Stack Operations: 1. push() : Insert the element into linked list nothing but which is the top node of Stack. 2. pop() : Return top element from the Stack and move the top pointer to the second node of linked list or Stack. 3. peek(): Return the top element. 4. display(): Print all element of Stack.
  • 18. IS_EMPTY(S) if S.top == null return TRUE return FALSE 18
  • 19. Queue • A queue follows FIFO (First-in, First out) policy. It is equivalent to the queues in our general life. For example, a new person enters a queue at the last and the person who is at the front (who must have entered the queue at first) will be served first. 19
  • 20. • Enqueue → Enqueue is an operation which adds an element to the queue. • Dequeue → it returns and deletes the front element from the queue. • isEmpty → It is used to check whether the queue has any element or not. • isFull → It is used to check whether the queue is full or not. • Front → it returns the front element of the queue (but don’t delete it). 20