Basic operations on Doubly linked list
package Linked_List;
class Node2
{
int data;
Node2 prev = null;
Node2 next = null;
Node2()
{}
Node2(int a)
{
this.data = a;
}
}
public class DoublyLinkedList
{
Node2 head = null;
public void push(int a)
{
Node2 node = new Node2(a);
if(head == null)
head = node;
else
{
Node2 temp = this.head;
for(temp=this.head; temp.next!=null; temp=temp.next)
{}
temp.next = node;
node.prev = temp;
}
}
public void print()
{
Node2 temp = this.head;
for(temp=this.head; temp!=null; temp=temp.next)
System.out.print(temp.data + " <--> ");
System.out.println("null");
}
}
Comments
Post a Comment