SlideShare a Scribd company logo
Linked Lists

            An Alternative Collections Data
            Structure




       What Is A Linked List?

          A data structure using memory that
          expands and shrinks as needed
          Relies on identical nodes “pointing” or
          “linked” to other nodes
          Each node is an instance of a class (i.e. an
          object)




     Head




       Example Declarations
public class IntNode
{
  private int data;
  private IntNode link;
  …
}
                          OR
public class Part
{
  private int partNo;
  private double price;
  private Part link;
  …
}
Special Nodes
Head node
  The first node in a linked list
  Almost all programs using linked lists will
  maintain a pointer to the head node
  An empty list is represented by storing Null in the
  head variable
Tail node
  The last node in a linked list
  Many programs maintain a pointer to the tail
  node
  A tail node points to a “null” reference
  In an empty list tail will also be Null




   Necessary Methods

 Methods to access and modify data:
 e.g. getData(), getLink(), setData(int
   newData), setLink(??? newLink)
 Methods for creating nodes
 i.e. constructors
 Methods for adding and removing
 nodes




   Caution!

   Always make sure that your linked list
   methods work correctly with an empty
   list.
Adding a Node to the Front
    of a List
     1. Create a new node with the desired data, and
        the link pointing to the current head node
     2. Make the “head” pointer point to the new node
     What happens if we started with an empty list?




remove

   Head




     Removing the First Node
     of a List
        Make the head pointer point to what the
        current head node is pointing to
     What happens to the original head node?
     What if there was only one node in the list?



remove

   Head




    Adding a Node After a
    Selected Node
     1. Create a new node pointing to whatever the
        selected node is currently pointing to
     2. Make the selected node point to the new node
     What happens if the selected node was the last node
        in the list?


                                 remove



 Head           Selected
Removing a Selected Node



      Head


                       Selected
                  Can we remove this?




     Removing the Node After
        a Selected Node

     Head
              Selected
                          Can we remove this?
Desired Result:




      Head
              Selected

What happens if we removed the tail node?




       Manipulating Entire Linked
       Lists
         Traverse the entire list in a loop that
         terminates when the null pointer is
         reached
         In the body of the loop perform the
         necessary operation(s)
Manipulating Entire Linked
       Lists
           Examples:
               count the elements of the list
               Print the elements of the list
               Update the values in the list (e.g. increase
               all prices by 10%)
           If implemented in the same class as the
           node (i.e. no separate list class), it is better
           to use static methods, this way the method
           can be used for empty lists




       Sample Code
IntNode current;
for (current = head; current != null; current = current.link)
{
    …
}
                            Or:
IntNode current;
current = head;
while (current != null)
{
    …
    current = current.link;
}




       Copying an Entire List

public static IntNode copyList (IntNode head)
{
   if (head == null) return null;
   IntNode copyHead = new IntNode (head.data,null);
   IntNode copyTail = copyHead;
   IntNode source = head;
   while (source.link != null)
   {
          source = source.link;
          copyTail.addNodeAfter(source.data);
          copyTail = copyTail.link;
   }
   return copyHead;
}
Copying a Part of a List

      Given a start node and an end node
      make a copy of that part of the list
      Can we return both the head and tail
      of the copy?
      How does the method work? Is it that
      much different from copying the full
      list?




  Linked List Application –
  IntLinkedBag

      Keeps track of integer elements
      Can add or delete elements
      Answers questions about its elements
      Can add all the contents of another
      bag
      Grows and shrinks in size as needed




   IntLinkedBag - Methods
public void add (int element)
public void addAll (IntLinkedBag addend)
public int countOccurences(int target)
public boolean remove (int target)
public int size()
public void trimToSize()
public static IntLinkedBag union (IntLinkedBag b1,
                                       IntLinkedBag b2)
public int grab()
public void ensureCapacity (int minimumCapacity) ???
public int getCapacity() ???
Another Linked List Application
 - DoubleLinkedSeq
Similar to bag but sequence is preserved
An additional instance variable is needed to hold
the position of the “current” element
Two additional instance variables “tail” and
“precursor” may be used for convenience
Some methods can be preserved, others must be
modified
Additional methods are needed
Implementation: assignment 5




  Doubly Linked Lists

     Similar to linked lists with pointers in
     both directions

Head                                          Tail




 Array vs. Linked Lists vs.
 Doubly Linked Lists
  Which is better for the following
  operations?:
       Random access
       Frequent additions/removals in the
       middle of a sequence
       Frequent forward and backward
       searching of a sequence
       Frequent size changes

More Related Content

PPT
List data structure
PDF
Java Generics wildcards
PDF
computer notes - Linked list
PDF
Linked_lists2
PPTX
linked list in Data Structure, Simple and Easy Tutorial
ODP
Linked List
PDF
8 python data structure-1
List data structure
Java Generics wildcards
computer notes - Linked list
Linked_lists2
linked list in Data Structure, Simple and Easy Tutorial
Linked List
8 python data structure-1

What's hot (20)

PPT
Chapter 14
PDF
linked_lists
PPTX
ArrayList in JAVA
PPTX
Data structure
PPT
Ap Power Point Chpt9
PDF
LPR - Week 1
PDF
Python set
PDF
computer notes - List implementation
PPT
Generics Collections
PPTX
Python Dictionary.pptx
PDF
linked_lists4
PPTX
Lists
PPTX
Beautiful Research Data (Structured Data and Open Refine)
PPTX
Pa1 session 2
PDF
Linked lists
PPTX
The awesome algorithm
PPTX
List view1
PDF
LectureNotes-03-DSA
PPTX
3 searching algorithms in Java
PDF
linked_lists3
Chapter 14
linked_lists
ArrayList in JAVA
Data structure
Ap Power Point Chpt9
LPR - Week 1
Python set
computer notes - List implementation
Generics Collections
Python Dictionary.pptx
linked_lists4
Lists
Beautiful Research Data (Structured Data and Open Refine)
Pa1 session 2
Linked lists
The awesome algorithm
List view1
LectureNotes-03-DSA
3 searching algorithms in Java
linked_lists3
Ad

Viewers also liked (18)

PPT
Nawawalang Paraiso
PDF
Глава 9. История развития компании «Евросеть»
PPT
Light Emitting Diode
PDF
Img025
PPT
Light Emitting Diode9(LED)
PDF
Глава 7. История развития компании «КОМСТАР-ОТС»
PDF
Глава 9. История развития компании «Евросеть»
PPTX
7th Grade Gifted Language Arts
PDF
Ianbresume
PPT
Light Emitting Diode9(LED)
PPT
Light Emitting Diode9(LED)
PPT
Light Emitting Diode9(LED)
PPT
Light Emitting Diode9(LED)
PDF
Глава 1. Революция услуг в мировой экономике
PDF
Глава 6. История развития компании «МегаФон» [обновление - март 2012]
PDF
Глава 6. История развития компании «МегаФон»
PPT
Light Emitting Diode
PPT
light emmitting diode
Nawawalang Paraiso
Глава 9. История развития компании «Евросеть»
Light Emitting Diode
Img025
Light Emitting Diode9(LED)
Глава 7. История развития компании «КОМСТАР-ОТС»
Глава 9. История развития компании «Евросеть»
7th Grade Gifted Language Arts
Ianbresume
Light Emitting Diode9(LED)
Light Emitting Diode9(LED)
Light Emitting Diode9(LED)
Light Emitting Diode9(LED)
Глава 1. Революция услуг в мировой экономике
Глава 6. История развития компании «МегаФон» [обновление - март 2012]
Глава 6. История развития компании «МегаФон»
Light Emitting Diode
light emmitting diode
Ad

Similar to linklisr (20)

PPT
Fundamentals of data structures
PPTX
Linked lists
PPT
linked_lists.ppt linked_lists linked_lists
PPT
LINKEDb2bb22bb3b3b3b3n3_LIST_UKL_1-2.ppt
PPT
Lecture 3 List of Data Structures & Algorithms
PPTX
Linked list
PPT
Algo>ADT list & linked list
PPT
List Data Structure
PDF
LinkedList1LinkedList1LinkedList1111.pdf
PPTX
Data Structures Introduction & Linear DS
PPTX
csc211_lecture_21.pptx
PPTX
Lecture ............ 3 - Linked Lists.pptx
PPTX
Datastucture-Unit 4-Linked List Presentation.pptx
PDF
Data structure
PPTX
Linked List Representation of a Linked List.pptx
PPT
Data Structure Lecture 5
PPT
Link list using array in Data structure amd algorithms
PPT
Funddamentals of data structures
PPTX
Data Structures Algorithms and Applications
PPT
Fundamentals of data structures
Linked lists
linked_lists.ppt linked_lists linked_lists
LINKEDb2bb22bb3b3b3b3n3_LIST_UKL_1-2.ppt
Lecture 3 List of Data Structures & Algorithms
Linked list
Algo>ADT list & linked list
List Data Structure
LinkedList1LinkedList1LinkedList1111.pdf
Data Structures Introduction & Linear DS
csc211_lecture_21.pptx
Lecture ............ 3 - Linked Lists.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
Data structure
Linked List Representation of a Linked List.pptx
Data Structure Lecture 5
Link list using array in Data structure amd algorithms
Funddamentals of data structures
Data Structures Algorithms and Applications

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Complications of Minimal Access Surgery at WLH
PPTX
GDM (1) (1).pptx small presentation for students
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
O7-L3 Supply Chain Operations - ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
GDM (1) (1).pptx small presentation for students
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Chinmaya Tiranga quiz Grand Finale.pdf
Pharma ospi slides which help in ospi learning
STATICS OF THE RIGID BODIES Hibbelers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Abdominal Access Techniques with Prof. Dr. R K Mishra
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Microbial disease of the cardiovascular and lymphatic systems
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

linklisr

  • 1. Linked Lists An Alternative Collections Data Structure What Is A Linked List? A data structure using memory that expands and shrinks as needed Relies on identical nodes “pointing” or “linked” to other nodes Each node is an instance of a class (i.e. an object) Head Example Declarations public class IntNode { private int data; private IntNode link; … } OR public class Part { private int partNo; private double price; private Part link; … }
  • 2. Special Nodes Head node The first node in a linked list Almost all programs using linked lists will maintain a pointer to the head node An empty list is represented by storing Null in the head variable Tail node The last node in a linked list Many programs maintain a pointer to the tail node A tail node points to a “null” reference In an empty list tail will also be Null Necessary Methods Methods to access and modify data: e.g. getData(), getLink(), setData(int newData), setLink(??? newLink) Methods for creating nodes i.e. constructors Methods for adding and removing nodes Caution! Always make sure that your linked list methods work correctly with an empty list.
  • 3. Adding a Node to the Front of a List 1. Create a new node with the desired data, and the link pointing to the current head node 2. Make the “head” pointer point to the new node What happens if we started with an empty list? remove Head Removing the First Node of a List Make the head pointer point to what the current head node is pointing to What happens to the original head node? What if there was only one node in the list? remove Head Adding a Node After a Selected Node 1. Create a new node pointing to whatever the selected node is currently pointing to 2. Make the selected node point to the new node What happens if the selected node was the last node in the list? remove Head Selected
  • 4. Removing a Selected Node Head Selected Can we remove this? Removing the Node After a Selected Node Head Selected Can we remove this? Desired Result: Head Selected What happens if we removed the tail node? Manipulating Entire Linked Lists Traverse the entire list in a loop that terminates when the null pointer is reached In the body of the loop perform the necessary operation(s)
  • 5. Manipulating Entire Linked Lists Examples: count the elements of the list Print the elements of the list Update the values in the list (e.g. increase all prices by 10%) If implemented in the same class as the node (i.e. no separate list class), it is better to use static methods, this way the method can be used for empty lists Sample Code IntNode current; for (current = head; current != null; current = current.link) { … } Or: IntNode current; current = head; while (current != null) { … current = current.link; } Copying an Entire List public static IntNode copyList (IntNode head) { if (head == null) return null; IntNode copyHead = new IntNode (head.data,null); IntNode copyTail = copyHead; IntNode source = head; while (source.link != null) { source = source.link; copyTail.addNodeAfter(source.data); copyTail = copyTail.link; } return copyHead; }
  • 6. Copying a Part of a List Given a start node and an end node make a copy of that part of the list Can we return both the head and tail of the copy? How does the method work? Is it that much different from copying the full list? Linked List Application – IntLinkedBag Keeps track of integer elements Can add or delete elements Answers questions about its elements Can add all the contents of another bag Grows and shrinks in size as needed IntLinkedBag - Methods public void add (int element) public void addAll (IntLinkedBag addend) public int countOccurences(int target) public boolean remove (int target) public int size() public void trimToSize() public static IntLinkedBag union (IntLinkedBag b1, IntLinkedBag b2) public int grab() public void ensureCapacity (int minimumCapacity) ??? public int getCapacity() ???
  • 7. Another Linked List Application - DoubleLinkedSeq Similar to bag but sequence is preserved An additional instance variable is needed to hold the position of the “current” element Two additional instance variables “tail” and “precursor” may be used for convenience Some methods can be preserved, others must be modified Additional methods are needed Implementation: assignment 5 Doubly Linked Lists Similar to linked lists with pointers in both directions Head Tail Array vs. Linked Lists vs. Doubly Linked Lists Which is better for the following operations?: Random access Frequent additions/removals in the middle of a sequence Frequent forward and backward searching of a sequence Frequent size changes