0% found this document useful (0 votes)
16 views

CSE 2151 Lecture 3

The document discusses linked lists, including: 1. Linked lists are collections of nodes that store data and pointers to the next node. This allows flexible and efficient use of memory compared to arrays. 2. Common operations on singly linked lists include inserting nodes at the head or tail and removing the head node. 3. Linked lists come in different forms like singly, doubly, and circularly linked lists depending on whether nodes store a single or double pointer and if the last node points to the first.

Uploaded by

saymyname.pt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CSE 2151 Lecture 3

The document discusses linked lists, including: 1. Linked lists are collections of nodes that store data and pointers to the next node. This allows flexible and efficient use of memory compared to arrays. 2. Common operations on singly linked lists include inserting nodes at the head or tail and removing the head node. 3. Linked lists come in different forms like singly, doubly, and circularly linked lists depending on whether nodes store a single or double pointer and if the last node points to the first.

Uploaded by

saymyname.pt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Linked List

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.

1. Linked List (https://www.javatpoint.com/singly-linked-list)


Linked List can be defined as collection of objects called nodes that are randomly stored in the memory. A
node contains two fields i.e. data stored at that particular address and the pointer which contains the address
of the next node in the memory. The last node of the list contains pointer to the null.

2. Benefits of using Linked List (https://www.javatpoint.com/singly-linked-list)

• 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.

3. Array vs Linked List (https://www.javatpoint.com/singly-linked-list)


Array contains following limitations:
• The size of array must be known in advance before using it in the program.
• Increasing size of the array is a time taking process. It is almost impossible to expand the size of the
array at run time.
• All the elements in the array need to be contiguously stored in the memory. Inserting any element in
the array needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array. Using linked list is useful
because,
• It allocates the memory dynamically. All the nodes of linked list are non-contiguously stored in the
memory and linked together with the help of pointers.
• Sizing is no longer a problem since we do not need to define its size at the time of declaration. List
grows as per the program's demand and limited to the available memory space.
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique
4. Types of Linked List (https://www.geeksforgeeks.org/what-is-linked-list/)
There are mainly three types of linked lists:
• Singly linked list: In a singly linked list, each node contains a reference to the next node in the
sequence. Traversing a singly linked list is done in a forward direction.

• 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.

5. Singly Linked List (Goodrich et al.: 3.2)


The linked list instance must keep a reference to the first node of the list, known as the head. The last node of
the list is known as the tail (keeping a reference to the tail is optional). Traversing the linked list— starting at
the head and moving from one node to another by following each node’s next reference is known as link
hopping or pointer hopping. It is common for a linked list instance to keep a count of the total number of nodes
that comprise the list (size of the list).
5.1. Inserting an Element at the Head of a Singly Linked List
The main idea is that we create a new node, set its element to the new element, set its next link to refer to the
current head, and set the list’s head to point to the new node.

2
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique

5.2. Inserting an Element at the Tail of a Singly Linked List


In this case, we create a new node, assign its next reference to null, set the next reference of the tail to point
to this new node, and then update the tail reference itself to this new node.

3
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique

5.3. Removing an Element from a Singly Linked List

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)

• size(): Returns the number of elements in the list.


• isEmpty(): Returns true if the list is empty, and false otherwise.
• first(): Returns (but does not remove) the first element in the list.
• last(): Returns (but does not remove) the last element in the list.
• addFirst(e): Adds a new element to the front of the list.
• addLast(e): Adds a new element to the end of the list.
• removeFirst(): Removes and returns the first element of the list.

Fig. The Node class.

5
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique

6
Data Structures and Algorithms – Lecture 3 Md. Farhan Sadique

Fig. The SinglyLinkedList class.

6. Circularly Linked List (Goodrich et al.: 3.3)

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

You might also like