OneCompiler

link list

315

public class CircularLinkedList {
static class Node {
int data;
Node next;
Node() {}
Node(int data) {
this.data = data;
}
}

private Node head;
private Node tail;

// constructor
public CircularLinkedList() {
this.head = null;
this.tail = null;
}

public boolean isEmpty() {
return head == null;
}

/**

  • insertAtFirst
    **/
    public void insertAtFirst(int data) {
    Node newNode = new Node(data);
    //Checks if the list is empty.
    if (head == null) {
    //If list is empty, both head and tail would point to new node.
    head = newNode;
    tail = newNode;
    newNode.next = head;
    } else {
    //Store data into temporary node
    Node temp = head;
    //New node will point to temp as next node
    newNode.next = temp;
    //New node will be the head node
    head = newNode;
    //Since, it is circular linked list tail will point to head.
    tail.next = head;
    }
    }

/**

  • insertAtLast
  • */
    public void insertAtLast(int data) {
    //Create new node
    Node newNode = new Node(data);
    //Checks if the list is empty.
    if (head == null) {
    //If list is empty, both head and tail would point to new node.
    head = newNode;
    tail = newNode;
    newNode.next = head;
    } else {
    //tail will point to new node.
    tail.next = newNode;
    //New node will become new tail.
    tail = newNode;
    //Since, it is circular linked list tail will point to head.
    tail.next = head;
    }
    }

/**
*

  • Insert at specified Position
    */
    public void insertAtIndex(int data, int position) {
    Node temp, newNode;
    int i, count;
    newNode = new Node();
    temp = head;
    count = size();
    if (temp == null || size() < position)
    System.out.println("Index is greater than size of the list");
    else {
    newNode.data = data;
    for (i = 1; i < position - 1; i++) {
    temp = temp.next;
    }
    newNode.next = temp.next;
    temp.next = newNode;
    }
    }

/**

  • delete the first node.
    */
    public void deleteFirst() {
    if (head == null) {
    return;
    } else {
    if (head != tail) {
    head = head.next;
    tail.next = head;
    }
    //If the list contains only one element
    //then it will remove it and both head and tail will point to null
    else {
    head = tail = null;
    }
    }
    }

/**
*Delete at Last
*/
public void deleteLast() {
if (head == null) {
return;
} else {
if (head != tail) {
Node current = head;
//Loop will iterate till the second last element as current.next is pointing to tail
while (current.next != tail) {
current = current.next;
}
//Second last element will be new tail
tail = current;
//Tail will point to head as it is a circular linked list
tail.next = head;
}
//If the list contains only one element
//Then it will remove it and both head and tail will point to null
else {
head = tail = null;
}
}
}

/**
*

  • Delete at Specified Position
    */
    public void deleteNode(int data) {
    if (head == null)
    System.out.println("List is empty");
    // Find the required node
    Node currentNode = head;
    Node previousNode = new Node();
    while (currentNode.data != data) {
    if (currentNode.next == head) {
    System.out.println("Given node with data " + data + " is not found in the circular linked list.");
    break;
    }
    previousNode = currentNode;
    currentNode = currentNode.next;
    }
// Check if node is only node   
if (currentNode == head && currentNode.next == head) {
  head = null;
}

// If more than one node, check if   
// it is first node   
if (currentNode == head) {
  previousNode = head;
  while (previousNode.next != head) {
    previousNode = previousNode.next;
  }
  head = currentNode.next;
  previousNode.next = head;
}

// check if node is last node   
else if (currentNode.next == head) {
  previousNode.next = head;
} else {
  previousNode.next = currentNode.next;
}

}

/**

  • Display the list elements
    */
    public void display() {
    Node temp = head;
    if (head != null) {
    do {
    System.out.printf("%d ", temp.data);
    temp = temp.next;
    } while (temp != head);
    }
    System.out.printf("\n");
    }
    public static void main(String[] args) {
    CircularLinkedList list = new CircularLinkedList();
    list.insertAtFirst(1);
    list.display();
list.insertAtFirst(2);
list.display();

list.insertAtLast(3);
list.display();


list.insertAtLast(4);
list.display();

list.insertAtIndex(5, 3);
list.display();

list.deleteNode(8);
list.display();

list.deleteNode(2);
System.out.println("Node with data 2 has been deleted");
list.display();


}
}

Doubly Linked List – Lab Sheet
import java.io.*;
class node {
node prev;
int data;
node next;
node(int value) // A constructor is called here
{
// By default previous pointer is pointed to NULL
prev = null;
// Value is assigned to the data
data = value;
// By default next pointer is pointed to NULL
next = null;
}
}
class DLLMain {
// Declaring an empty doubly linked list
static node head = null;
static node tail = null;
static void insertAtBeginning(int data)
{
node temp = new node(data);
if (head == null) {
head = temp;
tail = temp;
}
else {
temp.next = head;
head.prev = temp;
head = temp;
}
}
static void insertAtEnd(int data)
{
node temp = new node(data);
if (tail == null) {
head = temp;
tail = temp;
}
else {
tail.next = temp;
temp.prev = tail;
tail = temp;
}
}
static void insertAtPosition(int data, int position)
{
node temp = new node(data);
if (position == 1) {
insertAtBeginning(data);
}
else {
node current = head;
int currPosition = 1;
while (current != null
&& currPosition < position) {
current = current.next;
currPosition++;
}
if (current == null) {
insertAtEnd(data);
}
else {
temp.next = current;
temp.prev = current.prev;
current.prev.next = temp;
current.prev = temp;
}
}
}
static void deleteAtBeginning()
{
if (head == null) {
return;
}
if (head == tail) {
head = null;
tail = null;
return;
}
node temp = head;
head = head.next;
head.prev = null;
temp.next = null;
}
static void deleteAtEnd()
{
if (tail == null) {
return;
}
if (head == tail) {
head = null;
tail = null;
return;
}
node temp = tail;
tail = tail.prev;
tail.next = null;
temp.prev = null;
}
static void deleteAtSpecificPosition(int pos)
{
if (head == null) {
return;
}
if (pos == 1) {
deleteAtBeginning();
return;
}
node current = head;
int count = 1;
while (current != null && count != pos) {
current = current.next;
count++;
}
if (current == null) {
System.out.println("Position wrong");
return;
}
if (current == tail) {
deleteAtEnd();
return;
}
current.prev.next = current.next;
current.next.prev = current.prev;
current.prev = null;
current.next = null;
}
static void display(node head)
{
node temp = head;
while (temp != null) {
System.out.print(temp.data + " --> ");
temp = temp.next;
}
System.out.println("NULL");
}
// Drivers code
public static void main(String[] args)
{
insertAtEnd(1);
insertAtEnd(2);
insertAtEnd(3);
insertAtEnd(4);
insertAtEnd(5);
System.out.print("After insertion at tail: ");
display(head);
System.out.print("After insertion at head: ");
insertAtBeginning(0);
display(head);
insertAtPosition(6, 2);
System.out.print(
"After insertion at 2nd position: ");
display(head);
deleteAtBeginning();
System.out.print(
"After deletion at the beginning: ");
display(head);
deleteAtEnd();
System.out.print("After deletion at the end: ");
display(head);
deleteAtSpecificPosition(2);
System.out.print(
"After deletion at 2nd position: ");
display(head);
}
}

/Single linked list/

import java.util.Scanner;

public class SinglyLinkedList
{ //defining a node in singly linked list
class Node
{
int data;
Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
//defining the head and tail of a singly linked list
public Node head = null;
public Node tail = null;
//defining insert() function to add a node to the list
public void insert_begin(int data)
{
//Creating a new node
Node newNode = new Node(data);
//checking of the list is empty
if(head == null)
{
//if the given list is empty, making the two nodes head and tail to point to the newly created node newNode
head = newNode;
tail=newNode;
tail.next=null;

    }    
    else 
    {    

//otherwise the newNode will be added after tail so that the next pointer of tail points to the newNode

    	newNode.next=head;
        head=newNode;
        
    }    
} 
public void insert_end(int data) 
{    
    
    Node newNode = new Node(data);               
    
    if(head == null) 
    {    
        head = newNode;
        tail=newNode;
        tail.next=null;  
        
    }    
    else 
    {    
        Node  temp=head;
        while(temp.next!=null) {
        	temp=temp.next;
        }
        temp.next=newNode;
        tail=newNode;
        tail.next=null;
    	           
    }    
}          

//defining displaylist() function to display the data in the list  
public void display() 
{    
    //Pointing the head to the node called current    
    Node temp = head;               

    if(head == null)
    {    
        System.out.println("The given list is empty");    
        return;    
    }    
    System.out.println("The data in the given list are: ");    
    while(temp != null) 
    {    
        //printing each data in the list and next pointer pointing to the next node   
        System.out.print(temp.data + "----> ");    
        temp = temp.next;    
    }    
    System.out.println("NULL");    
}            
	public static void main(String[] args) {