SlideShare a Scribd company logo
1 of 19
CS250 - Data Structures and
Algorithm
BESE-2A/B
Linked List Operations
• Insert a node
– At the start of the list
– At the end of the list
• Delete a node
– At the start of the list
– At the end of the list
• Traverse linked list
– Iterate through each item/node
• Search
– Find the specified item in linked list
9/17/2012 DSA - Fall 2012 - SEECS, NUST 2
Insertion Corner Case
• Empty List
– With head pointer only
• Head = NULL
– With head and tail pointer only
• Head = Tail = NULL
Insert Item at Start
1. Empty node is created –
new node
2. Initialize the value to
user specified value
3. If list is empty
1. Set next pointer to null
2. Set head to new node
4. If list is not empty
1. Set the next pointer to
the head
2. Set head to new node
1. Node item = new
Node();
2. Item.data = data;
3. If (head == NULL)
1. Item.next = NULL;
2. Head = item;
4. Else
1. Item.next = head;
2. Head = item;
Insert Item at Start
• When list is empty
Insert Item at Start
• When list is not empty
Insert item at end
1. Empty node is created
2. Initialize the value to
user specified value
3. When list is not empty
1. Set next pointer of new
node to NULL
2. Set the next pointer of
last node to the new
node
4. When list is empty
1. Set the next pointer of
new node to NULL
2. Set head to new node
1. Node item = new Node();
2. Item.data = data;
3. If( head == NULL)
1. Item.next = NULL;
2. Tail.next = item;
4. Else
1. Item.next = NULL;
2. Head = Tail = item;
Insert Item at End
• When list
is not empty
Delete Item from Start
1. Set tempPtr to head
2. Set head to next of
itself / tempPtr
3. Delete tempPtr
1. tempPtr = head
2. Head = head->next
3. Delete tempPtr
Delete Item from Start
Delete Item from end
1. Find the node just
before the start by
traversing the list
2. Delete the last node
3. Set tail to current node
4. Set next of current
node to null
1. tempPtr->next = tail;
2. Delete tail
3. Tail = node
4. tempPtr->next = NULL
Delete Item from end
Delete Item from end
Deletion Corner Cases
• Empty List
– Prompt user
• Only one item in list
– Set head and tail to NULL
Traverse List
1. Set tempPtr to head
2. If tempPtr is not NULL
– Print node data value
– Set tempPtr to next of itself
– Go to 2
3. Else
– Exit
Search Item in Linked List
• Scan exiting list to find the specified item
1. Get the value to search
2. Set tempPtr to head
3. Check if tempPtr is NULL
4. No
1. If tempPtr data is same as user ’s data
• Exit Loop with Success
2. No
• Set tempPtr to tempPtr next value
• Go to 3
5. Yes
1. Exit Loop
Search Item in Linked List
Homework Assignment
• Middle of linked list
– Insert an item
– Delete an item
Questions?

More Related Content

What's hot (17)

Linked lists
Linked listsLinked lists
Linked lists
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
linked list
linked listlinked list
linked list
 
Lists
Lists Lists
Lists
 
Sorting
SortingSorting
Sorting
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Searching
SearchingSearching
Searching
 
Sorting
SortingSorting
Sorting
 
Lists and loops
Lists and loopsLists and loops
Lists and loops
 
Lists methods
Lists methodsLists methods
Lists methods
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Arrays
ArraysArrays
Arrays
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked list
 
Introduction to lists
Introduction to listsIntroduction to lists
Introduction to lists
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
 
Binary search python
Binary search pythonBinary search python
Binary search python
 

Similar to Link list 2

Circular linked list
Circular linked list Circular linked list
Circular linked list sajinis3
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxskilljiolms
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptxnikhilcse1
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversalkasthurimukila
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Double linked list.pptx
Double linked list.pptxDouble linked list.pptx
Double linked list.pptxAlbin562191
 
Ppt of operations on one way link list
Ppt of operations on one way  link listPpt of operations on one way  link list
Ppt of operations on one way link listSukhdeep Kaur
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptxmsohail37
 

Similar to Link list 2 (20)

Linked list
Linked listLinked list
Linked list
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
Circular linked list
Circular linked list Circular linked list
Circular linked list
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Double linked list.pptx
Double linked list.pptxDouble linked list.pptx
Double linked list.pptx
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
3.linked list
3.linked list3.linked list
3.linked list
 
Ppt of operations on one way link list
Ppt of operations on one way  link listPpt of operations on one way  link list
Ppt of operations on one way link list
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 

More from sana younas

7 habits of highly effective people
7 habits of highly effective people7 habits of highly effective people
7 habits of highly effective peoplesana younas
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphssana younas
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithmsana younas
 
circular linklist
circular linklistcircular linklist
circular linklistsana younas
 
Enterpise system
Enterpise systemEnterpise system
Enterpise systemsana younas
 
Database administration
Database administrationDatabase administration
Database administrationsana younas
 
Universal logic gate
Universal logic gateUniversal logic gate
Universal logic gatesana younas
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingsana younas
 
Memory management
Memory managementMemory management
Memory managementsana younas
 

More from sana younas (16)

7 habits of highly effective people
7 habits of highly effective people7 habits of highly effective people
7 habits of highly effective people
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphs
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Binary search
Binary searchBinary search
Binary search
 
circular linklist
circular linklistcircular linklist
circular linklist
 
Link list 1
Link list 1Link list 1
Link list 1
 
Heapsort 1
Heapsort 1Heapsort 1
Heapsort 1
 
Arrays
ArraysArrays
Arrays
 
Enterpise system
Enterpise systemEnterpise system
Enterpise system
 
Database administration
Database administrationDatabase administration
Database administration
 
Encoders
EncodersEncoders
Encoders
 
Universal logic gate
Universal logic gateUniversal logic gate
Universal logic gate
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Memory management
Memory managementMemory management
Memory management
 
Parallel adders
Parallel addersParallel adders
Parallel adders
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Link list 2

  • 1. CS250 - Data Structures and Algorithm BESE-2A/B
  • 2. Linked List Operations • Insert a node – At the start of the list – At the end of the list • Delete a node – At the start of the list – At the end of the list • Traverse linked list – Iterate through each item/node • Search – Find the specified item in linked list 9/17/2012 DSA - Fall 2012 - SEECS, NUST 2
  • 3. Insertion Corner Case • Empty List – With head pointer only • Head = NULL – With head and tail pointer only • Head = Tail = NULL
  • 4. Insert Item at Start 1. Empty node is created – new node 2. Initialize the value to user specified value 3. If list is empty 1. Set next pointer to null 2. Set head to new node 4. If list is not empty 1. Set the next pointer to the head 2. Set head to new node 1. Node item = new Node(); 2. Item.data = data; 3. If (head == NULL) 1. Item.next = NULL; 2. Head = item; 4. Else 1. Item.next = head; 2. Head = item;
  • 5. Insert Item at Start • When list is empty
  • 6. Insert Item at Start • When list is not empty
  • 7. Insert item at end 1. Empty node is created 2. Initialize the value to user specified value 3. When list is not empty 1. Set next pointer of new node to NULL 2. Set the next pointer of last node to the new node 4. When list is empty 1. Set the next pointer of new node to NULL 2. Set head to new node 1. Node item = new Node(); 2. Item.data = data; 3. If( head == NULL) 1. Item.next = NULL; 2. Tail.next = item; 4. Else 1. Item.next = NULL; 2. Head = Tail = item;
  • 8. Insert Item at End • When list is not empty
  • 9. Delete Item from Start 1. Set tempPtr to head 2. Set head to next of itself / tempPtr 3. Delete tempPtr 1. tempPtr = head 2. Head = head->next 3. Delete tempPtr
  • 11. Delete Item from end 1. Find the node just before the start by traversing the list 2. Delete the last node 3. Set tail to current node 4. Set next of current node to null 1. tempPtr->next = tail; 2. Delete tail 3. Tail = node 4. tempPtr->next = NULL
  • 14. Deletion Corner Cases • Empty List – Prompt user • Only one item in list – Set head and tail to NULL
  • 15. Traverse List 1. Set tempPtr to head 2. If tempPtr is not NULL – Print node data value – Set tempPtr to next of itself – Go to 2 3. Else – Exit
  • 16. Search Item in Linked List • Scan exiting list to find the specified item 1. Get the value to search 2. Set tempPtr to head 3. Check if tempPtr is NULL 4. No 1. If tempPtr data is same as user ’s data • Exit Loop with Success 2. No • Set tempPtr to tempPtr next value • Go to 3 5. Yes 1. Exit Loop
  • 17. Search Item in Linked List
  • 18. Homework Assignment • Middle of linked list – Insert an item – Delete an item