SlideShare uma empresa Scribd logo
1 de 21
List ADT
   List is an ordered collection of items. Items in the
    collection may be integers, Strings, characters or
    any kind of objects.

   Basic operations are
       Adding an item to the collection
       Deleting an item from the collection
       Testing if list is empty
       Testing if list is full
       Getting an item on specified location
       Getting an item with specified key value
Linked List
   Collection of links with reference to the first.

   Each link has
       part to store data
       link that refers to the next link in the list.


   Data part of the link can be an integer, a character, a
    String or an object of any kind.
Linked Lists
   A linked list is a linear collection of data elements called nodes
    where linear order is given by means of pointers.
   Each node has two parts
    1) Data Field – Stores data of the node
    2) Link Field – Store address of the next node
                         ( i.e. Link to the next node)



                                          LINK



             NODE    :

                            DATA
Linked Lists
         -START is List pointer contains address of the first node in
         the List
         -   All nodes are connected to each other through Link fields
         -   Link of the last node is NULL pointer denoted by ‘X’ sign
         -   Null pointer indicated end of the list
start




                data link     datalink      datalink   Data link
                                                                  

                                                                     x




                  A              B              C            D
Algorithms


     Lets consider,

•   START is the 1st position in Linked List
•   NewNode is the new node to be created
•   DATA is the element to be inserted in new node
•   POS is the position where the new node to be inserted
•   TEMP and HOLD are temporary pointers to hold the
    node address
Algorithm to Insert a Node at the beginning


   1.      Input DATA to be inserted
   2.      Create NewNode
   3.      NewNode -> DATA = DATA
   4.      If START is equal to NULL
           a) NewNode -> LINK = NULL
   5.      Else
           a) NewNode -> LINK = START
   6.      START = NewNode
   7.      Exit
Insert a Node at the beginning



start




                    datalink    datalink   data link
                                                   

     NewNode
                                                     x
Algorithm to Insert a Node at the end
1.   Input DATA to be inserted
2.   Create NewNode
3.   NewNode -> DATA = DATA
4.   NewNode -> LINK = NULL
5.   If START is equal to NULL
                   a) START = NewNode
    6.      Else
                   a) TEMP = START
                   b) while (TEMP -> LINK not equal to
     NULL)
                          i) TEMP = TEMP -> LINK
    7.      TEMP -> Link = NewNode
    8. Exit
Insert a Node at the end

start




         data link   datalink   datalink   NewNode

                                                       X




           A           B           C           P
Algorithm to Insert a Node at any specified position

1.   Input DATA to be inserted and POS, the position to be
     inserted.
2.   Initialize TEMP = START and K=1
3.   Repeat step 3 while ( K is less than POS)
               a) TEMP = TEMP -> LINK
               b) If TEMP -> LINK = NULL
                       i) Exit
               c) K = K + 1
    4. Create a Newnode
    5. Newnode -> DATA = DATA
    6. Newnode -> LINK = TEMP -> LINK
    7. TEMP -> LINK = NewNode
    8. Exit
Insert a Node at middle position
start          Lets   consider, POS = 3


          1              2                    3          4

     data link    datalink              datalink   data link
                                                             

                                                                 x


                                                C

         A             B                                D
                                 NewNode




                                    P
Algorithm to Delete a Node

   1. Input DATA to be deleted
   2. If START is equal to DATA
                  a) TEMP = START
                  b) START = START -> LINK
                  c) Set free node TEMP - which is
    deleted
                  d) Exit
   3. HOLD = START
   4. While ((HOLD -> LINK ->LINK) not equal to NULL)
    a) If(HOLD -> LINK ->DATA) equal to DATA
                  i) TEMP = HOLD -> LINK
                  ii) HOLD -> LINK = TEMP -> LINK
                  iii) Set free node TEMP - which is
    deleted
                  iv) Exit
    b) HOLD = HOLD -> NEXT
Algorithm to Delete a Node

   5. If(HOLD -> LINK ->DATA) equal to DATA
    i) TEMP = HOLD -> LINK
    ii) Set free node TEMP - which is deleted
    iii) HOLD -> LINK = NULL
    iv) Exit
   6. Display DATA not found
   7. Exit
Algorithm to Delete a Node


                       Node  to be
start                   deleted



         data link   datalink     datalink   data link
                                                       

                                                         x




          A            B             C           D
Algorithm for Searching a Node

    Suppose START is the address of the first node in the linked list and DATA
     is the informSation to be searched.
1.   Input the DATA to be searched.
2.   Initialize TEMP = START and Pos =1
3.   Repeat the step 4,5 and 6 until (TEMP is equal to NULL)
4.   If (TEMP -> DATA is equal to DATA)
           (a) Display “The DATA found at POS “
           (b) Exit
    5. TEMP = TEMP -> LINK
    6. POS = POS + 1
    7. If (TEMP is equal to NULL)
         (a) Display “ The DATA is not found in the list”
    8. Exit.
Algorithm for Displaying all Nodes

    Suppose List is the address of the first node in the linked list.
1.   If ( START is equal to NULL)
           ( a ) Display “The List is Empty”
           ( b ) Exit
2.   Initialize TEMP = START
3.   Repeat the Step 4 and 5 until (TEMP == NULL)
4.   Display TEMP -> DATA
5.   TEMP = TEMP -> LINK
6.   Exit
Singly Linked Lists and Arrays


         Singly linked list                   Array
  Elements are stored in linear   Elements are stored in linear
  order, accessible with links.   order, accessible with an
                                  index.

  Do not have a fixed size.       Have a fixed size.

  Cannot access the previous      Can access the previous
  element directly.               element easily.
Doubly Linked List

    A doubly linked list is often more     prev           next
     convenient!
    Nodes store:
        element
        link to the previous node
                                                    elem    node
        link to the next node
    Special trailer and header nodes

header                                   nodes/positionstrailer




                                                elements
Insertion

    We visualize operation insertAfter(p, X), which returns position q
                              p


                  A            B            C

                              p


                  A            B                 q       C

                                              X
                              p            q


                  A            B            X            C
Deletion

    We visualize remove(p), where p == last()
                                                 p



                A         B         C          D



                A         B         C         p



                                                  D



                A         B         C

Mais conteúdo relacionado

Mais procurados

Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
tech4us
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 
Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1
IIUM
 
Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2
IIUM
 

Mais procurados (20)

10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Ch03
Ch03Ch03
Ch03
 
Ch06
Ch06Ch06
Ch06
 
linked list
linked listlinked list
linked list
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
LR Parsing
LR ParsingLR Parsing
LR Parsing
 
Ch04
Ch04Ch04
Ch04
 
Lec3
Lec3Lec3
Lec3
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
 
Lisp tutorial
Lisp tutorialLisp tutorial
Lisp tutorial
 
Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2
 

Semelhante a U2.linked list

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
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Data structures linked list introduction.pptx
Data structures linked list introduction.pptxData structures linked list introduction.pptx
Data structures linked list introduction.pptx
Kalpana Mohan
 

Semelhante a U2.linked list (20)

Singly link list
Singly link listSingly link list
Singly link list
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
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
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Stack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanmsStack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanms
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
Data structures linked list introduction.pptx
Data structures linked list introduction.pptxData structures linked list introduction.pptx
Data structures linked list introduction.pptx
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
03-Lists.ppt
03-Lists.ppt03-Lists.ppt
03-Lists.ppt
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
Data structure
Data structureData structure
Data structure
 

Mais de Ssankett Negi (10)

Binary tree
Binary treeBinary tree
Binary tree
 
Multi way&btree
Multi way&btreeMulti way&btree
Multi way&btree
 
Heapsort
HeapsortHeapsort
Heapsort
 
Binary trees
Binary treesBinary trees
Binary trees
 
Threaded binarytree&heapsort
Threaded binarytree&heapsortThreaded binarytree&heapsort
Threaded binarytree&heapsort
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
Recursion
RecursionRecursion
Recursion
 
Qprgs
QprgsQprgs
Qprgs
 
Circular queues
Circular queuesCircular queues
Circular queues
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 

U2.linked list

  • 1.
  • 2. List ADT  List is an ordered collection of items. Items in the collection may be integers, Strings, characters or any kind of objects.  Basic operations are  Adding an item to the collection  Deleting an item from the collection  Testing if list is empty  Testing if list is full  Getting an item on specified location  Getting an item with specified key value
  • 3. Linked List  Collection of links with reference to the first.   Each link has  part to store data  link that refers to the next link in the list.  Data part of the link can be an integer, a character, a String or an object of any kind.
  • 4. Linked Lists  A linked list is a linear collection of data elements called nodes where linear order is given by means of pointers.  Each node has two parts 1) Data Field – Stores data of the node 2) Link Field – Store address of the next node ( i.e. Link to the next node) LINK NODE : DATA
  • 5. Linked Lists -START is List pointer contains address of the first node in the List - All nodes are connected to each other through Link fields - Link of the last node is NULL pointer denoted by ‘X’ sign - Null pointer indicated end of the list start data link datalink datalink Data link  x A B C D
  • 6. Algorithms  Lets consider, • START is the 1st position in Linked List • NewNode is the new node to be created • DATA is the element to be inserted in new node • POS is the position where the new node to be inserted • TEMP and HOLD are temporary pointers to hold the node address
  • 7. Algorithm to Insert a Node at the beginning  1. Input DATA to be inserted  2. Create NewNode  3. NewNode -> DATA = DATA  4. If START is equal to NULL  a) NewNode -> LINK = NULL  5. Else  a) NewNode -> LINK = START  6. START = NewNode  7. Exit
  • 8. Insert a Node at the beginning start datalink datalink data link  NewNode x
  • 9. Algorithm to Insert a Node at the end 1. Input DATA to be inserted 2. Create NewNode 3. NewNode -> DATA = DATA 4. NewNode -> LINK = NULL 5. If START is equal to NULL  a) START = NewNode  6. Else  a) TEMP = START  b) while (TEMP -> LINK not equal to NULL)  i) TEMP = TEMP -> LINK  7. TEMP -> Link = NewNode  8. Exit
  • 10. Insert a Node at the end start data link datalink datalink NewNode X A B C P
  • 11. Algorithm to Insert a Node at any specified position 1. Input DATA to be inserted and POS, the position to be inserted. 2. Initialize TEMP = START and K=1 3. Repeat step 3 while ( K is less than POS)  a) TEMP = TEMP -> LINK  b) If TEMP -> LINK = NULL  i) Exit  c) K = K + 1  4. Create a Newnode  5. Newnode -> DATA = DATA  6. Newnode -> LINK = TEMP -> LINK  7. TEMP -> LINK = NewNode  8. Exit
  • 12. Insert a Node at middle position start Lets consider, POS = 3 1 2 3 4 data link datalink datalink data link  x C A B D NewNode P
  • 13. Algorithm to Delete a Node  1. Input DATA to be deleted  2. If START is equal to DATA  a) TEMP = START  b) START = START -> LINK  c) Set free node TEMP - which is deleted  d) Exit  3. HOLD = START  4. While ((HOLD -> LINK ->LINK) not equal to NULL)  a) If(HOLD -> LINK ->DATA) equal to DATA  i) TEMP = HOLD -> LINK  ii) HOLD -> LINK = TEMP -> LINK  iii) Set free node TEMP - which is deleted  iv) Exit  b) HOLD = HOLD -> NEXT
  • 14. Algorithm to Delete a Node  5. If(HOLD -> LINK ->DATA) equal to DATA  i) TEMP = HOLD -> LINK  ii) Set free node TEMP - which is deleted  iii) HOLD -> LINK = NULL  iv) Exit  6. Display DATA not found  7. Exit
  • 15. Algorithm to Delete a Node Node to be start deleted data link datalink datalink data link  x A B C D
  • 16. Algorithm for Searching a Node  Suppose START is the address of the first node in the linked list and DATA is the informSation to be searched. 1. Input the DATA to be searched. 2. Initialize TEMP = START and Pos =1 3. Repeat the step 4,5 and 6 until (TEMP is equal to NULL) 4. If (TEMP -> DATA is equal to DATA)  (a) Display “The DATA found at POS “  (b) Exit  5. TEMP = TEMP -> LINK  6. POS = POS + 1  7. If (TEMP is equal to NULL)  (a) Display “ The DATA is not found in the list”  8. Exit.
  • 17. Algorithm for Displaying all Nodes  Suppose List is the address of the first node in the linked list. 1. If ( START is equal to NULL)  ( a ) Display “The List is Empty”  ( b ) Exit 2. Initialize TEMP = START 3. Repeat the Step 4 and 5 until (TEMP == NULL) 4. Display TEMP -> DATA 5. TEMP = TEMP -> LINK 6. Exit
  • 18. Singly Linked Lists and Arrays Singly linked list Array Elements are stored in linear Elements are stored in linear order, accessible with links. order, accessible with an index. Do not have a fixed size. Have a fixed size. Cannot access the previous Can access the previous element directly. element easily.
  • 19. Doubly Linked List  A doubly linked list is often more prev next convenient!  Nodes store:  element  link to the previous node elem node  link to the next node  Special trailer and header nodes header nodes/positionstrailer elements
  • 20. Insertion  We visualize operation insertAfter(p, X), which returns position q p A B C p A B q C X p q A B X C
  • 21. Deletion  We visualize remove(p), where p == last() p A B C D A B C p D A B C