Java Doubly Linked List Insert
Java Doubly Linked List Insert
[Implementation]
}
temp.next=newNode;
newNode.previous=temp;
}
}
public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
System.out.print(current.data + "<==>");
current = current.next;
}
System.out.println();
}
//This function will add the new node at the end of the list.
public void addAtStart(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 {
//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;
}
}
//This function will add the new node at the end of the list.
public void addAtEnd(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 points to head.
tail.next = head;
}
}
//This function will add the new node at the end of the list.
public void add(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;
}
}
class Stack {
private int arr[];
Stack(int size)
{
capacity = size;
top = -1;
if (isFull())
{
System.out.println("Stack OverFlow");
System.exit(1);
arr[++top] = x;
if (isEmpty())
{
System.out.println("STACK EMPTY");
System.exit(1);
return arr[top--];
return top + 1;
}
public Boolean isEmpty()
{
}
}
stack.push(1);
stack.push(2);
stack.push(3);
System.out.print("Stack: ");
stack.printStack();
stack.pop();
System.out.println("\nAfter pop");
stack.printStack();
}
==============================