SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and QueuesPraveen M Jigajinni
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked listVivek Bhargav
 
linked list
linked listlinked list
linked listAbbott
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queuestech4us
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1IIUM
 
Lisp tutorial
Lisp tutorialLisp tutorial
Lisp tutorialNilt1234
 
Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2IIUM
 

Was ist angesagt? (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
 

Ähnlich wie 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 queuessuser7319f8
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
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 algorithanmsAdithaBuwaneka
 
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)Adam Mukharil Bachtiar
 
Data structures linked list introduction.pptx
Data structures linked list introduction.pptxData structures linked list introduction.pptx
Data structures linked list introduction.pptxKalpana Mohan
 
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.pptshashankbhadouria4
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked listSumathi Kv
 
Data structure
Data structureData structure
Data structureNida Ahmed
 

Ähnlich wie 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
 

Mehr von 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
 

Kürzlich hochgeladen

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
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 WorkerThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
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 RobisonAnna Loughnan Colquhoun
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Kürzlich hochgeladen (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI 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