SlideShare a Scribd company logo
U2.linked list
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

More Related Content

PPSX
Data Structure (Double Linked List)
PDF
Linked List Static and Dynamic Memory Allocation
PPS
Single linked list
PPT
PPTX
Linked list
PPT
Linked list
PPT
Ch17
PPTX
Linked list
Data Structure (Double Linked List)
Linked List Static and Dynamic Memory Allocation
Single linked list
Linked list
Linked list
Ch17
Linked list

What's hot (20)

PPT
10 Linked Lists Sacks and Queues
PDF
stacks and queues
PPTX
Lecture 6: linked list
PDF
Ch03
PDF
Ch06
PDF
linked list
PPT
Rana Junaid Rasheed
PPT
Stacks & Queues
PPT
Data structure lecture7
PPT
DATA STRUCTURES
PPT
Stacks and queues
PPT
Csc1100 lecture07 ch07_pt1-1
PPTX
LISP: Introduction to lisp
PPT
Heaps & priority queues
PDF
LR Parsing
PDF
Ch04
PDF
Python lecture 07
PDF
Lisp tutorial
PPT
Csc1100 lecture12 ch08_pt2
10 Linked Lists Sacks and Queues
stacks and queues
Lecture 6: linked list
Ch03
Ch06
linked list
Rana Junaid Rasheed
Stacks & Queues
Data structure lecture7
DATA STRUCTURES
Stacks and queues
Csc1100 lecture07 ch07_pt1-1
LISP: Introduction to lisp
Heaps & priority queues
LR Parsing
Ch04
Python lecture 07
Lisp tutorial
Csc1100 lecture12 ch08_pt2
Ad

Similar to U2.linked list (20)

PPT
Singly link list
PPT
lecture four of data structures :Linked List-ds.ppt
PPTX
Linked lists a
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
PPTX
Revisiting a data structures in detail with linked list stack and queue
PPTX
Doubly & Circular Linked Lists
PPTX
singlelinkedlistasdfghzxcvbnmqwertyuiopa
PPTX
DSModule2.pptx
PPTX
linkedlist-130914084342-phpapp02.pptx
PPTX
ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE
PPTX
Linked List.pptx
PPTX
Stack - PPT Slides.pptx-data sturutures and algorithanms
PPTX
LEC_4,5_linked_list.pptx for single and double linked list
PPTX
LEC_4,5_linked_list.pptx this is Good for data structure
PPT
Abstract data types
PPT
DS Unit 2.ppt
PPT
Algo>ADT list & linked list
PPTX
Linear data structure concepts
PPSX
Data Structure (Dynamic Array and Linked List)
PPTX
Data structures linked list introduction.pptx
Singly link list
lecture four of data structures :Linked List-ds.ppt
Linked lists a
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
Revisiting a data structures in detail with linked list stack and queue
Doubly & Circular Linked Lists
singlelinkedlistasdfghzxcvbnmqwertyuiopa
DSModule2.pptx
linkedlist-130914084342-phpapp02.pptx
ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE
Linked List.pptx
Stack - PPT Slides.pptx-data sturutures and algorithanms
LEC_4,5_linked_list.pptx for single and double linked list
LEC_4,5_linked_list.pptx this is Good for data structure
Abstract data types
DS Unit 2.ppt
Algo>ADT list & linked list
Linear data structure concepts
Data Structure (Dynamic Array and Linked List)
Data structures linked list introduction.pptx
Ad

More from Ssankett Negi (10)

PDF
Binary tree
PDF
Multi way&btree
PPTX
Heapsort
PDF
Binary trees
PDF
Threaded binarytree&heapsort
PPTX
U3.stack queue
DOCX
Stack prgs
PPTX
Recursion
DOCX
PPT
Circular queues
Binary tree
Multi way&btree
Heapsort
Binary trees
Threaded binarytree&heapsort
U3.stack queue
Stack prgs
Recursion
Circular queues

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Empathic Computing: Creating Shared Understanding
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Getting Started with Data Integration: FME Form 101
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPT
Teaching material agriculture food technology
PDF
Electronic commerce courselecture one. Pdf
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Big Data Technologies - Introduction.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Machine Learning_overview_presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Programs and apps: productivity, graphics, security and other tools
Empathic Computing: Creating Shared Understanding
SOPHOS-XG Firewall Administrator PPT.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Getting Started with Data Integration: FME Form 101
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Teaching material agriculture food technology
Electronic commerce courselecture one. Pdf
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
NewMind AI Weekly Chronicles - August'25-Week II
Big Data Technologies - Introduction.pptx
Machine learning based COVID-19 study performance prediction
Spectral efficient network and resource selection model in 5G networks
Building Integrated photovoltaic BIPV_UPV.pdf
Network Security Unit 5.pdf for BCA BBA.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine Learning_overview_presentation.pptx

U2.linked list

  • 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