CSE 2151 Lecture 3
CSE 2151 Lecture 3
Course No.: 0714 09 CSE 2151, Course Title: Data Structures and Algorithms
Electronics and Communication Engineering Discipline, Khulna University, Khulna
Md. Farhan Sadique
Email: [email protected]
This lecture is not a study material. Use this lecture as an outline. Follow the outline to study from the mentioned sources
after each section header. The sections of this lecture have been prepared from the mentioned sources.
• The list is not required to be contiguously present in the memory. The node can reside anywhere in the
memory and linked together to make a list. This achieves optimized utilization of space.
• List size is limited to the memory size and doesn't need to be declared in advance.
• Empty node cannot be present in the linked list.
• We can store values of primitive types or objects in the linked list.
• Circularly linked list: In a circular linked list, the last node points back to the head node, creating a
circular structure. It can be either singly or doubly linked.
• Doubly linked list: In a doubly linked list, each node contains references to both the next and previous
nodes. This allows for traversal in both forward and backward directions, but it requires additional
memory for the backward reference.
2
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique
3
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique
4
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique
We cannot easily delete the last node of a singly linked list. We cannot reach the node before the tail by
following next links from the tail. The only way to access this node is to start from the head of the list and
search all the way through the list. But such a sequence of link-hopping operations could take a long time.
5.4. Implementing a Singly Linked List Class (Goodrich et al.: 3.2.1)
5
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique
6
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique
Continued…
Review Questions
1. The removeFirst method of the SinglyLinkedList class includes a special case to reset the tail field to
null when deleting the last node of a list (see lines 64 and 65). What are the consequences if we were
to remove those two lines from the code? Explain why the class would or would not work with such a
modification.
2. Give an algorithm for finding the second-to-last node in a singly linked list in which the last node is
indicated by a null next reference.
Bibliography
• Website: https://www.javatpoint.com/singly-linked-list
• Website: https://www.geeksforgeeks.org/what-is-linked-list/
• Book: Data Structures and Algorithms in Java, Sixth Edition - Michael T. Goodrich, Roberto Tamassia and
Michael H. Goldwasser