UNIT - 5
Dr. Anu Singha
Assistant Professor, School of Computer Science and Engineering
MIT World Peace University, Pune.
Outline:
• Linked Lists Operations
• Search a Key in LL
• LL Types : Double LL, Circular LL
• LL Applications: Polynomial Expression Representation
• Two Polynomials Addition
Continue…
Search: Linear Search
Continue…
Linked List Applications
Applications of linked list in computer science
• Implementation of stacks and queues
• Implementation of graphs
• Dynamic memory allocation : We use linked list of free blocks.
• Maintaining directory of names
• Performing arithmetic operations on long integers
• Manipulation of polynomials by storing constants in the node of linked list representing sparse
matrices
Applications of linked list in real world
• Image viewer – Previous and next images are linked, hence can be accessed by next and previous
button.
• Previous and next page in web browser – We can access previous and next url searched in web
browser by pressing back and next button since, they are linked as linked list.
• Music Player – Songs in music player are linked to previous and next song. you can play songs either
from starting or ending of the list.
Types of Lists
• Depending on the way in which the links are used to maintain adjacency,
several different types of linked lists are possible.
– Linear singly-linked list (or simply linear list)
• One we have discussed so far.
– Doubly-linked list
– Circular-linked list
Continue…
Double Linked List
• Doubly linked list is a complex type of linked list in which a node contains a pointer to the
previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node
consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to
the previous node (previous pointer).
Continue…
Advantages of a Doubly Linked List
✓ Allows us to iterate in both directions.
✓ We can delete a node easily as we have access to its previous node.
✓ Reversing is easy.
✓ Can grow or shrink in size dynamically.
✓ Useful in implementing various other data structures.
Disadvantages of a Doubly Linked List
✓ Compared to a singly linked list, each node store an extra pointer which
consumes extra memory.
✓ Operations require more time due to the overhead of handling extra pointers as
compared to singly-linked lists.
✓ No random access of elements.
Continue…
Circular Linked List
• Circular Linked List is a variation of Linked list in which the first element points to the last
element and the last element points to the first element. Both Singly Linked List and Doubly
Linked List can be made into a circular linked list.
Continue…
Applications of CLL
• Circular linked lists are useful for playing video and sound files in “looping” mode
• Circular lists are useful in applications to repeatedly go around the list. For
example, when multiple applications are running on a PC, it is common for the
operating system to put the running applications on a list and then to cycle through
them, giving each of them a slice of time to execute, and then making them wait
while the CPU is given to another application.
Continue…
Advantages of a Circular Linked List
✓ We can go to any node from any node in the Circular linked list which was not
possible in the singly linked list if we reached the last node.
✓ Easily we can go to head from the last node.
✓ In a circular list, any node can be starting point means we can traverse each
node from any point.
Disadvantages of a Circular Linked List
✓ If not handled proper validation then code may go in an infinite loop.
✓ For the implementation perspective, to insert at the beginning we have to
traverse the complete list to find the last node.
✓ Harder to find the end of the list and loop control.
Polynomial Representation using LL
✓ Polynomial manipulations are one of the most important applications of linked
lists.
✓ A polynomial is a collection of different terms, each comprising coefficients,
and exponents.
✓ It can be represented using a linked list.
✓ While representing a polynomial using a linked list, each polynomial term
represents a node in the linked list.
✓ Each node of a linked list representing polynomial constitute three parts:
1. The first part contains the value of the coefficient of the term.
2. The second part contains the value of the exponent.
3. The third part, LINK points to the next term (next node).
Continue…
✓ Consider a polynomial P(x) = 7x4 + 15x3 + 2 x2 + 9.
✓ Here 7, 15, 2, and 9 are the coefficients, and 4,3,2,0 are the exponents of the
terms in the polynomial.
✓ On representing this polynomial using a linked list, we have
Addition of Polynomials
✓ To add two polynomials, we traverse the list P and Q.
✓ We take corresponding terms of the list P and Q and compare their exponents.
✓ If the two exponents are equal, the coefficients are added to create a new coefficient.
✓ If the new coefficient is equal to 0, then the term is dropped, and if it is not zero, it is
inserted at the end of the new linked list containing the resulting polynomial.
✓ If one of the exponents is larger than the other, the corresponding term is immediately
placed into the new linked list, and
✓ the term with the smaller exponent is held to be compared with the next term from the
other list.
✓ If one list ends before the other, the rest of the terms of the longer list is inserted at the
end of the new linked list containing the resulting polynomial.
Continue…
Continue…
Continue…
Continue…
Continue…
Questions ??