SlideShare a Scribd company logo
Linked List outline

•   Why linked lists
•   Linked lists
•   Representation of Linked lists in memory
•   Traversing a linked lists
•   Memory allocation: Garbage collection
•   Overflow and Underflow
•   Basic operations of linked lists
     – Insert, find, delete, print, etc.
•   Variations of linked lists
     – Circular linked lists
     – Doubly linked lists
Lecture – 5
     on
Data Structure

  Linked Lists
Why linked lists

•   Disadvantages of arrays as storage data structures:
     – Slow insertion in ordered array
     – Contiguous Size is required
     – Inefficient usage of Memory
•   Linked lists solve some of these problems
•   Linked lists are general purpose storage data structures.
Linked lists
   •A linked list, or one way list, is a linear collection of data elements, called
   nodes, where the linear order is given by means of pointers.
   •Each node is divided into two parts:
   •The first part contains the information of the element, and
   •The second part, called the link field or next pointer field, contains the
   address of the next node in the list.
   •The pointer of the last node contains a special value,called the null pointer.
   •A pointer variable – called START which contains the address of the first
   node.
   •A special case is the list that has no nodes, such a list is called the null list
   or empty list and is denoted by the null pointer in the variable START.

Start
           node                      node                      node
            Data Next                 Data Next                 Data

                           Linked list with 3 nodes
                                                               node
                                                                          A

                                                                         data   pointer
linked lists

•A linked list organizes a collection of data items (elements ) such that elements
can easily be added to and deleted from any position in the list.
•Only references To next elements are updated in insertion and deletion
operations.
•There is no need to copy or move large blocks of data to facilitate insertion and
deletion of elements.
•Lists grow dynamically.
Representation of Linked lists in memory

            INFO   LINK
        1
START                     START=3, INFO[3]=45
        2   67     5      LINK[3]=2, INFO[2]=67
 3
        3   45     2      LINK[2]=5, INFO[5]=75

            80     7      LINK[5]=4, INFO[4]=80
        4
                          LINK[4]=7, INFO[7]=90
        5   75     4
                          LINK[7]=0, NULL value, So the list has ended
        6

        7   90     0

        8
Traversing a linked lists
LIST be a linked list in memory stored in linear arrays INFO and LINK with START
pointing to the first element and NULL indicating the end of LIST.
We want to traverse LIST in order to process each node exactly once.
Pointer variable PTR points to the node that is currently being processed.
LINK[PTR] points to the next node to be processed.
Thus update PTR by the assignment
                  PTR : =LINK[PTR]
                                                 PTR



                                    START               INFO LINK
                                                                             X


                                             Fig : PTR : = LINK[PTR]
Traversing a linked lists
For printing the information at each node of a linked list, must traverse the list.
Algorithm 5.1 : PRINT(INFO, LINK, START)
             Algorithm Prints the information at each node of the list.


              1. Set PTR : =START.
              2. Repeat steps 3 and 4 while PTR : ≠ NULL:
              3. Write : INFO[PTR].
              4. Set PTR : =LINK[PTR].
              5. Exit.
Traversing a linked lists
For Finding the number NUM of elements in a linked list, must traverse the list.
Algorithm 5.1 : COUNT(INFO, LINK, START, NUM)


             1. Set NUM: =0.
             2. . Set PTR : =START.
             3. Repeat steps 4 and 5 while PTR : ≠ NULL:
             4. Set NUM : =NUM+1.
             5. Set PTR : =LINK[PTR].
             6. Exit.
Memory allocation: Garbage collection
•   Memory space can be reused if a node is deleted from a list
     – i.e deleted node can be made available for future use
•   The operating system of a computer may periodically collect all the deleted space
    on to the free storage list. Any technique which does this collection called garbage
    collection




                                                                          Periodic
                                                                          Collector
       Computer
       programs                       Garbage                                      Sent to
                                  (Deleted Space)
                                                                                   avail list

          Takes
          space                                            …
          avail list                       Avail List (Free space)
Overflow and Underflow
•   Overflow:
     – Sometimes data are inserted into a data structure but there is no
       available space.
     – This situation is called overflow
     – Example: In linked list overflow occurs when
         • AVAIL= NULL and
         • There is an insertion operation
•   Underflow:
     – Situation:
         • Want to delete data from data structure that is empty.
     – Example: In linked list overflow occurs when
         • START = NULL and
         • There is an deletion operation
Insertion into a linked list
•    Node N is to be inserted in to the list between nodes A and B
•    Three pointer fields are changed as follows:
1.   The next pointer field of node A now points to the new node N, to which AVAIL previously
     pointed.
2.   AVAIL now point to the second node in the free pool, to which node N previously pointed.
3.   The next pointer field of node N now points to node B, to which node A previously pointed.
        START

                                           Node          Node
                                           A             B
      START
                                 (a) Before insertion
                                         Node               Node
                                         A                  B


     AVAIL                (a) After insertion
                   Node N                         Node N




                                                                 Free storage list
Inserting a new node

•   Possible cases of Insert Node

    1.   Insert into an empty list
    2.   Insert in front
    3.   Insert at back
    4.   Insert in middle

•   But, in fact, only need to handle two cases:

    1.   Insert as the first node (Case 1 and Case 2)
    2.   Insert in the middle or at the end of the list (Case 3 and Case 4)
Inserting a new node



INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)

1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit

2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]

3. Set INFO[NEW]= ITEM
                                                        Check for available
                                                            memory
4. IF LOC = NULL then [Insert as first Node]
   Set LINK[NEW]= START and START=NEW.

 Else: [Insert after node with location LOC]
   Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW

5. Exit.
Inserting a new node



INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)

1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit

2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]

3. Set INFO[NEW]= ITEM

4. IF LOC = NULL then [Insert as first Node]
   Set LINK[]= START and START=NEW.
                                                  Create a new node
  Else: [Insert after node with location LOC]
   Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW

5. Exit.
Inserting a new node



INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)

1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit

2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]

3. Set INFO[NEW]= ITEM
                                                         Insert as first element
4. IF LOC = NULL then [Insert as first Node]
        Set LINK[NEW]= START and START=NEW.                        START

   Else: [Insert after node with location LOC]
          Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW

5. Exit.                                                               NEW
Inserting a new node


INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)

1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit

2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]

3. Set INFO[NEW]= ITEM

4. IF LOC = NULL then [Insert as first Node]
        Set LINK[NEW]= START and START=NEW.    Insert after currNode
                                                        LOC
  Else: [Insert after node with location LOC]
       Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW

5. Exit.

                                                              NEW

More Related Content

PPTX
Doubly Linked List
PPTX
Priority Queue in Data Structure
PPTX
linked list in data structure
PPTX
linked list in Data Structure, Simple and Easy Tutorial
PPT
Data Structures with C Linked List
PPTX
Array operations
PDF
Searching and Sorting Techniques in Data Structure
PPTX
Priority queue in DSA
Doubly Linked List
Priority Queue in Data Structure
linked list in data structure
linked list in Data Structure, Simple and Easy Tutorial
Data Structures with C Linked List
Array operations
Searching and Sorting Techniques in Data Structure
Priority queue in DSA

What's hot (20)

PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PPTX
Linked List
PPTX
Stack organization
PPTX
single linked list
PPTX
Heap Sort in Design and Analysis of algorithms
PPTX
Presentation on queue
PPTX
Linked List - Insertion & Deletion
PPTX
Sorting algorithms
PPT
Data Structure and Algorithms Arrays
PPTX
PDF
Array data structure
PPTX
Infix to postfix conversion
PPTX
PDF
Linked list implementation of Queue
PPTX
Selection sorting
PPTX
Stacks IN DATA STRUCTURES
PPTX
Circular Queue data structure
PPTX
PPTX
Transaction Processing in DBMS.pptx
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Linked List
Stack organization
single linked list
Heap Sort in Design and Analysis of algorithms
Presentation on queue
Linked List - Insertion & Deletion
Sorting algorithms
Data Structure and Algorithms Arrays
Array data structure
Infix to postfix conversion
Linked list implementation of Queue
Selection sorting
Stacks IN DATA STRUCTURES
Circular Queue data structure
Transaction Processing in DBMS.pptx
Ad

Similar to Data structure lecture 5 (20)

PPT
linked list1.ppt linked list ppts and notes
PDF
Bca data structures linked list mrs.sowmya jyothi
PPTX
5.Linked list
PPT
linked list2.ppt linked list part 2 ppt
PPTX
DSModule2.pptx
PPTX
linked list in dsa python (presentation)
PPTX
Stack and queue power point presentation data structure and algorithms Stack-...
PPT
Unit 2 linked list and queues
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
PPTX
RPT_03_A_Linked List presentation for FE
PPTX
Linked List Representation of a Linked List.pptx
PPTX
Linked list
PDF
unit 2- linked list- PPT for btech students.pdf
PDF
unit 2- PPT.pdf
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
PPTX
Unit 5 linked list
PPT
Unit ii(dsc++)
PPTX
Dounly linked list
PDF
Data Structure Lecture 3 Linked Lists.pdf
PPTX
Linked List Presentation in data structurepptx
linked list1.ppt linked list ppts and notes
Bca data structures linked list mrs.sowmya jyothi
5.Linked list
linked list2.ppt linked list part 2 ppt
DSModule2.pptx
linked list in dsa python (presentation)
Stack and queue power point presentation data structure and algorithms Stack-...
Unit 2 linked list and queues
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
RPT_03_A_Linked List presentation for FE
Linked List Representation of a Linked List.pptx
Linked list
unit 2- linked list- PPT for btech students.pdf
unit 2- PPT.pdf
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
Unit 5 linked list
Unit ii(dsc++)
Dounly linked list
Data Structure Lecture 3 Linked Lists.pdf
Linked List Presentation in data structurepptx
Ad

More from Kumar (20)

PPT
Graphics devices
PPT
Fill area algorithms
PDF
region-filling
PDF
Bresenham derivation
PPT
Bresenham circles and polygons derication
PPTX
Introductionto xslt
PPTX
Extracting data from xml
PPTX
Xml basics
PPTX
XML Schema
PPTX
Publishing xml
PPTX
DTD
PPTX
Applying xml
PPTX
Introduction to XML
PDF
How to deploy a j2ee application
PDF
JNDI, JMS, JPA, XML
PDF
EJB Fundmentals
PDF
JSP and struts programming
PDF
java servlet and servlet programming
PDF
Introduction to JDBC and JDBC Drivers
PDF
Introduction to J2EE
Graphics devices
Fill area algorithms
region-filling
Bresenham derivation
Bresenham circles and polygons derication
Introductionto xslt
Extracting data from xml
Xml basics
XML Schema
Publishing xml
DTD
Applying xml
Introduction to XML
How to deploy a j2ee application
JNDI, JMS, JPA, XML
EJB Fundmentals
JSP and struts programming
java servlet and servlet programming
Introduction to JDBC and JDBC Drivers
Introduction to J2EE

Recently uploaded (20)

PDF
DevOps & Developer Experience Summer BBQ
PDF
Why Endpoint Security Is Critical in a Remote Work Era?
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PDF
creating-agentic-ai-solutions-leveraging-aws.pdf
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
PDF
This slide provides an overview Technology
PDF
REPORT: Heating appliances market in Poland 2024
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Software Development Methodologies in 2025
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
DevOps & Developer Experience Summer BBQ
Why Endpoint Security Is Critical in a Remote Work Era?
Enable Enterprise-Ready Security on IBM i Systems.pdf
creating-agentic-ai-solutions-leveraging-aws.pdf
A Day in the Life of Location Data - Turning Where into How.pdf
This slide provides an overview Technology
REPORT: Heating appliances market in Poland 2024
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
CroxyProxy Instagram Access id login.pptx
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
madgavkar20181017ppt McKinsey Presentation.pdf
Software Development Methodologies in 2025
Smarter Business Operations Powered by IoT Remote Monitoring
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Understanding_Digital_Forensics_Presentation.pptx
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
Top Generative AI Tools for Patent Drafting in 2025.pdf
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx

Data structure lecture 5

  • 1. Linked List outline • Why linked lists • Linked lists • Representation of Linked lists in memory • Traversing a linked lists • Memory allocation: Garbage collection • Overflow and Underflow • Basic operations of linked lists – Insert, find, delete, print, etc. • Variations of linked lists – Circular linked lists – Doubly linked lists
  • 2. Lecture – 5 on Data Structure Linked Lists
  • 3. Why linked lists • Disadvantages of arrays as storage data structures: – Slow insertion in ordered array – Contiguous Size is required – Inefficient usage of Memory • Linked lists solve some of these problems • Linked lists are general purpose storage data structures.
  • 4. Linked lists •A linked list, or one way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. •Each node is divided into two parts: •The first part contains the information of the element, and •The second part, called the link field or next pointer field, contains the address of the next node in the list. •The pointer of the last node contains a special value,called the null pointer. •A pointer variable – called START which contains the address of the first node. •A special case is the list that has no nodes, such a list is called the null list or empty list and is denoted by the null pointer in the variable START. Start node node node Data Next Data Next Data Linked list with 3 nodes node A data pointer
  • 5. linked lists •A linked list organizes a collection of data items (elements ) such that elements can easily be added to and deleted from any position in the list. •Only references To next elements are updated in insertion and deletion operations. •There is no need to copy or move large blocks of data to facilitate insertion and deletion of elements. •Lists grow dynamically.
  • 6. Representation of Linked lists in memory INFO LINK 1 START START=3, INFO[3]=45 2 67 5 LINK[3]=2, INFO[2]=67 3 3 45 2 LINK[2]=5, INFO[5]=75 80 7 LINK[5]=4, INFO[4]=80 4 LINK[4]=7, INFO[7]=90 5 75 4 LINK[7]=0, NULL value, So the list has ended 6 7 90 0 8
  • 7. Traversing a linked lists LIST be a linked list in memory stored in linear arrays INFO and LINK with START pointing to the first element and NULL indicating the end of LIST. We want to traverse LIST in order to process each node exactly once. Pointer variable PTR points to the node that is currently being processed. LINK[PTR] points to the next node to be processed. Thus update PTR by the assignment PTR : =LINK[PTR] PTR START INFO LINK X Fig : PTR : = LINK[PTR]
  • 8. Traversing a linked lists For printing the information at each node of a linked list, must traverse the list. Algorithm 5.1 : PRINT(INFO, LINK, START) Algorithm Prints the information at each node of the list. 1. Set PTR : =START. 2. Repeat steps 3 and 4 while PTR : ≠ NULL: 3. Write : INFO[PTR]. 4. Set PTR : =LINK[PTR]. 5. Exit.
  • 9. Traversing a linked lists For Finding the number NUM of elements in a linked list, must traverse the list. Algorithm 5.1 : COUNT(INFO, LINK, START, NUM) 1. Set NUM: =0. 2. . Set PTR : =START. 3. Repeat steps 4 and 5 while PTR : ≠ NULL: 4. Set NUM : =NUM+1. 5. Set PTR : =LINK[PTR]. 6. Exit.
  • 10. Memory allocation: Garbage collection • Memory space can be reused if a node is deleted from a list – i.e deleted node can be made available for future use • The operating system of a computer may periodically collect all the deleted space on to the free storage list. Any technique which does this collection called garbage collection Periodic Collector Computer programs Garbage Sent to (Deleted Space) avail list Takes space … avail list Avail List (Free space)
  • 11. Overflow and Underflow • Overflow: – Sometimes data are inserted into a data structure but there is no available space. – This situation is called overflow – Example: In linked list overflow occurs when • AVAIL= NULL and • There is an insertion operation • Underflow: – Situation: • Want to delete data from data structure that is empty. – Example: In linked list overflow occurs when • START = NULL and • There is an deletion operation
  • 12. Insertion into a linked list • Node N is to be inserted in to the list between nodes A and B • Three pointer fields are changed as follows: 1. The next pointer field of node A now points to the new node N, to which AVAIL previously pointed. 2. AVAIL now point to the second node in the free pool, to which node N previously pointed. 3. The next pointer field of node N now points to node B, to which node A previously pointed. START Node Node A B START (a) Before insertion Node Node A B AVAIL (a) After insertion Node N Node N Free storage list
  • 13. Inserting a new node • Possible cases of Insert Node 1. Insert into an empty list 2. Insert in front 3. Insert at back 4. Insert in middle • But, in fact, only need to handle two cases: 1. Insert as the first node (Case 1 and Case 2) 2. Insert in the middle or at the end of the list (Case 3 and Case 4)
  • 14. Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) 1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit 2. Set NEW= AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]= ITEM Check for available memory 4. IF LOC = NULL then [Insert as first Node] Set LINK[NEW]= START and START=NEW. Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW 5. Exit.
  • 15. Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) 1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit 2. Set NEW= AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]= ITEM 4. IF LOC = NULL then [Insert as first Node] Set LINK[]= START and START=NEW. Create a new node Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW 5. Exit.
  • 16. Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) 1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit 2. Set NEW= AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]= ITEM Insert as first element 4. IF LOC = NULL then [Insert as first Node] Set LINK[NEW]= START and START=NEW. START Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW 5. Exit. NEW
  • 17. Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) 1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit 2. Set NEW= AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]= ITEM 4. IF LOC = NULL then [Insert as first Node] Set LINK[NEW]= START and START=NEW. Insert after currNode LOC Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW 5. Exit. NEW