SlideShare a Scribd company logo
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

Arrays
ArraysArrays
Arrays
uos
 

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

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 (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

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 

Recently uploaded (20)

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 

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