0% found this document useful (0 votes)
2 views22 pages

Unit-5_Java

The document provides an overview of data structures implemented in Java, focusing on singly and singly circular linked lists. It details the concepts, advantages, disadvantages, and operations such as insertion, deletion, and traversal for both types of linked lists. Additionally, it includes code examples for creating and manipulating singly linked lists, along with applications and characteristics of circular linked lists.

Uploaded by

salonisingh58494
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)
2 views22 pages

Unit-5_Java

The document provides an overview of data structures implemented in Java, focusing on singly and singly circular linked lists. It details the concepts, advantages, disadvantages, and operations such as insertion, deletion, and traversal for both types of linked lists. Additionally, it includes code examples for creating and manipulating singly linked lists, along with applications and characteristics of circular linked lists.

Uploaded by

salonisingh58494
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/ 22

Unit-5

5.1 Implementation of Data Structure using Java


Class:
5.1.1 Concepts of singly and singly circular link
list
5.1.2 Singly Link List : Create, traverse, insert,
delete node
5.1.3 Singly circular link list: create, traverse,
insert, delete node
► Linked List is a linear data structure. Linked list elements
are not stored at a contiguous location, the elements
are linked using pointers.
► Types of Linked List
1) Singly Link list
2) Doubly Link list
3) Circular Link List
Singly link list

• Linked List can be defined as a 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 the pointer to
the null.

Advantage of Linked Lists


• It is dynamic. It allocates memory when required.
• It can easily implement Insertion and deletion
operations.
• It reduces access time.
Disadvantages of Linked Lists

► Memory is wasted because the Linked List


requires extra memory to store.
• It cannot access elements randomly. • It is very
difficult to perform Reverse Traversing.
Operations

• Traversal - access each element of the linked


list
• Insertion - adds a new element to the linked
list
• Deletion - removes the existing elements
• Search - find a node in the linked list •
Sort - sort the nodes of the linked list
Create Singly Link List
public class SinglyLinkedList {
// reference to head / first node of the Singly Linked
List public Node head = null;
// class Node that hold data and a
reference/link // to the next Node in the list
class Node {
private int data;
private Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
Display Singly Link List
public void print() {
if (this.head == null) {
System.out.println("The List is empty.");}
else {
System.out.println("The contents of the Singly Linked List are :
"); Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + " -> ");
cur = cur.next;
}

}}
Insertion
► In a single linked list, the insertion
operation can be performed in three
ways. They are as follows...
1. Inserting At Beginning of the
list 2. Inserting At End of the list
3. InsertingAt Specific location in the list
Inserting At Beginning of the list
• Assignthe reference of the HEAD to the new node’s next field.
• Make the new node as the HEAD of the list.
• //Point the new node's next to head
• newNode.next = this.head;
• // Make the new node as head
• this.head = newNode;
Inserting At End of the list
• Traverse the list until we find the last node.
• The new node’s reference is assigned to the last node’s next field.
• Node cur = this.head; • // traverse to the end of the list
• while (cur.next != null) {
• cur = cur.next;
•}
• cur.next = newNode;
Inserting At Specific location in the list
• Traverse (position – 1) times or till the end of the list is reached and
maintain previous and current references.
• Assign the reference of the new node to the prev node’s next field. •
Assign the cur node’s reference to the new node’s next field.

// traverse to the end of the list and check positions


moved while (cur.next != null && --position > 0) {
// update the prev and cur references
prev = cur;
cur = cur.next;
}
// update prev to point to new node
prev.next = newNode;

// & new node to point to current node


newNode.next = cur;
Deleting Nodes From a Singly Linked List

► FirstNode: If the node to be deleted is the first


node itself, we assign the reference of the next of
the HEAD node to the HEAD node.

// If the data is found at the first node


if (this.head.data == data) {
this.head = this.head.next;
return;
}
Delete Last Node or Any Other Node:
• To delete any other node in the list, we traverse the list
keeping track of the previous and current nodes in the list
until we find the node to be deleted with the required data
field or we reach the end of the list i.e. NULL without finding
the data element in the list.
• If the node is found, we assign the reference of the next field
of the current node to the previous node’s next.
// Traverse the list until it ends or you
// find the node that holds the data
while (cur != null && cur.data != data) {

// update the prev and cur references


prev = cur;
cur = cur.next;
}

// If the node was found, adjust the prev node


// to point to the next of the node to be deleted.
if (cur != null) {
prev.next = cur.next;
} else {
System.out.println("The data " + data + " could not be found in the
List"); }

Applications of Singly Linked Lists

• To implement complex data structures i.e. Stacks , Queues


and Skip Lists.
• To implement the adjacency list representation of a Graph.
Singly Circular Link List
► In a circular Singly linked list, the last node of the list
contains a pointer to the first node of the list.
► We traverse a circular singly linked list until we reach
the same node where we started.

► The circular singly liked list has no beginning and no


ending. There is no null value present in the next part
of any of the nodes.
Advantages

► Themajor advantage is that we can start from any node


and still we can traverse the entire list.
► We can maintain one pointer “last” and get the first
(head) node using the next pointer of the last node.
► We no longer need a NULL assignment unless the list itself
is empty.
► It is very useful in CPU round robin scheduling.
Disadvantages

► The biggest disadvantage of this list is that if it is


not programmed correctly, you will end up in an
infinite loop.
► Implementing a circular linked list is complex
compared to singly linked list.
Insert the new node as the first node:
1. The new node’s next will point to the first node or the
last node’s next.
2. The last node’s next will point to the new node.

Insert at the End of the Linked List:


1. The new node next will point to the current last node’s
next.
2. The current node’s next will point to the new node.
3. The last pointer will point to the new node.

Delete the First Node

To delete the first node from the circular linked list,


point the last node’s next to the last node’s next’s
next.

Delete the last element

► To delete the last element from the circular linked list, traverse till the
second last element and change the previous node’s next to the last
node’s next.

You might also like