Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

insert node at end of doubly linked list

// Java Program 
// Insert node at end of circular doubly linked list

// Define class of linked list Node
class LinkNode
{
    public int data;
    public LinkNode next;
    public LinkNode prev;
    public LinkNode(int data)
    {
        // Set node value
        this.data = data;
        this.next = null;
        this.prev = null;
    }
}
public class CircularDLL
{
    public LinkNode head;
    public LinkNode tail;
    public CircularDLL()
    {
        // Set head and tail values
        this.head = null;
        this.tail = null;
    }
    // Insert node at end of circular doubly linked list
    public void insert(int value)
    {
        // Create a node
        LinkNode node = new LinkNode(value);
        if (this.head == null)
        {
            // First node of linked list
            this.head = node;
            this.tail = node;
            node.next = node;
            node.prev = node;
        }
        else
        {
            node.next = this.head;
            node.prev = this.tail;

            this.head.prev = node;
            this.tail.next = node;

            // Set new last node
            this.tail = node;
        }
    }

    public void headToTail()
    {
        if (this.head == null)
        {
            System.out.println("Empty linked list");
        }
        else
        {
            LinkNode temp = this.head;
            System.out.println("
Node Form Front to Rear :");
            while (temp != null)
            {
                System.out.print(temp.data + "  ");
                temp = temp.next;
                if (temp == this.head)
                {
                    return;
                }
            }
        }
    }
    public void tailToHead()
    {
        if (this.tail == null)
        {
            System.out.print("Empty linked list");
        }
        else
        {
            LinkNode temp = this.tail;
            System.out.println("
Node Form Rear to Front :");
            while (temp != null)
            {
                System.out.print(temp.data + "  ");
                temp = temp.prev;
                if (temp == this.tail)
                {
                    return;
                }
            }
        }
    }
    public static void main(String[] args)
    {
        CircularDLL cdll = new CircularDLL();
        // Add following linked list nodes
        cdll.insert(1);
        cdll.insert(2);
        cdll.insert(3);
        cdll.insert(4);
        cdll.insert(5);
        cdll.insert(6);
        // Display node
        cdll.headToTail();
        cdll.tailToHead();
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java add values to array 
Java :: using super in java 
Java :: java first index of an arraylist 
Java :: java hashmap 
Java :: Java Exception handling using try...catch 
Java :: enum class in java 
Java :: How to remove an element from a Java List? 
Java :: Java forEach() Method 
Java :: round int java 
Java :: check if object is empty java 8 
Java :: prime factors of a number 
Java :: java hashmap set value 
Java :: java firebase syncrhonous data read 
Java :: fragment to activity typecasting 
Java :: if else bedingungen java 
Java :: edit text on 2sec change andropid 
Java :: method object class in android 
Java :: how to set credentials for speechClient Java google api 
Java :: coin flip random java 
Java :: input method manager hide keyboard 
Java :: Copying value from one input field to another input field using checkbox 
Java :: java deserialize json array 
Java :: transform primitive float array to float array 
Java :: How can I add a listener on the ok button of JOptionPane 
Java :: using for each loop for searching in array 
Java :: Java HashMap Class Declaration 
Java :: OCA Exam Questions 
Java :: install the app from assest of other app 
Java :: how to set to nothing a ComboBox in java 
Java :: java jackson optional 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =