Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 1
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Recognize need of a Data Structure, which Dynamically can Shrink and Grow  Realization of Linked List as Dynamic Data StructureTo understand the well-defined, clear, and simple approach of program design  Utilize Flexibility of the same Easily and EffectivelyTo understand sequential organization of data  Learn Variations of Linked List and Use them for Appropriate Applications 2
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 3  The linked list is a very effective and efficient dynamic data structure  Data items can be stored anywhere in memory in a scattered manner  To maintain the specific sequence of these data items we need to maintain link(s) with successor (and/ or predecessor)  It is called as a linked list
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  A linked list is an ordered collection of data in which each element contains minimum two values, data and link(s) to its successor (and/ or predecessor)  The list with one link field using which every element is associated to either its predecessor or successor is called as singly linked list  In a linked list, before adding any element to the list, memory space for that node must be allocated  A link is kept with each item to the next item in the list 4
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 5 Fig: 1 Data Organization
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 6 Fig: 2 (a): A linked list of n elements Fig: 2 (b) : A linked list of weekdays
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Each node of the linked list has minimum two elements : The data member(s) being stored in the list A pointer or link to the next element in the list  The last node in the list contains a NULL (or -1) pointer to indicate that it is the end or tail of the list 7
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  The last node in the list contains a NULL (or -1) pointer to indicate that it is the end or tail of the list  Provides static allocation, which means space allocation is done by compiler once cannot be changed during execution and size has to be known in advance.  As individual objects are stored at fixed distance apart, we can access any element randomly.  Insertion and deletion of objects in between the list requires a lot of data movement.  Space inefficient for large objects and large quantity with often insertions and deletions  Element need not know/store keep address of its successive element. 8
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Element can be placed anywhere in the memory  Dynamic allocation (size need not be known in advance) i.e. space allocation as per need can be done during execution.  As objects are not placed at fixed distance apart, random access to elements is not possible.  Insertion and deletion of objects do not require any data shifting.  Space efficient for large objects and large quantity with often insertions and deletions  Each element in general is a collection of data and a link. At least one link field is must.  Every element keeps address of its successor element in a link field. 9
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 10  Some more operations, which are based on above basic operations are:  Searching a node  Updating node.  Printing the node or list.  Counting length of the list.  Reverse the list.  Sort the list using pointer manipulation.  Concatenate two lists.  Merge two-sorted list into third sorted list
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 11  Data Structure of Node  Insertion of a Node  Linked List Traversal Non-Recursive Method Recursive Traversal Method
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 12  Structure of Node  Doubly linked list
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 13  A linked list in which every node has one link field, to provide information about where the next node of list is, is called as singly linked list
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 14  In doubly linked list, each node has two link fields to store information about who is the next and also about who is ahead of the node  Hence each node has knowledge of its successor and also its predecessor.  In doubly linked list, from every node the list can be traversed in both the directions.
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 15 The other classification of linked lists is,  Linear linked list  Circular linked list
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 16  The linked list that we have seen so for are often known as linear linked lists.  All elements of such a linked list can be accessed by first setting up a pointer pointing to the first node of the list and then traversing the entire list. Linear linked list
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 17  For example, consider a singly linked list.  Given a pointer A to a node in a linear list, we cannot reach any of the nodes that precede the node to which A is pointing.  This disadvantage can be overcome by making a small change. This change is without any additional data structure.  The link field of last node is set to NULL in linear list to mark end of list. This link field of last node can be set to point first node rather than NULL. Such a linked list is called a circular linked list. Although a linear linked list is a useful and popular data structure, it has some shortcomings.
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 18 Circular linked list
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 19 Doubly Linked List We can use doubly linked lists in which each node contain two links, one to its predecessor and other to its successor
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 20 Polynomial Manipulations A node will have 3 fields, which represent the coefficient and exponent of a term and a pointer to the next term
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 21  For instance, the polynomial, say A = 6x7 + 3x5 + 4x3 + 12 would be stored as :
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 22 Operations on Polynomials Polynomial evaluation Polynomial addition Multiplication of two polynomials Representation of sparse matrix using linked list Linked list implementation of the stack Generalized linked list
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 23  Overflow : Sometimes new data node is to be inserted into data structure but there is no available space i.e. free-pool is empty. This situation is called overflow  Underflow : This refers to situation where programmer wants to delete a node from empty list  For good memory utilization, operating system periodically collects all the free blocks and inserts into free-pool  Any technique that does this collection is called garbage collection
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 24  There are two implementation of linked linear list; array and pointers  There is a need is of such a data structure which can dynamically shrink and grow  Hence the popular implementation us using pointers and dynamic memory management  Singly linked lists are useful data structures, especially if you need to automatically allocate and de-allocate space in a list  The basic operation are create list, transverse the list, insert and delete a node  There are two variation of linked list singly and doubly linked list. Both linked list can be circular lists  The linked could be with or without head node. Head node is used to store some information about the list, so that it can be accessed without traversing the same
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 25  In doubly linked list, each node has two link fields to store information about who is the next and also about who is ahead of the node  Hence each node has knowledge of its successor and also its predecessor.  In doubly linked list, from every node the list can be traversed in both the directions.  Information could be like total number of nodes in the list and similarly any other Linked list is the most popular data structure used. It has many application such as process queue, print queue, garbage collection etc
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 26 End of Chapter 6 …!

6. Linked list - Data Structures using C++ by Varsha Patil

  • 1.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 1
  • 2.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  Recognize need of a Data Structure, which Dynamically can Shrink and Grow  Realization of Linked List as Dynamic Data StructureTo understand the well-defined, clear, and simple approach of program design  Utilize Flexibility of the same Easily and EffectivelyTo understand sequential organization of data  Learn Variations of Linked List and Use them for Appropriate Applications 2
  • 3.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 3  The linked list is a very effective and efficient dynamic data structure  Data items can be stored anywhere in memory in a scattered manner  To maintain the specific sequence of these data items we need to maintain link(s) with successor (and/ or predecessor)  It is called as a linked list
  • 4.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  A linked list is an ordered collection of data in which each element contains minimum two values, data and link(s) to its successor (and/ or predecessor)  The list with one link field using which every element is associated to either its predecessor or successor is called as singly linked list  In a linked list, before adding any element to the list, memory space for that node must be allocated  A link is kept with each item to the next item in the list 4
  • 5.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 5 Fig: 1 Data Organization
  • 6.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 6 Fig: 2 (a): A linked list of n elements Fig: 2 (b) : A linked list of weekdays
  • 7.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  Each node of the linked list has minimum two elements : The data member(s) being stored in the list A pointer or link to the next element in the list  The last node in the list contains a NULL (or -1) pointer to indicate that it is the end or tail of the list 7
  • 8.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  The last node in the list contains a NULL (or -1) pointer to indicate that it is the end or tail of the list  Provides static allocation, which means space allocation is done by compiler once cannot be changed during execution and size has to be known in advance.  As individual objects are stored at fixed distance apart, we can access any element randomly.  Insertion and deletion of objects in between the list requires a lot of data movement.  Space inefficient for large objects and large quantity with often insertions and deletions  Element need not know/store keep address of its successive element. 8
  • 9.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil  Element can be placed anywhere in the memory  Dynamic allocation (size need not be known in advance) i.e. space allocation as per need can be done during execution.  As objects are not placed at fixed distance apart, random access to elements is not possible.  Insertion and deletion of objects do not require any data shifting.  Space efficient for large objects and large quantity with often insertions and deletions  Each element in general is a collection of data and a link. At least one link field is must.  Every element keeps address of its successor element in a link field. 9
  • 10.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 10  Some more operations, which are based on above basic operations are:  Searching a node  Updating node.  Printing the node or list.  Counting length of the list.  Reverse the list.  Sort the list using pointer manipulation.  Concatenate two lists.  Merge two-sorted list into third sorted list
  • 11.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 11  Data Structure of Node  Insertion of a Node  Linked List Traversal Non-Recursive Method Recursive Traversal Method
  • 12.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 12  Structure of Node  Doubly linked list
  • 13.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 13  A linked list in which every node has one link field, to provide information about where the next node of list is, is called as singly linked list
  • 14.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 14  In doubly linked list, each node has two link fields to store information about who is the next and also about who is ahead of the node  Hence each node has knowledge of its successor and also its predecessor.  In doubly linked list, from every node the list can be traversed in both the directions.
  • 15.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 15 The other classification of linked lists is,  Linear linked list  Circular linked list
  • 16.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 16  The linked list that we have seen so for are often known as linear linked lists.  All elements of such a linked list can be accessed by first setting up a pointer pointing to the first node of the list and then traversing the entire list. Linear linked list
  • 17.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 17  For example, consider a singly linked list.  Given a pointer A to a node in a linear list, we cannot reach any of the nodes that precede the node to which A is pointing.  This disadvantage can be overcome by making a small change. This change is without any additional data structure.  The link field of last node is set to NULL in linear list to mark end of list. This link field of last node can be set to point first node rather than NULL. Such a linked list is called a circular linked list. Although a linear linked list is a useful and popular data structure, it has some shortcomings.
  • 18.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 18 Circular linked list
  • 19.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 19 Doubly Linked List We can use doubly linked lists in which each node contain two links, one to its predecessor and other to its successor
  • 20.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 20 Polynomial Manipulations A node will have 3 fields, which represent the coefficient and exponent of a term and a pointer to the next term
  • 21.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 21  For instance, the polynomial, say A = 6x7 + 3x5 + 4x3 + 12 would be stored as :
  • 22.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 22 Operations on Polynomials Polynomial evaluation Polynomial addition Multiplication of two polynomials Representation of sparse matrix using linked list Linked list implementation of the stack Generalized linked list
  • 23.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 23  Overflow : Sometimes new data node is to be inserted into data structure but there is no available space i.e. free-pool is empty. This situation is called overflow  Underflow : This refers to situation where programmer wants to delete a node from empty list  For good memory utilization, operating system periodically collects all the free blocks and inserts into free-pool  Any technique that does this collection is called garbage collection
  • 24.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 24  There are two implementation of linked linear list; array and pointers  There is a need is of such a data structure which can dynamically shrink and grow  Hence the popular implementation us using pointers and dynamic memory management  Singly linked lists are useful data structures, especially if you need to automatically allocate and de-allocate space in a list  The basic operation are create list, transverse the list, insert and delete a node  There are two variation of linked list singly and doubly linked list. Both linked list can be circular lists  The linked could be with or without head node. Head node is used to store some information about the list, so that it can be accessed without traversing the same
  • 25.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 25  In doubly linked list, each node has two link fields to store information about who is the next and also about who is ahead of the node  Hence each node has knowledge of its successor and also its predecessor.  In doubly linked list, from every node the list can be traversed in both the directions.  Information could be like total number of nodes in the list and similarly any other Linked list is the most popular data structure used. It has many application such as process queue, print queue, garbage collection etc
  • 26.
    Oxford University Press© 2012Data Structures Using C++ by Dr Varsha Patil 26 End of Chapter 6 …!