MODULE 2
MODULE 2
LINKED LIST
Linked list
o Linked List can be defined as collection of objects called nodes that are randomly
stored in the memory.
o 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.
o The last node of the list contains pointer to the null.
o 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.
o list size is limited to the memory size and doesn't need to be declared in advance.
o Empty node cannot be present in the linked list.
o We can store values of primitive types or objects in the singly linked list.
Till now, we were using array data structure to organize the group of elements that are to be
stored individually in the memory. However, Array has several advantages and
disadvantages which must be known in order to decide the data structure which will be used
throughout the program.
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to expand
the size of the array at run time.
3. 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,
1. 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.
2. 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.
There are majorly 3 types of linked lists in the data structures that can be implemented. And
that is –
Singly Linked List is a type of linked list that is Unidirectional. It can be only one
directional.(Head Node to Tail Node).
The singly linked list is a linear data structure in which each element of the list contains a
pointer which points to the next element in the list. Each element in the singly linked list is
called a node. Each node has two components: data and a pointer next which points to the
next node in the list. The first node of the list is called as head, and the last node of the list is
called a tail. The last node of the list contains a pointer to the null. Each node in the list can
be accessed linearly by traversing through the list from head to tail.
nodename.data=value;
Currentnodename.next = Nextnodename;
Traversing is the most common operation that is performed in almost every scenario of
singly linked list. Traversing means visiting each node of the list once in order to perform
some operation on that. This will be done by using the following statements.
Program:
//creations of node:
Node head=new Node();
Node second=new Node();
Node third=new Node();
Node fourth=new Node();
Node fifth=new Node();
//assign the value
head.data=1;
second.data=2;
third.data=3;
fourth.data=4;
fifth.data=5;
Implementation
import java.util.Scanner;
// Search for the key to be deleted, keep track of the previous node
while(temp!=null && temp.data!=key)
{
data=temp;
temp=temp.next;
}
head.next=second;
second.next=third;
third.next=fourth;
fourth.next=fifth;
fifth.next=null;
deleteNode(head, m);
Link − Each Link of a linked list can store a data called an element.
Next − Each Link of a linked list contain a link to next link called Next.
Prev − Each Link of a linked list contain a link to previous link called Prev.
Basic Operations
Following are the basic operations supported by an list.
Insertion − add an element at the list.
Deletion − delete an element at the list.
Display forward − displaying complete list in forward manner.
Display backward − displaying complete list in backward manner.
nodename.data=value;
Currentnodename.next = Nextnodename;
Currentnodename.prev = Previousnodename;
To traverse in a doubly linked list, we will start from the head node and follow the
next reference until we reach the end of the list similarly, we can also start from the tail node
and follow the prev until we reach the head of the node.
Forward Traversing:
Backward Traversal:
Program:
public class doubly
{
static class Node
{
int data;
Node next;
Node prev;
}
static void Printnext(Node n)
{
while(n!=null)
{
System.out.println(n.data);
n=n.next;
}
}
static void Printprev(Node n)
{
while(n!=null)
{
System.out.println(n.data);
n=n.prev;
}
}
public static void main(String args[])
{
//Node Creations
Node head=new Node();
Node second=new Node();
Node tail=new Node();
tail.prev=second;
second.prev=head;
head.prev=null;
}
Output:
Head Node to Tail Node
10
20
30
Tail Node to Head Node
30
20
10
// Traverse the list to find the node with the specified data value
while (current != null && current.data != valueToDelete)
current = current.next;
// If the node to delete is the head node, update the head reference
if (current == head)
head = current.next;
// Set the next and previous references of the node to delete to null
current.next = null;
current.pre = null;
}
return head;
}
head.data = 10;
two.data = 20;
three.data = 30;
four.data = 40;
head.next = two;
two.next = three;
three.next = four;
four.next = null;
head.pre = null;
two.pre = head;
three.pre = two;
four.pre = three;
Output:
The circular linked list is a linked list where all nodes are connected to form a circle. In a circular
linked list, the first node and the last node are connected to each other which form a circle. There is
no NULL at the end.
Circular Singly Linked List has properties of both singly linked list and circular linked 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 the circular singly linked list until we reach the same node where we started. The circular
singly linked list has no beginning or end. No null value is present in the next part of any of the
nodes.
{
this.value = value;
}
}
Program:
class Node
{
int data;
Node next;
public CircularSinglyLinkedList()
{
this.head = null;
this.tail = null;
}
Output:
Circular Doubly Linked List has properties of both doubly linked list and circular linked list in
which two consecutive elements are linked or connected by the previous and next pointer and the
last node points to the first node by the next pointer and also the first node points to the last node by
the previous pointer.
class Node
{
int data;
Node next;
Node prev;
head.prev = hello;
hello.prev = tail;
head = hello;
}
}
tail = hello;
}
}
Node n = head;
do
{
System.out.print(n.data + " ");
n = n.next;
} while (n != head);
System.out.println();
}
}
list.display();
// Add at the beginning
list.addAtBeginning(10);
list.display();
list.addAtBeginning(20);
list.display();
// Add at the end
list.addAtEnd(30);
list.display();
list.addAtEnd(40);
list.display();
}
}
Output:
// set the .next pointer of the new node to point to the current
// top node of the list
node.next = top;
return top;
}
stack.push(1);
stack.push(2);
stack.push(3);
if (stack.isEmpty()) {
System.out.println("The stack is empty");
}
else {
System.out.println("The stack is not empty");
}
}
}
Output:
Inserting 1
Inserting 2
Inserting 3
The top element is 3
Removing 3
Removing 2
Removing 1
The stack is empty
import java.util.*;
public class Main
{
private Node front, rear;
private int s; // number of items
public Main ()
{
front = null;
rear = null;
s = 0;
}
}
//to Remove item
public void dequeue ()
{
int data = front.data;
front = front.next;
if (isEmpty ())
{
rear = null;
}
s--;
queue.dequeue ();
queue.dequeue ();
System.out.println ("Queue after dequeue");
queue.disp ();
}
}
Output:
1 added to the queue
2 added to the queue
3 added to the queue
4 added to the queue
Original queue :
1234
Queue after dequeue
34